【发布时间】:2019-11-02 21:09:46
【问题描述】:
我正在用我的应用测试推送通知。
当App在前台时:
Step 1. Received the notification (in system tray).
2. now, I'm in some other screen than the home screen.
3. Actual Problem: On tap on the notification, it is going to the home screen.
4. Expected: If the app is in the foreground, just I need to cancel on tap of the notification. (No need to swipe.)
当应用程序在后台/被杀死时:(运行良好)
Step 1. Received the notification (in the system tray)
2. On tap, open the home screen of the app.
尝试在意图中设置启动模式标志。没有帮助。下面是我的代码。请各位大侠提出解决方案。
Intent resultIntent = new Intent(this, MainActivity.class);
//resultIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
// resultIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent resultPendingIntent =
PendingIntent.getActivity(
this,
0,
resultIntent,
PendingIntent.FLAG_UPDATE_CURRENT
);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this);
mBuilder.setContentIntent(resultPendingIntent);
mBuilder.setSmallIcon(R.mipmap.ic_launcher);
mBuilder.setContentTitle(title);
mBuilder.setContentText(body);
mBuilder.setAutoCancel(true);
mBuilder.setStyle(new NotificationCompat.BigTextStyle().bigText(body));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
mBuilder.setChannelId(TestUtils.creatChanel(this).getId());
}
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(642, mBuilder.build());
【问题讨论】:
-
您想一直取消通知吗?应用在前台时取消的目的是什么?
-
没有。我不想总是取消通知。通知的目的不应影响应用导航。如果我在屏幕 3 中并且我点击通知,它现在会将我带到主屏幕。
-
如果导航不应该影响应用导航,那么您必须在应用中取消所有通知。
标签: android android-intent push-notification notifications android-pendingintent