Android: AlarmManager 一直显示相同的通知
Android: AlarmManager shows same notifications all the time
我用不同的文本创建了多个通知。但是,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);
这个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。请注意,使用不同的 putString
调用 setAlarm
可能不会改变结果。
编辑:
将此行放在 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();
对带有标记 FLAG_UPDATE_CURRENT
的不同 PendingIntent
使用不同的 requestCode
。
PendingIntent contentIntent = PendingIntent.getActivity(context, requestCode,
notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
1.从setAlarm()
方法发送通知id
、titleText
和bigText
广播receiver
通过 intent
并使用标志 FLAG_UPDATE_CURRENT
而不是 FLAG_ONE_SHOT
.
更新setAlarm()
方法如下:
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);
}
2. 在您的 onReceive()
方法中,从 [=29= 获取通知 id
、titleText
和 bigText
].
3. 使用通知 id
作为 contentIntent
的 requestCode
并使用 titleText
和 bigText
通知 text
.
更新 onReceive()
方法如下:
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();
// get id, titleText and bigText from intent
int NOTIFY_ID = intent.getIntExtra("NOTIFICATION_ID", 0);
String titleText = intent.getStringExtra("TITLE_TEXT");
String bigText = intent.getStringExtra("BIG_TEXT");
// Create intent.
Intent notificationIntent = new Intent(context, MainActivity.class);
// use NOTIFY_ID as requestCode
PendingIntent contentIntent = PendingIntent.getActivity(context,
NOTIFY_ID, notificationIntent, PendingIntent.FLAG_UPDATE_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();
}
....................
..........................
}
希望对您有所帮助~
我用不同的文本创建了多个通知。但是,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);
这个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。请注意,使用不同的 putString
调用 setAlarm
可能不会改变结果。
编辑:
将此行放在 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();
对带有标记 FLAG_UPDATE_CURRENT
的不同 PendingIntent
使用不同的 requestCode
。
PendingIntent contentIntent = PendingIntent.getActivity(context, requestCode,
notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
1.从setAlarm()
方法发送通知id
、titleText
和bigText
广播receiver
通过 intent
并使用标志 FLAG_UPDATE_CURRENT
而不是 FLAG_ONE_SHOT
.
更新setAlarm()
方法如下:
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);
}
2. 在您的 onReceive()
方法中,从 [=29= 获取通知 id
、titleText
和 bigText
].
3. 使用通知 id
作为 contentIntent
的 requestCode
并使用 titleText
和 bigText
通知 text
.
更新 onReceive()
方法如下:
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();
// get id, titleText and bigText from intent
int NOTIFY_ID = intent.getIntExtra("NOTIFICATION_ID", 0);
String titleText = intent.getStringExtra("TITLE_TEXT");
String bigText = intent.getStringExtra("BIG_TEXT");
// Create intent.
Intent notificationIntent = new Intent(context, MainActivity.class);
// use NOTIFY_ID as requestCode
PendingIntent contentIntent = PendingIntent.getActivity(context,
NOTIFY_ID, notificationIntent, PendingIntent.FLAG_UPDATE_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();
}
....................
..........................
}
希望对您有所帮助~