java - 从通知的后台打开后,Android PendingIntent 未启动活动
问题描述
我正在使用 Google FCM 向我的应用发送通知。我目前正在发送数据通知并让我的 android 应用程序构建推送通知。我希望用户能够单击通知并将其路由到适当的片段/活动。
除了一个流程外,它工作得很好。用户终止应用程序->接收通知->点击通知->应用程序启动和数据从通知正确传递到启动活动(我正在使用通知活动来广播/启动意图)->用户在应用程序打开时收到另一个通知-> 用户单击通知 -> 之前正确启动的通知活动不会启动。
我正在挠头,试图弄清楚为什么会这样。我的猜测与上下文错误有关,因为应用程序是从通知中启动的,但我尝试同时使用“this”和“getApplicationContext()”,但都没有解决问题。
以下是我构建通知和待处理意图的方式。
Intent notificationIntent = new Intent(this, NotificationActivity.class);
notificationIntent.putExtra(Variables.INTENT_NOTIFICATION_DATA, "dummy_data");
notificationIntent.setAction(Variables.NOTIFICATION_CLICK);
PendingIntent contentIntent = PendingIntent.getActivity(this,
0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Notification notification = new NotificationCompat.Builder(this, Variables.CHANNEL)
.setContentTitle("New Notification")
.setContentText("Dummy text")
.setSmallIcon(R.drawable.notification_icon)
.setContentIntent(contentIntent)
.setAutoCancel(true)
.setOngoing(false)
.build();
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (mNotificationManager != null) {
mNotificationManager.notify(0, notification);
}
解决方案
推荐阅读
- python - 在 python 中处理 json.loads() 值错误
- c++ - 为什么在使用 prinft() 和 string::c_str() 打印输出时,程序中定义的 std::string 不等于从文件加载的 std::string?
- php - 通过单击 HTML div 带参数的 PHP 调用函数
- android - E/chromium:[错误:compositor_impl_android.cc(585)] 无法为合成器初始化 OutputSurface
- php - laravel 播种机给出错误并且不会播种
- xml - DITA OT 返回平面主题结构
- bots - discord.py(on_reaction_add)中的自定义表情符号不起作用
- google-apps-script - ISO 8601 UTC 时间的当前时间戳(即:2013-11-03T00:45:54+02:00)
- laravel - 如何使用 laravel 后端 API 在 nuxt.js 中制作图片上传表单
- python - Gurobi 与 Python(优化迭代/模拟)