[英]Firebase push notification on new chat message
I have created an app where I have chat in it.我创建了一个应用程序,我可以在其中聊天。
I would like that the users will receive a notification that they got a new message whenever they are outside of the app.我希望用户在离开应用程序时会收到一条通知,告知他们收到了一条新消息。
In order to do so, I'm using Firebase
service.为此,我使用
Firebase
服务。
Now, whenever a message is sent, I call this method: I have created the following MyFirebaseMessagingService
class to test when it is called:现在,每当发送消息时,我都会调用此方法:我创建了以下
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;
}
}
and In my Service I created:在我的服务中,我创建了:
public class MyFirebaseMessagingService extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.d("TESTING_MESSAGE", "" + remoteMessage);
}
}
However, It seems like I never get this Log
when I send a message and it doesnt look like onMessageReceived
is ever called.但是,似乎我在发送消息时从未收到此
Log
,而且似乎从未调用过onMessageReceived
。
Is there any reason?有什么理由吗?
My next goal is to make notification inside onMessageReceived
but I want to make sure first that I get there.我的下一个目标是在
onMessageReceived
中发出通知,但我想首先确保我到达那里。
My manifest is:我的清单是:
<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>
I make a demo app with your code, and it works well and I can see that onMessageReceived function is trigged when a message arrives.我用你的代码制作了一个演示应用程序,它运行良好,我可以看到消息到达时触发了 onMessageReceived function。
I use the firebase-messaging version 20.1.7我使用 firebase-messaging 版本 20.1.7
implementation 'com.google.firebase:firebase-messaging:20.1.7'
so I think you should do the following checking:所以我认为你应该做以下检查:
check if your app is in the foreground.检查您的应用程序是否在前台。 because you can't trigger onMessageReceived when the app is in the background.
因为您无法在应用程序处于后台时触发 onMessageReceived。 refer: https://firebase.google.com/docs/cloud-messaging/android/receive#handling_messages
参考: https://firebase.google.com/docs/cloud-messaging/android/receive#handling_messages
check if you wrote the right device token(your field 'userDeviceIdKey') when you sent the http request.检查您在发送 http 请求时是否写入了正确的设备令牌(您的字段“userDeviceIdKey”)。
check if your http request has some error in catch.检查您的 http 请求是否有捕获错误。
hope can help you.希望能帮到你。
The onMessageReceived()
method will never be called if the app is killed.如果应用程序被终止,将永远不会调用
onMessageReceived()
方法。 Firebase onMessageReceived not called when app in background Firebase 应用程序在后台时未调用 onMessageReceived
First of all check what you send: data message or notification message.首先检查您发送的内容:数据消息或通知消息。 They may look the same, but depending on what type of message, the different method called.
它们可能看起来相同,但根据消息的类型,调用不同的方法。 Check this out:https://firebase.google.com/support/faq/#fcm-android-background
检查一下:https://firebase.google.com/support/faq/#fcm-android-background
// Check if message contains a notification payload. // 检查消息是否包含通知负载。
if (remoteMessage.getNotification() != null) {
Map test = remoteMessage.getData();
Log.d(TAG, "Message data payload: " + test);
// Check if message contains a data payload. // 检查消息是否包含数据负载。
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.