android 如何在用户单击通知时启动 Activity ?

标签 android notifications android-pendingintent

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

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());

这里是 documentation我跟着。有人有什么主意吗?为什么 ResultActivity 打不开?

最佳答案

引用此引用链接获取解决方案

Start activity once notification clicked

Open application after clicking on Notification

Clicking on Notification is not starting intended activity?

所有这些都包含在点击通知时打开 Activity 的可接受解决方案。

因此,请正确引用它并获得解决您现在发生的问题的结果。

感谢这个问题 block 所有者接受的答案提供者。因为它对许多面临此类问题的开发人员很有用

关于android 如何在用户单击通知时启动 Activity ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31428325/

相关文章:

android - 如何从我的应用程序将数据共享到社交网站

iOS:我可以从表格 View 注册 textfieldDidBeginEditing 吗?

安卓工作室 : No build variant found error

Java 安卓工作室 : change an attribute of a View inside its own listener

javascript - Chrome 扩展程序 : Clicking notification opens multiple tabs

javascript - 显示 HTML5 通知,无需调用按钮

android - 为什么服务不工作 "immediately"?

android - 在 android N 中打开 PDF 文件在空白页中显示黑色

android - AlarmManager 没有启动 Activity(当它存在时)

带有希伯来语文本的 TextView 的 Android 设置?