问题与FCM通知 [英] Issue with FCM notification

查看:129
本文介绍了问题与FCM通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Firebase向我的应用发送推送通知,并希望将一些值保存到共享首选项。它的工作完美时,应用程序是前景,但它不保存应用程序在后台的值。
我的代码如下所示:

  @Override 
public void onMessageReceived(RemoteMessage remoteMessage){
Log.e(Remotemessage,remoteMessage.toString());

字符串标题= remoteMessage.getNotification()。getTitle();
String message = remoteMessage.getNotification()。getBody();
Map< String,String> data = remoteMessage.getData();
Log.e(Firebase通知,KEY1);
sessionManager = new SessionManager(this); (Firebase notification,KEY+ data.get(click_action));

Log.e
Log.e(Firebase notification,KEY new+ data.get(key));
int studentid = Integer.parseInt(data.get(student_id));
Log.e(学生ID,+ studentid);
if(studentid!= 0)
{
sessionManager.saveStudentId(studentid);





$ p $但是通知显示了这两种情况。如何解决这个问题?

解决方案

根据您的文章,我会假设您发送一个包含两个通知数据消息。有关详细信息,请参阅消息类型



可以在处理邮件对于Android的文档,当应用程序在后台发送包含消息类型的有效内容时,消息( notification data)将由Android处理系统托盘。



您可以做两件事:


  1. 发送一个 data - only 消息有效载荷,以便处理 onMessageReceived()无论您的应用程序处于前台还是后台,消息都是如此。

  2. 在用户点击Android系统托盘中的通知后,$ c>有效载荷。请参阅我的答案
    此处


I am using Firebase for sending push notification to my app and want to save some values to sharedpreferences. Its working perfectly when the app is foreground but it is not saving the values when the app is in background. My code looks like this:

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        Log.e("Remotemessage",remoteMessage.toString());

        String Heading = remoteMessage.getNotification().getTitle();
        String message = remoteMessage.getNotification().getBody();
        Map<String, String> data = remoteMessage.getData();
        Log.e("Firebase notification ", " KEY1");
        sessionManager=new SessionManager(this);

        Log.e("Firebase notification ", " KEY"+data.get("click_action"));
        Log.e("Firebase notification ", " KEY new"+data.get("key"));
        int studentid= Integer.parseInt(data.get("student_id"));
        Log.e("STUDENT ID",""+studentid);
        if(studentid!=0)
        {
            sessionManager.saveStudentId(studentid);

        } 
}

but notification is showing both cases. How to solve this issue??

解决方案

Basing from your post, I'll presume that you are sending a payload that contains both notification and data messages. See Message Types for more details.

As can be seen in the Handling Messages for Android documentation, when sending a payload that contains both message type while the the app is in background, the message (notification data) will be handled by the Android System Tray.

There are two things you could do:

  1. Send a data-only message payload, so that onMessageReceived() will be handling the message regardless if your app is in foreground or background.

  2. Implement the handling in getting the details in your data payload after the user taps on the notification in the Android system tray. See my answer here.

这篇关于问题与FCM通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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