在我的应用程序中,GCM消息最初显示在通知栏中。点击该通知,我的应用程序活动将显示通过GCM收到的消息。如果我正在阅读我的应用程序活动中的GCM消息,我收到另一条GCM消息,该消息将首先显示在通知区域。如果我点击新通知,它应该启动我的应用程序的预期活动并将数据传递给它并销毁自己。但是不是这种行为,而是什么?发生的事情是:如果我已经在阅读GCM消息并点击我刚刚收到的新通知,它只会显示已经运行的旧消息活动,也不会破坏通知。
我处理通知的代码是:
private void sendNotification(String msg) {
SharedPreferences prefs = getSharedPreferences(
DataAccessServer.PREFS_NAME, MODE_PRIVATE);
mNotificationManager = (NotificationManager) this
.getSystemService(Context.NOTIFICATION_SERVICE);
Intent intent = new Intent(this, WarningDetails.class);
Bundle bundle = new Bundle();
bundle.putString("warning", msg);
bundle.putInt("warningId", NOTIFICATION_ID);
intent.putExtras(bundle);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
this).setSmallIcon(R.drawable.weather_alert_notification)
.setContentTitle("Weather Notification")
.setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
.setContentText(msg);
String selectedSound = prefs.getString("selectedSound", "");
if (!selectedSound.equals("")) {
Uri alarmSound = Uri.parse(selectedSound);
mBuilder.setSound(alarmSound);
} else {
Uri alarmSound = RingtoneManager
.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
mBuilder.setSound(alarmSound);
}
if (prefs.getBoolean("isVibrateOn", false)) {
long[] pattern = { 500, 500, 500, 500, 500, 500, 500, 500, 500 };
mBuilder.setVibrate(pattern);
}
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}
WarningDetails活动代码是:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.warning_details);
warning = (TextView) findViewById(R.id.warningDetails);
Bundle extras = getIntent().getExtras();
if (extras != null) {
String warningData = extras.getString("warning");
//if (extras.getInt("warningId") != 0) {
NotificationManager mNotificationManager = (NotificationManager) this
.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.cancel(1);
//}
if (warningData != null) {
warning.setText(warningData);
} else {
// Do nothing
}
} else {
// Do nothing
}
}
我需要显示新的通知数据并销毁通知。请帮助
答案 0 :(得分:0)
要在点击通知时取消(销毁)通知,您应该将.setAutoCancel(true)
添加到NotificationCompat.Builder
。
要使用新通知中的数据更新您的活动,您必须将逻辑从onCreate
移至onResume
,因为创建时仅在创建活动时调用,并且活动已经当您点按新通知时,会使用现有活动。