Java 新聊天信息上的Firebase推送通知

Java 新聊天信息上的Firebase推送通知,java,android,firebase,firebase-cloud-messaging,Java,Android,Firebase,Firebase Cloud Messaging,我已经创建了一个应用程序,我在其中聊天 我想,用户将收到一个通知,他们得到了一个新的消息,每当他们在应用程序之外 为此,我使用了Firebase服务 现在,每当发送消息时,我调用此方法: 我创建了以下MyFirebaseMessagingService类,以便在调用时进行测试: private class MyTask extends AsyncTask<MyTaskParams, Void, Void> { @Override protected Void doIn

我已经创建了一个应用程序,我在其中聊天

我想,用户将收到一个通知,他们得到了一个新的消息,每当他们在应用程序之外

为此,我使用了
Firebase
服务

现在,每当发送消息时,我调用此方法: 我创建了以下
MyFirebaseMessagingService
类,以便在调用时进行测试:

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;
    }
}
然而,当我发送一条消息时,我似乎从来没有收到过这个
日志,而且它看起来从来没有调用过
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()
方法。

首先检查您发送的内容:数据消息或通知消息。 它们看起来可能相同,但根据消息的类型,调用的方法不同。 看看这个:

//检查消息是否包含通知负载

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());

我用您的代码制作了一个演示应用程序,它运行良好,我可以看到当消息到达时,onMessageReceived函数被触发

我使用firebase消息传递版本20.1.7

implementation 'com.google.firebase:firebase-messaging:20.1.7'
因此,我认为您应该进行以下检查:

  • 检查你的应用程序是否在前台。因为当应用程序处于后台时,无法触发onMessageReceived。参考:

  • 检查发送http请求时是否写入了正确的设备令牌(字段“userDeviceIdKey”)

  • 检查您的http请求在catch中是否有错误


  • 希望可以帮助您。

    您告诉我检查remoteMessage是什么,但我说它甚至没有被调用,所以它不会显示任何结果。
    implementation 'com.google.firebase:firebase-messaging:20.1.7'