【问题标题】:Push Notification with action button is not working带有操作按钮的推送通知不起作用
【发布时间】:2017-01-28 06:48:47
【问题描述】:

我已经使用 fcm 进行通知并且它正确出现,但是当我尝试在通知中添加添加按钮时它没有显示。

我的代码

   Intent intent = new Intent(this, MainActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
            PendingIntent.FLAG_ONE_SHOT);

NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle("New Message From Film")
            .setContentText(messageBody)
            .setAutoCancel(true)
             .addAction(R.drawable.pp, "Accept",pendingIntent) 
            .addAction(R.drawable.pp, "Reject",pendingIntent)  
            .setContentIntent(pendingIntent);
  NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    notificationManager.notify(0, notificationBuilder.build());

【问题讨论】:

  • 我们可以看到pendingIntent 代码吗?
  • 我对其进行了测试,它在 lolypop 和 pre lolypop 设备上运行良好!如果可能,添加更多代码。
  • @pRaNaY 你确定我更新了代码请检查
  • @Seyyed 在 marashmellow 中测试通知即将到来,但按钮未显示
  • 在您的案例中应该有 3 种不同的意图,一种用于通知意图,第二种用于操作接受,第三种用于操作拒绝。您不应该对所有 3 个都使用相同的待处理意图。参考这个stackoverflow.com/a/15521314/1576416

标签: android firebase notifications android-notifications


【解决方案1】:

你忘记在最后构建使用方法build()

修改后的代码

建造者

NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
        .setSmallIcon(R.mipmap.ic_launcher)
        .setContentTitle("New Message From Film")
        .setContentText(messageBody)
        .setAutoCancel(true)
         .addAction(R.drawable.pp, "Accept",pendingIntent) 
        .addAction(R.drawable.pp, "Reject",pendingIntent)  
        .setContentIntent(pendingIntent);

通知

final Notification notification = notificationBuilder.build();
mNotificationManager.notify(notificationId, notification);

【讨论】:

  • 检查已编辑的答案。它显示,但您必须展开通知
猜你喜欢
  • 1970-01-01
  • 2016-08-18
  • 1970-01-01
  • 2021-10-22
  • 1970-01-01
  • 2018-08-20
  • 2012-06-16
  • 2023-03-10
  • 1970-01-01
相关资源
最近更新 更多