android - 通过通知启动应用程序后通知停止工作
问题描述
情况
用户收到来自我的应用程序的可点击通知。如果单击它,它将启动应用程序并根据它附带的数据将用户重定向到特定片段。这在应用程序运行时工作得很好。
在安卓 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());
到目前为止我尝试了什么
- 在我的 AndroidManifest.xml 中添加
android:exported="true"
MainActivity 的条目 - 添加标志
PendingIntent.FLAG_UPDATE_CURRENT
而不是和另外FLAG_ONE_SHOT
问题
谁能向我解释为什么这种行为只发生在应用程序由通知启动之后而不是在它已经运行时发生?
有谁知道如何解决这个问题?
解决方案
推荐阅读
- php - Laravel 5.5 应用程序不再以“尚未设置外观根目录”启动。
- python - 偶数和奇数之和包括超出范围的数字,如果不是偶数或奇数
- typescript - 类型 'T' 不能分配给类型 'T extends T ?T : T' 在 TypeScript 中
- react-native - 反应本机响应问题
- hybris - Hybris 30 天许可证已过期
- azure - 对 Azure ExpressRoute 合作伙伴一词感到困惑
- python - nltk-hook 无法找到 nltk_data
- tsql - 如何在单行上对齐 SSRS 中的每日时间线?
- javascript - 等待跳过以及如何解决?
- json - 如何使用 Spring WebClient 按名称获取 json 字段?