android - FCM 通知问题

标签 android firebase push-notification firebase-cloud-messaging

我正在使用 Firebase 向我的应用程序发送推送通知,并希望将一些值保存到 sharedpreferences。当应用程序处于前台时它可以完美运行,但当应用程序处于后台时它不会保存值。 我的代码如下所示:

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

        } 
}

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

最佳答案

根据您的帖子,我假设您发送的负载包含 notificationdata 消息。参见 Message Types了解更多详情。

Handling Messages for Android中可以看出文档,当应用程序在后台发送包含两种消息类型的负载时,消息(通知数据)将由 Android 系统托盘处理。

你可以做两件事:

  1. 发送一个data-only 消息有效负载,这样无论您的应用程序是在前景或背景中。

  2. 在用户点击 Android 系统托盘中的通知后,在您的 data 负载中执行获取详细信息的处理。看我的回答here .

关于android - FCM 通知问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43246549/

相关文章:

android - 以编程方式从 Google Play 开发者控制台下载 APK 文件

ios - 处理程序远程通知背景

javascript - firebase.firestore.FieldValue.arrayUnion 不是函数

java - Android: boolean 值变化的监听器

java - 根据 Activity 及其实例,AndroidManifest.xml 的真正目的是什么?

android - 如何更改工具栏选项背景的颜色

Firebase CLI : "functions: WARNING! NO ENGINES FIELD FOUND IN PACKAGE.JSON. DEFAULTING TO NODE 6 RUNTIME."

ios - iOS 推送通知中的多行标题

iphone - 如何管理用户单击徽章时的通知

ios - 在新 Mac 上推送通知证书