简体   繁体   English

Firebase 新聊天消息推送通知

[英]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:所以我认为你应该做以下检查:

  1. 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

  2. check if you wrote the right device token(your field 'userDeviceIdKey') when you sent the http request.检查您在发送 http 请求时是否写入了正确的设备令牌(您的字段“userDeviceIdKey”)。

  3. 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.

相关问题 Firebase 在没有云功能的情况下发送新消息时向聊天成员推送通知 - Firebase pushing notification to chat members when a new message gets delivered without cloud functions Laravel 推送通知与 Firebase - Laravel Push Notification with Firebase Firebase 网络推送通知 - Firebase Web Push notification 重定向Firebase推送通知 - Redirect Firebase Push notification 是否可以在带有可扩展选项的推送通知中发送 Firebase(FCM) 通知消息(多行消息); 使用 Python? - Is it possible to send Firebase(FCM) notification message(multiple lined message) in the push notification with expandable option on it; using Python? firebase 数据库推送通知改变 - Push notification on firebase database changed firebase 推送通知 expo 不工作 - firebase push notification with expo not working 如何使用 Firebase 云消息在 Twilio 上配置新的服务器密钥以进行推送通知 - How to configure new Server Key on Twilio using Firebase cloud messaging for push notification Flutter - Firebase 使用 firebase 消息传递成功但未收到通知的推送通知 - Flutter - Firebase push notification using firebase messaging success but not get notification Firebase 深层链接的 APNS 推送通知结构 - APNS Push Notification Structure for Firebase Deep Links
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM