Android中通知无法启动活动

3

我正在开发一个小应用程序,其中我希望后台服务发送通知,并且当用户单击通知时,它必须启动相应的活动。我在此处发布了通知的代码,我的问题是通知显示在状态栏上,但当我单击它时,它无法启动活动。有人能建议我错在哪里吗?请帮忙。

NotificationManager notificationManager = (NotificationManager) 
        getSystemService(NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.ic_launcher,
        "A New Message!", System.currentTimeMillis());
Intent notificationIntent = new Intent(NotificationService.this,
        com.jeris.android.MetroActivity.class);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingIntent = PendingIntent
        .getService(NotificationService.this, 0, notificationIntent,
                PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_ONE_SHOT);
notification.setLatestEventInfo(NotificationService.this, "Bullion",
        "Rates for "+ parsedDate+"has not been updated. Go to app to check rates",
        pendingIntent);
notificationManager.notify(11, notification);
1个回答

5

PendingIntent.getService(NotificationService.this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_ONE_SHOT);

替换为 PendingIntent.getActivity(NotificationService.this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_ONE_SHOT);

请注意getService已被替换为getActivity


我明白了...是我自己的一个愚蠢错误,谢谢你的帮助Arun...现在它完美地工作了。 - Jeris
在“通知内置服务未启动目标活动”的情况下,修改目标Activity的清单定义并在“activity”标签中添加*android:exported="true"*对我起了作用。 - Kerem
1
@mass,@Arun George 我在我的 FCM 服务中使用了这个,如果应用程序已经打开,则它会打开意图活动,否则它会使用 getIntent().getExtras() 打开启动活动,该值为 null。我尝试在意图活动上使用 android:exported="true",但没有起作用。有什么想法吗? - Mahdi Rostami

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接