android 如何在用户点击通知时启动活动?

nAk*_*dov 4 notifications android android-pendingintent

我想在用户单击通知时打开活动。我知道这个问题是重复的,但找不到解决方案,这就是我所做的

NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle("My notification")
.setContentText("Hello World!");

Intent resultIntent = new Intent(this, ResultActivity.class);

// Because clicking the notification opens a new ("special") activity, there's
// no need to create an artificial back stack.
PendingIntent resultPendingIntent =
    PendingIntent.getActivity(
    this,
    0,
    resultIntent,
    PendingIntent.FLAG_UPDATE_CURRENT
);

mBuilder.setContentIntent(resultPendingIntent);
// Sets an ID for the notification
int mNotificationId = 001;
// Gets an instance of the NotificationManager service
NotificationManager mNotifyMgr = 
    (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// Builds the notification and issues it.
mNotifyMgr.notify(mNotificationId, mBuilder.build());
Run Code Online (Sandbox Code Playgroud)

这是我遵循的文档。有人有什么主意吗?为什么 ResultActivity 无法打开?

Mr.*_*ndy 5

请参阅此参考链接获取解决方案

单击通知后开始活动

单击“通知”后打开应用程序

单击“通知”未启动预期活动?

所有这些都包含单击通知时打开活动的可接受的解决方案。

因此,请正确参考并获得结果,解决您现在出现的问题。

感谢此问题块所有者以及已接受的答案提供者。因为它对于许多面临此类问题的开发人员很有用