【问题标题】:Issue with FCM notificationFCM 通知问题
【发布时间】:2017-04-06 05:38:47
【问题描述】:

我正在使用 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);

        } 
}

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

【问题讨论】:

  • 该文档还提到了当应用程序在后台时接收通知的以下内容:在这些情况下,通知被传递到设备的系统托盘,并且数据负载被传递到额外的启动器 Activity 的意图。
  • replace sessionManager=new SessionManager(this);sessionManager=new SessionManager(getBaseContext());
  • 从您的服务器发送通知时仅使用数据字段。
  • 感谢@RatilalChopda 成功了。
  • 欢迎...@Saneesh

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


【解决方案1】:

根据您的帖子,我假设您正在发送包含notificationdata 消息的有效负载。详情请见Message Types

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

你可以做两件事:

  1. 发送data-消息负载,以便onMessageReceived() 将处理该消息,无论您的应用是在前台还是后台。

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

【讨论】:

  • 如果用户不点击通知而只是清除通知怎么办?
  • @MayuraDevani 然后它不会继续进行活动。这本身就是用户的选择
  • 是的,您的信息是正确的,但在 @Saneness 的情况下,将数据保存在共享首选项中将不起作用
  • 没错。但问题是在后台没有收到 FCM 消息。
  • 在问题中,他说:当应用程序处于前台时它可以正常工作,但当应用程序处于后台时它不会保存值。所以他一定想把重要数据保存在共享偏好中。
猜你喜欢
  • 2017-01-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-07
  • 2022-06-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多