![](/img/trans.png)
[英]Firebase pushing notification to chat members when a new message gets delivered without cloud functions
[英]Firebase push notification on new chat message
我創建了一個應用程序,我可以在其中聊天。
我希望用戶在離開應用程序時會收到一條通知,告知他們收到了一條新消息。
為此,我使用Firebase
服務。
現在,每當發送消息時,我都會調用此方法:我創建了以下MyFirebaseMessagingService
class 來測試它何時被調用:
private class MyTask extends AsyncTask<MyTaskParams, Void, Void> {
@Override
protected Void doInBackground(MyTaskParams... params) {
String userDeviceIdKey = params[0].url;
String title = params[0].title;
String body = params[0].body;
String authKey = "XXXXX"; // You FCM AUTH key
String FMCurl = "https://fcm.googleapis.com/fcm/send";
URL url;
try {
url = new URL(FMCurl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setUseCaches(false);
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Authorization","key="+authKey);
conn.setRequestProperty("Content-Type","application/json");
JSONObject json = new JSONObject();
json.put("to",userDeviceIdKey);
json.put("priority","high");
JSONObject info = new JSONObject();
info.put("title", title); // Notification title
info.put("body", body); // Notification body
info.put("var1", chatID);
info.put("var2",receiverID);
info.put("var3",itemID);
json.put("data", info);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(json.toString());
wr.flush();
Log.w("", "getInstanceId failed" + json);
conn.getInputStream();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
}
在我的服務中,我創建了:
public class MyFirebaseMessagingService extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.d("TESTING_MESSAGE", "" + remoteMessage);
}
}
但是,似乎我在發送消息時從未收到此Log
,而且似乎從未調用過onMessageReceived
。
有什么理由嗎?
我的下一個目標是在onMessageReceived
中發出通知,但我想首先確保我到達那里。
我的清單是:
<service
android:name=".service.MyFirebaseMessagingService"
android:permission="com.google.android.c2dm.permission.SEND">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
</intent-filter>
</service>
我用你的代碼制作了一個演示應用程序,它運行良好,我可以看到消息到達時觸發了 onMessageReceived function。
我使用 firebase-messaging 版本 20.1.7
implementation 'com.google.firebase:firebase-messaging:20.1.7'
所以我認為你應該做以下檢查:
檢查您的應用程序是否在前台。 因為您無法在應用程序處於后台時觸發 onMessageReceived。 參考: https://firebase.google.com/docs/cloud-messaging/android/receive#handling_messages
檢查您在發送 http 請求時是否寫入了正確的設備令牌(您的字段“userDeviceIdKey”)。
檢查您的 http 請求是否有捕獲錯誤。
希望能幫到你。
如果應用程序被終止,將永遠不會調用onMessageReceived()
方法。 Firebase 應用程序在后台時未調用 onMessageReceived
首先檢查您發送的內容:數據消息或通知消息。 它們可能看起來相同,但根據消息的類型,調用不同的方法。 檢查一下:https://firebase.google.com/support/faq/#fcm-android-background
// 檢查消息是否包含通知負載。
if (remoteMessage.getNotification() != null) {
Map test = remoteMessage.getData();
Log.d(TAG, "Message data payload: " + test);
// 檢查消息是否包含數據負載。
if (remoteMessage.getData().size() > 0) {
if (remoteMessage.getNotification() != null) {
Log.d(TAG, "Message Notification Body: " +
remoteMessage.getNotification().getBody());
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.