@Override
public void onMessageReceived(RemoteMessage remoteMessage)
{
if (remoteMessage.getData().size() > 0)
{
Log.e(TAG, "Message body:" + remoteMessage.getNotification().getBody());
for (Map.Entry<String, String> entry : remoteMessage.getData().entrySet())
{
String key = entry.getKey();
String value = entry.getValue();
data.add(value);
Log.e(TAG, "key, " + key + " value " + value);
}
post_id = data.get(0);
lang = data.get(1);
link = data.get(2);
Log.e("post_id",post_id);
Log.e("lang",lang);
Log.e("link",link);
}
sendNotification(remoteMessage.getNotification().getBody());
}
当应用程序处于后台时,此方法永远不会调用,因此当应用程序处于后台时,我无法接收任何数据
任何帮助将不胜感激...
答案 0 :(得分:1)
您应该在服务中实现onMessageReceived()。因此,当您的应用程序在后台运行时,它仍然会收到正常的回调。 Firebase支持所有这些。
在AndroidManifest.xml中声明
<service android:name=".services.MyFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
在Java代码中:
public class MyFirebaseMessagingService extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
//do receive data
}
答案 1 :(得分:0)
@Override
public void onMessageReceived(RemoteMessage remoteMessage)
{
super.onMessageReceived(remoteMessage);
if (remoteMessage.getData() != null)
sendNotification(remoteMessage);
}
答案 2 :(得分:0)
发送数据时,添加以下两个属性meteor shell
例如
'ttl' => 3600, 'content_available' => true
这曾经对我有帮助。
那么这应该工作
return Curl::to('https://fcm.googleapis.com/fcm/send')
->withHeader('Authorization: key=' . env('FIREBASE_KEY'))
->withHeader('Content-Type: application/json')
->withData(['to' => $firebase_token, 'data' => $data, 'ttl' => 3600, 'content_available' => true ])
->asJson(true)
->post();
答案 3 :(得分:0)
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.d(TAG, "From: " + remoteMessage.getFrom());
// Check if message contains a data payload.
if (remoteMessage.getData().size() > 0) {
Log.d(TAG, "Message data payload: " + remoteMessage.getData());
Log.e(TAG, "Data Payload: " + remoteMessage.getData().toString());
try {
JSONObject json = new JSONObject(remoteMessage.getData().toString());
handleDataMessage(json);
} catch (Exception e) {
Log.e(TAG, "Exception: " + e.getMessage());
}
}
// Check if message contains a notification payload.
if (remoteMessage.getNotification() != null) {
Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
}
// Also if you intend on generating your own notifications as a result of a received FCM
// message, here is where that should be initiated. See sendNotification method below.
}
private void handleDataMessage(JSONObject json) {
Log.e(TAG, "push json: " + json.toString());
String message = "";
String notification_type = "";
String name = "";
int id = 0;
String title = "";
JSONObject postData = null;
int notify_id = 0;
try {
JSONObject data = json.getJSONObject("notification");
message = data.getString("body");
if (data.getJSONObject("obj") != null) {
postData = data.getJSONObject("obj");
message = postData.getString("message");
notification_type = postData.getString("notification_type");
id = postData.getJSONObject("response").getInt("id");
name = postData.getJSONObject("response").getString("name");
}
title = data.getString("title");
Log.e(TAG, "title: " + title);
Log.e(TAG, "postData: " + postData);
} catch (JSONException e) {
Log.e(TAG, "Json Exception: " + e.getMessage());
} catch (Exception e) {
Log.e(TAG, "Exception: " + e.getMessage());
}
}