问题
The notification message string in my app only shows if the development device is connected to USB. As soon as I disconnect the phone from USB, even with the app open, the notifications for my app no longer shows the message, it is just blank, yet everything else seems to work fine for the notification.
I have no errors in logcat when running app in ADB, and all other params are passed successfully to the notification. The only issue is the setContentText() only seems to work if phone is connected to computer via USB, even if Android Studio/ADB is not running.
The dev device is an old Samsung SPH-L300 (el-cheapo Virgin mobile phone) running 4.1.2. I don't have another device to test it on. I am using Android Studio 1.5.1 as the IDE.
Am I missing some manifest element? I've heard of a similar problem but it was the other way around, where it only worked when NOT connected to USB. The solution to that, being to change the SDK minimum and target in gradle.build, did not solve my problem (although, I could have missed something when trying that).
The notification is activated using a broadcast receiver from an alarm manager....here is the code:
package com.example.notificationApp;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Color;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
public class AlarmReceiver extends BroadcastReceiver {
public AlarmReceiver() {
}
@Override
public void onReceive(Context context, Intent intent) {
// get the info needed to identify this specific reminder...
Bundle bundle = intent.getExtras()
int alarmID = bundle.getInt("reminderID");
Intent returnIntent = new Intent(context, HomeActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(context, alarmID, returnIntent, 0);
Uri alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
// make vibrate pattern...
long[] pattern = {500,500,500,500,500,500,500,500,500};
Notification notify = new NotificationCompat.Builder(context)
.setSmallIcon(R.mipmap.ic_launcher)
.setTicker("Appointment reminder")
.setContentTitle("Reminder...")
.setContentText("TEST TEXT")
.setSound(alarmUri)
.setLights(Color.BLUE, 500, 500)
.setVibrate(pattern)
.setContentIntent(pIntent)
.setAutoCancel(true)
.build();
NotificationManager notificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(alarmID, notify);
}
And here is my Manifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.notificationApp">
<!-- Permission to start Alarm on device reboot -->
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-sdk
android:minSdkVersion="15"
android:targetSdkVersion="23" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".HomeActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".AutoStart">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<receiver android:name=".CheckAlarmReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<receiver android:name=".AlarmReceiver" />
<!--
ATTENTION: This was auto-generated to add Google Play services to your project for
App Indexing. See https://g.co/AppIndexing/AndroidStudio for more information.
-->
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
</application>
I am stumped on this one, anyone have any clues? Thanks in advance!!
回答1:
So I found the problem and wanted to post it in case anyone else ends up with the same issue. Believe it or not, the problem was in the phone's developer settings with Stay Awake mode on. Turn off Stay Awake in developer settings and voila!
I got the idea from this other post I just found:
https://stackoverflow.com/a/25566924/3716557
(Please direct any upvotes to the answer in link)
来源:https://stackoverflow.com/questions/36706450/notification-message-only-shows-when-device-connected-to-usb