Firebase推送通知无需单击即可打开活动

时间:2018-06-01 18:20:10

标签: android firebase push-notification android-pendingintent

我遇到firebase推送通知问题。 它不会显示通知,而是自动打开我放置的活动。我想要这种行为,但点击通知后。

这是我的代码:

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    super.onMessageReceived(remoteMessage);

    final String title = remoteMessage.getData().get("title");
    final String message = remoteMessage.getData().get("body");

    showNotifications(title, message);
}

private void showNotifications(String title, String msg) {
    Intent i = new Intent(this, OrderDetailActivity.class);
    i.putExtra("order_id", 99);
    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
            i, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_CANCEL_CURRENT | PendingIntent.FLAG_ONE_SHOT);

    Notification notification = new NotificationCompat.Builder(this)
            .setContentText(msg)
            .setContentTitle(title)
            .setContentIntent(pendingIntent)
            .setSmallIcon(R.mipmap.ic_launcher_logo)
            .build();

    NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    manager.notify(NOTIFICATION_ID, notification);

    try {
        pendingIntent.send();
    } catch (PendingIntent.CanceledException e) {
        e.printStackTrace();
    }
}

1 个答案:

答案 0 :(得分:1)

快速浏览一下,我想知道你为什么需要pendingIntent.send();?来自send()上的文档:

  

执行与此PendingIntent相关联的操作。

这就是为什么它可能会执行附加的Intent。尝试删除该行。有关详情,请参阅Create a Notification