FCM通知问题

时间:2017-04-06 05:38:47

标签: 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);

        } 
}

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

1 个答案:

答案 0 :(得分:1)

根据您的帖子,我假设您发送的有效内容包含notificationdata条消息。有关详细信息,请参阅Message Types

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

你可以做两件事:

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

  2. 在用户点按Android系统托盘中的通知后,实施处理以获取data有效内容中的详细信息。请参阅我的回答here

相关问题