我在确定通知的意图, PendingIntent 过滤器和标志方面遇到了麻烦。
通知正在运行并且正在按原样生成,但问题是只有最后创建的通知会保留捆绑数据。
我希望所有通知都保留每个通知的捆绑数据,直到用户点击它们为止。
考虑一个应用程序片刻,当不同的用户向您发送消息时,会创建新的通知,当您单击任何通知时,应用程序将启动并带您进入某个特定的活动。我想要同样的事情,但是当有多个通知时,最后一个通知会保留数据,因为之前的通知会放弃捆绑数据和意图 。
还有一个过滤器用于限制应用在每次点击通知时启动 MainActivity 的新实例。
Notification_ID 对于每个通知都不同。
public class AlarmSchedulingService extends IntentService {
private NotificationManager mNotificationManager;
public AlarmSchedulingService() {
super("SchedulingService");
}
protected void onHandleIntent(Intent intent) {
Bundle extras = intent.getExtras();
sendNotification(extras.getInt(KEY_EXTRAS_NOTIFICATION_ID));
}
public void sendNotification(int NOTIFICATION_ID) {
mNotificationManager = (NotificationManager) this
.getSystemService(Context.NOTIFICATION_SERVICE);
Intent intent = new Intent(this, MainActivity.class);
intent.putExtra(keyName, extraData);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
// use the right class it should be called from the where alarms are set
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
this)
.setAutoCancel(true)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(titleString)
.setStyle(
new NotificationCompat.BigTextStyle()
.bigText(messageString))
.setDefaults(
Notification.DEFAULT_SOUND
| Notification.DEFAULT_LIGHTS)
.setContentText(messageString);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}
}
答案 0 :(得分:7)
这表明:
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
您正在向所有通知提供请求代码0。 应该用每个唯一编号
替换0