【问题标题】:Pending intent in notification not working通知中的待处理意图不起作用
【发布时间】:2014-01-19 19:11:00
【问题描述】:

下面是我的代码块,当点击通知时应该打开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());

}

【问题讨论】:

  • 您是否检查了当前时间而不是 NOTIFICATION_ID?
  • 你的清单中有NotificationActivity2吗?
  • 通过添加系统时间作为 ID 解决。感谢 piyush.. 以及所有其他试图帮助我的人 :)

标签: android android-pendingintent


【解决方案1】:

试试这个:

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

}

【讨论】:

  • 你能给出一个为什么这样有效的原因吗......这非常模糊。
  • 确实很奇怪。我可以验证此解决方案是否有效。谢谢 droidx。
  • 也为我工作。只需传递一个不同于零的 requestCode 即可完成这项工作。我只是添加标志:intent.addFlags (Intent.FLAG_ACTIVITY_NEW_TASK);
  • 感谢stackoverflow大神!
  • 确认这对我也有帮助。我通过将第二个参数 (requestCode) 更改为 PendingIntent.getService 从 0 到 1 解决了运行 4.4.2 的三星 Galaxy S3 的类似问题。
【解决方案2】:

您可能正在使用等于 0 的通知 id。通知 id 为 0 存在已知问题。如果您将使用任何其他 id,它应该可以工作。

【讨论】:

  • 没看懂,是不是应该是0?您是说“必须为 0”和“任何其他 id 都应该有效”??
  • 我猜测问题是他必须使用导致问题的'0'。它不应该是 0,而是任何其他 id
【解决方案3】:

我添加了任务生成器,下面的代码块对我有用

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

【讨论】:

  • 我也在做同样的事情,它可以工作。但是 - 如果我想要两个不同的动作怎么办? IE。内容动作和按钮动作。如何创建第二个待处理意图?
  • @Micer 你可以单独创建它们并在各自的方法中分配它们。
【解决方案4】:

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

例如,

我无法让我的 pending intent 导航到 HomeActivity,但导航到 SplashScreen 时它可以工作

希望这会有所帮助。

【讨论】:

  • 它有帮助,但如果您确实想要导航到“HomeActivity”,我不确定如何解决这个问题。我们应该将活动添加到过滤器之一吗?或者指定一个动作?或者向意图过滤器添加一个操作?
  • 巧合的是,我的“活动”并未列在此列表中,因为它只是另一个活动的子视图。这是最后的问题。谢谢:-)
【解决方案5】:

如果应用已经在运行,那么你需要在onNewIntent处理它

@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);

    //Handle intent here...
}

【讨论】:

  • 使用 setIntent() 更新意图值setIntent(intent);
【解决方案6】:

添加了requestid,但intent仍未打开。

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

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

设置这些标志以清除意图活动下方的活动。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多