当应用程序在后台运行时,Android Firebase通知单击不起作用

时间:2019-10-04 13:32:30

标签: android

仅在响应中获取消息类型“数据”。但是应用未在收到通知时调用onMessageReceive方法。

{
    "data" : {
      "body" : "Body of the notification example",
      "action_id" : "4"
    }
  }
}

1 个答案:

答案 0 :(得分:0)

您做了这样的事情吗?这是用于处理数据消息的。

public class FirebaseMessaging extends FirebaseMessagingService {

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

private void showNotification(Map<String, String> data) {
    String title = data.get("title");
    String body = data.get("body");

    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    String NOTIFICATION_CHANNEL_ID = "ChannelID";

    if(notificationManager != null) {
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel notificationChannel = new NotificationChannel(
                    NOTIFICATION_CHANNEL_ID, "NotificationManager",
                    NotificationManager.IMPORTANCE_DEFAULT);

              notificationChannel.setDescription("My Channel");
              notificationManager.createNotificationChannel(notificationChannel);
        }

        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);
        notificationBuilder
                .setAutoCancel(true)
                .setDefaults(Notification.DEFAULT_ALL)
                .setWhen(System.currentTimeMillis())
                .setSmallIcon(R.drawable.supportop_icon)
                .setContentTitle(title)
                .setContentText(body);

        // Handle notification click
        PendingIntent intent = PendingIntent.getActivity(this, 0, new Intent(this, Activity.class), 0);
        notificationBuilder.setContentIntent(intent);

        notificationManager.notify(id, notificationBuilder.build());
    }
}
}