【问题标题】:MyFirebaseMessagingService is not working but notification is shownMyFirebaseMessagingService 不工作,但显示通知
【发布时间】:2017-11-14 14:06:37
【问题描述】:

我收到来自 Firebase 的通知,但我创建的课程无法正常工作(未参与操作)。似乎它们默认显示。

public class MyFirebaseMessagingService extends FirebaseMessagingService {

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {

        String title = remoteMessage.getNotification().getTitle();
        String message = remoteMessage.getNotification().getBody();
        String click_action = remoteMessage.getNotification().getClickAction();
        Intent intent = new Intent(click_action);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);

        NotificationCompat.Builder notifiBuilder = new NotificationCompat.Builder(this)

                .setContentTitle(title)
                .setContentText(message)
                .setAutoCancel(true)
                .setContentIntent(pendingIntent);

        NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(0, notifiBuilder.build());

    }
}

【问题讨论】:

    标签: android firebase notifications onclick firebase-cloud-messaging


    【解决方案1】:

    有两种类型的 FCM 消息:通知和数据。它们在接收设备中的处理方式不同。如果您正在发送没有数据字段的通知消息,则通知由客户端 SDK 生成,并且不会调用onMessageReceived()。这是explained in more detail in the documentation

    • 通知消息:FCM 代表客户端应用程序自动向最终用户设备显示消息。通知消息 有一组预定义的用户可见键和可选数据 自定义键值对的有效负载。

    • 数据消息:客户端应用负责处理数据消息。数据消息只有自定义键值对。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-08-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多