首页 > 解决方案 > 通过通知启动应用程序后通知停止工作

问题描述

情况
用户收到来自我的应用程序的可点击通知。如果单击它,它将启动应用程序并根据它附带的数据将用户重定向到特定片段。这在应用程序运行时工作得很好。
在安卓 7 上运行

问题
如果应用程序没有运行,然后由通知启动,则启动和重定向工作正常。但是,如果在应用程序仍在运行时收到另一个通知,它就不再工作了。通知提供的唯一功能是关闭通知抽屉。在被点击后它甚至不再消失,就像它应该的那样。Intent 中定义的 Activity 的 onCreate 或 onResume 也不会被调用。它似乎只是忽略其所有设置并在单击后关闭导航抽屉

通知代码

private void showNotification(String message, PushEntity.ActionEnum actionEnum, Map<String, String> params) {
 NotificationManager mNotificationManager = (NotificationManager)this.getSystemService(Context.NOTIFICATION_SERVICE);
 Intent startIntent = new Intent(getApplicationContext(), MainActivity.class);
 if (params != null) {
    for (Map.Entry<String, String> entry : params.entrySet()) {
        startIntent.putExtra(entry.getKey(), entry.getValue());
    }
 }
 PendingIntent contentIntent = PendingIntent.getActivity(this, 0, startIntent, PendingIntent.FLAG_ONE_SHOT);
 NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
 .setSmallIcon(R.drawable.ic_default_notification)
 .setContentTitle(getResources().getString(R.string.app_name))
 .setStyle(new NotificationCompat.BigTextStyle().bigText(message))
 .setContentText(message)
 .setDefaults(Notification.DEFAULT_ALL)
 .setPriority(NotificationCompat.PRIORITY_MAX);
 mBuilder.setContentIntent(contentIntent);
 mNotificationManager.notify(pushNotificationId, mBuilder.build());

到目前为止我尝试了什么

问题
谁能向我解释为什么这种行为只发生在应用程序由通知启动之后而不是在它已经运行时发生?
有谁知道如何解决这个问题?

标签: androidpush-notification

解决方案


推荐阅读