我使用不同的文字创建多个通知。但是,AlarmManager始终显示具有相同文本的通知,如果未刷新旧通知,则替换旧通知。 NOTIFY_ID始终不同(已调试)。另外我发现如果在显示通知后我在onRecieve方法中崩溃应用它工作正常......这是代码:
public class Schedule extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "");
wakeLock.acquire();
//next is notification code. //
//get res.
SharedPreferences mPrefs = context.getSharedPreferences("appsettings", 0);
String titleText = mPrefs.getString("titleText", "");
String bigText = mPrefs.getString("bigText", "");
int NOTIFY_ID = mPrefs.getInt("id", 0);
//create intent.
Intent notificationIntent = new Intent(context, MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(context,
0, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);
//get res.
Resources res = context.getResources();
//build notification.
Notification.Builder builder = new Notification.Builder(context)
.setContentIntent(contentIntent)
.setSmallIcon(R.drawable.statusbaricon)
.setAutoCancel(true)
.setContentTitle(titleText)
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setContentText(bigText);
//check vibration.
if (mPrefs.getBoolean("vibration", true)) {
builder.setVibrate(new long[] { 0, 50 });
}
//create default title if empty.
if (titleText.length() == 0) {
builder.setContentTitle(context.getString(R.string.notification_Title_Default));
}
//show notification. check for delay.
builder.setWhen(System.currentTimeMillis());
Notification notification = new Notification.BigTextStyle(builder)
.bigText(bigText).build();
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(NOTIFY_ID, notification);
////
wakeLock.release();
}
public void setAlarm(Context context) {
SharedPreferences mPrefs = PreferenceManager.getDefaultSharedPreferences(context);
int delay = mPrefs.getInt("delay", 0);
int id = mPrefs.getInt("id", 0);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, Schedule.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, id, intent, PendingIntent.FLAG_ONE_SHOT);
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() /* + 1000 * 60 * delay */, pendingIntent);
}
}
这就是我所说的:
//store stuff to revoke in Schedule.
mPrefsEditor.putString("bigText", bigText).apply();
mPrefsEditor.putString("titleText", titleText).apply();
Schedule schedule = new Schedule();
schedule.setAlarm(context);
答案 0 :(得分:2)
对不同的requestCode
使用不同的PendingIntent
并使用标记FLAG_UPDATE_CURRENT
。
PendingIntent contentIntent = PendingIntent.getActivity(context, requestCode,
notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
1。从setAlarm()
方法发送通知id
,titleText
和bigText
广播receiver
到{{1}并使用标记intent
代替FLAG_UPDATE_CURRENT
。
更新FLAG_ONE_SHOT
方法,如下所示:
setAlarm()
2。在public void setAlarm(Context context) {
SharedPreferences mPrefs = PreferenceManager.getDefaultSharedPreferences(context);
int delay = mPrefs.getInt("delay", 0);
int id = mPrefs.getInt("id", 0);
String titleText = mPrefs.getString("titleText", "");
String bigText = mPrefs.getString("bigText", "");
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
// send notification id, titleText and bigText with intent
// to use later when alarm triggers
Intent intent = new Intent(context, Schedule.class);
intent.putExtra("NOTIFICATION_ID", id);
intent.putExtra("TITLE_TEXT", titleText);
intent.putExtra("BIG_TEXT", bigText);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, id, intent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() /* + 1000 * 60 * delay */, pendingIntent);
}
方法中,从onReceive()
获取通知id
,titleText
和bigText
。
3。使用[{1}}作为Intent
id
,并使用requestCode
和contentIntent
进行通知{{1} }。
更新titleText
方法,如下所示:
bigText
希望这会有所帮助〜
答案 1 :(得分:0)
这个PendingIntent.FLAG_CANCEL_CURRENT
是你的问题。
根据你想要做的事情,在你的代码中使用不同的标志,并按预期工作!
PendingIntent contentIntent = PendingIntent.getActivity(context,
0, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);
例如:FLAG_ACTIVITY_SINGLE_TOP,FLAG_ACTIVITY_CLEAR_TOP,FLAG_ACTIVITY_NEW_TASK,......或者根本不使用任何标志(0)。
PendingIntent.FLAG_ONE_SHOT
强制您的程序只使用pendingintent一次。请注意,使用不同setAlarm
调用putString
可能不会更改结果。
编辑:
将这一行放在Schedule.java
第33行和第34行。这会改变通知,但这不是一个好的解决方案,你应该用不同的变量字符串替换"something different" + bigText
。
String titleText = mPrefs.getString("titleText", "");
String bigText = mPrefs.getString("bigText", "");
SharedPreferences.Editor mPrefsTextsEditor = getSharedPreferences("notifications", 0).edit();
bigText = "something different" + bigText;
titleText = "something different" + titleText;
mPrefsTextsEditor .putString("bigText", bigText).apply();
mPrefsTextsEditor .putString("titleText", titleText).apply();