通知中的待定意图无法工作

48

以下是我的代码块,当通知被点击时应该打开NotificationActivity,但它无法工作。

private void setNotification(String notificationMessage) {
    Uri alarmSound = getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    mNotificationManager  = getApplication().getSystemService(Context.NOTIFICATION_SERVICE);

    Intent notificationIntent = new Intent(getApplicationContext(), NotificationActivity2.class);

    PendingIntent contentIntent = PendingIntent.getActivity(this, 0,notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getApplicationContext())
    .setSmallIcon(R.drawable.logo)
    .setContentTitle("My Notification")
    .setStyle(new NotificationCompat.BigTextStyle()
    .bigText(notificationMessage))
    .setContentText(notificationMessage).setAutoCancel(true);
    mBuilder.setSound(alarmSound);
    mBuilder.setContentIntent(contentIntent);
    mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());

}

2
你有尝试使用当前时间而不是 NOTIFICATION_ID 吗? - Piyush
3
你的清单文件中是否有“NotificationActivity2”? - David Wasser
4
添加系统时间作为ID解决了问题。谢谢Piyush...以及所有尝试帮助我的人 :) - prashantwosti
6个回答

89

试试这个:

private void setNotification(String notificationMessage) {

//**add this line**
int requestID = (int) System.currentTimeMillis();

Uri alarmSound = getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
mNotificationManager  = getApplication().getSystemService(Context.NOTIFICATION_SERVICE);

Intent notificationIntent = new Intent(getApplicationContext(), NotificationActivity2.class);

//**add this line**
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); 

//**edit this line to put requestID as requestCode**
PendingIntent contentIntent = PendingIntent.getActivity(this, requestID,notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getApplicationContext())
.setSmallIcon(R.drawable.logo)
.setContentTitle("My Notification")
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(notificationMessage))
.setContentText(notificationMessage).setAutoCancel(true);
mBuilder.setSound(alarmSound);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());

}

14
你能给出一个理由说明为什么这个方法有效吗?这说法非常模糊。 - Elliott
确实很奇怪。我可以验证这个解决方案是有效的。谢谢droidx。 - kha
6
对我来说也起作用了。只需要传递一个不为零的requestCode就可以了。我只是添加了标志:intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); - voghDev
1
感谢 Stack Overflow 的众神! - Lara Ruffle Coles
确认这也对我有帮助。我解决了一个类似的问题,使用运行4.4.2的三星Galaxy S3,通过将第二个参数(requestCode)从0更改为1的PendingIntent.getService。 - mbafford
显示剩余3条评论

13

你可能正在使用等于0的通知标识符。已知通知标识符为0存在问题。如果你使用任何其他标识符,它应该可以正常工作。


我不明白,应该是0还是不是?你说“必须是0”和“任何其他ID都可以使用”? - Buddy
我猜问题在于他一定使用了“0”,这导致了问题。它不应该是0,而应该是任何其他的id。 - pratsJ

7
我添加了任务构建器,以下块代码适用于我。
Intent intent = new Intent(context, HomeActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addParentStack(SplashActivity.class);
stackBuilder.addNextIntent(intent);
PendingIntent pendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

我正在做同样的事情,而且它有效。但是 - 如果我想要有两个不同的操作怎么办?即内容操作和按钮操作。如何创建第二个待处理意图? - Micer
@Micer 你可以分别创建它们并在相应的方法中进行分配。 - rahulrvp

3

尝试检查你的 AndroidManifest.xml 文件,仔细检查是否正在导航到带有 intent-filter main action, launcher category 的活动。

比如说,

enter image description here

我无法使我的“挂起意图”导航到“HomeActivity”,但是当导航到“SplashScreen”时它可以工作。
希望这有所帮助。

这有所帮助,但如果您确实想要导航到“HomeActivity”,我不确定该如何解决。我们应该将该活动添加到其中一个过滤器中吗?还是指定一个操作?或者在意图过滤器中添加一个操作? - Bouke Versteegh
巧合的是,我发现我的“活动”没有列在此列表中,因为它只是另一个活动的子视图。这最终是问题所在。谢谢 :-) - Bouke Versteegh

2
如果应用程序已经在运行,则需要在onNewIntent中处理它。
@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);

    //Handle intent here...
}

使用 setIntent() 更新意图值。 - Eldhopj

2

已添加请求ID,但意图仍未打开。

这是对我有效的解决方案:

intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);

将这些标志设置为清除意图活动下面的活动。

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