MyFirebaseMessagingService无法正常工作,但显示通知 [英] MyFirebaseMessagingService is not working but notification is shown

查看:622
本文介绍了MyFirebaseMessagingService无法正常工作,但显示通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从Firebase收到通知,但是我创建的班级无法正常工作(不参与操作).似乎默认情况下显示它们.

I receive notifications from Firebase but my classes I've created not working (not involved in the actions). Seems like they are shown by default.

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

    }
}

推荐答案

FCM消息有两种类型:通知和数据.在接收设备中对它们的处理方式有所不同.如果您发送的通知消息中没有数据字段,则该通知是由客户端SDK生成的,并且不会调用onMessageReceived().这是在文档中进行了详细说明:

There are two types of FCM messages: notification and data. They are handled differently in the receiving device. If you are sending a notification message with no data fields, the notification is generated by the client SDK and onMessageReceived() is not invoked. This is explained in more detail in the documentation:

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

  • Notification Message: FCM automatically displays the message to end-user devices on behalf of the client app. Notification messages have a predefined set of user-visible keys and an optional data payload of custom key-value pairs.

数据消息:客户端应用负责处理数据消息.数据消息仅具有自定义键值对.

Data Message: Client app is responsible for processing data messages. Data messages have only custom key-value pairs.

这篇关于MyFirebaseMessagingService无法正常工作,但显示通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆