android - 通知未在 android 中启动 Activity

标签 android service android-activity notifications

我正在开发一个小应用程序,我希望后台服务在其中发送通知,当用户单击通知时,它必须启动相应的 Activity 。我在这里发布通知代码,我的问题是通知显示在状态栏上,但是当我单击它时它不会启动 Activity 。有人可以建议我哪里出错了。请帮忙。

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

最佳答案

替换 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

关于android - 通知未在 android 中启动 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11081414/

相关文章:

java - 无法在android studio中集成SignalR库

android - 从 Android 服务中的 TCP 套接字重置连接

java - 未连接充电器时 Android 速度变慢?

android - 开始 Activity 时出现意外延迟

Android - 如何修复 EditText 左侧的卢比符号字体图标

java - 如何根据特定条件在我的 RecyclerView 上隐藏某些项目的 ImageView?

android - 如何在本地的 Activity 和服务之间来回发送数据

java - 在 OSGi 中,传递服务引用是一种好的做法还是应该由 bundle 自行查找?

Android:带有参数的 Intent

Android Share intent,保持在同一个Activity中