MyFirebaseMessagingService is not working but notification is shown

天大地大妈咪最大 提交于 2020-01-05 07:23:08

问题


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

    }
}

回答1:


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:

  • 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.



来源:https://stackoverflow.com/questions/47287828/myfirebasemessagingservice-is-not-working-but-notification-is-shown

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!