我正在使用 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);
}
}
但通知显示了这两种情况。如何解决这个问题??
最佳答案
根据您的帖子,我假设您发送的负载包含 notification
和 data
消息。参见 Message Types了解更多详情。
在Handling Messages for Android中可以看出文档,当应用程序在后台发送包含两种消息类型的负载时,消息(通知
数据)将由 Android 系统托盘处理。
你可以做两件事:
发送一个
data
-only 消息有效负载,这样无论您的应用程序是在前景或背景中。在用户点击 Android 系统托盘中的通知后,在您的
data
负载中执行获取详细信息的处理。看我的回答here .
关于android - FCM 通知问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43246549/