Firebase推送通知在新的聊天消息时

huangapple go评论66阅读模式
英文:

Firebase push notification on new chat message

问题

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

我希望用户在离开应用程序时能收到新消息的通知。

为了实现这一点,我正在使用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";   // 你的 FCM AUTH 密钥
        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);   // 通知标题
            info.put("body", 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>
英文:

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.

Now, whenever a message is sent, I call this method:
I have created the following MyFirebaseMessagingService class to test when it is called:

private class MyTask extends AsyncTask&lt;MyTaskParams, Void, Void&gt; {
@Override
protected Void doInBackground(MyTaskParams... params) {
String userDeviceIdKey = params[0].url;
String title = params[0].title;
String body = params[0].body;
String authKey = &quot;XXXXX&quot;;   // You FCM AUTH key
String FMCurl = &quot;https://fcm.googleapis.com/fcm/send&quot;;
URL url;
try {
url = new URL(FMCurl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setUseCaches(false);
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setRequestMethod(&quot;POST&quot;);
conn.setRequestProperty(&quot;Authorization&quot;,&quot;key=&quot;+authKey);
conn.setRequestProperty(&quot;Content-Type&quot;,&quot;application/json&quot;);
JSONObject json = new JSONObject();
json.put(&quot;to&quot;,userDeviceIdKey);
json.put(&quot;priority&quot;,&quot;high&quot;);
JSONObject info = new JSONObject();
info.put(&quot;title&quot;, title);   // Notification title
info.put(&quot;body&quot;, body); // Notification body
info.put(&quot;var1&quot;, chatID);
info.put(&quot;var2&quot;,receiverID);
info.put(&quot;var3&quot;,itemID);
json.put(&quot;data&quot;, info);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(json.toString());
wr.flush();
Log.w(&quot;&quot;, &quot;getInstanceId failed&quot; + 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(&quot;TESTING_MESSAGE&quot;, &quot;&quot; + remoteMessage);
}
}

However, It seems like I never get this Log when I send a message and it doesnt look like onMessageReceived is ever called.

Is there any reason?

My next goal is to make notification inside onMessageReceived but I want to make sure first that I get there.

My manifest is:

&lt;service
android:name=&quot;.service.MyFirebaseMessagingService&quot;
android:permission=&quot;com.google.android.c2dm.permission.SEND&quot;&gt;
&lt;intent-filter&gt;
&lt;action android:name=&quot;com.google.firebase.MESSAGING_EVENT&quot; /&gt;
&lt;action android:name=&quot;com.google.android.c2dm.intent.RECEIVE&quot; /&gt;
&lt;/intent-filter&gt;
&lt;/service&gt;

答案1

得分: 1

我使用你的代码制作了一个演示应用,它能够很好地工作,当消息到达时我可以看到onMessageReceived函数被触发。

我使用的是firebase-messaging版本20.1.7

implementation 'com.google.firebase:firebase-messaging:20.1.7'

因此我认为你应该进行以下检查:

  1. 检查你的应用是否在前台运行。因为当应用在后台时无法触发onMessageReceived。参考:https://firebase.google.com/docs/cloud-messaging/android/receive#handling_messages

  2. 检查当你发送HTTP请求时,是否写了正确的设备令牌(你的字段'userDeviceIdKey')。

  3. 检查你的HTTP请求在catch块中是否有错误。

希望能对你有所帮助。

英文:

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.

I use the firebase-messaging version 20.1.7

implementation &#39;com.google.firebase:firebase-messaging:20.1.7&#39;

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. refer: 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.

  3. check if your http request has some error in catch.

hope can help you.

答案2

得分: 0

onMessageReceived()方法在应用程序被杀死时不会被调用。
https://stackoverflow.com/questions/37358462/firebase-onmessagereceived-not-called-when-app-in-background

英文:

The onMessageReceived() method will never be called if the app is killed.
https://stackoverflow.com/questions/37358462/firebase-onmessagereceived-not-called-when-app-in-background

答案3

得分: 0

首先,检查您发送的内容:数据消息还是通知消息。
它们可能看起来相同,但根据消息类型,调用不同的方法。
请查看此链接: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());

英文:

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

// Check if message contains a notification payload.

if (remoteMessage.getNotification() != null) {
Map test = remoteMessage.getData();
Log.d(TAG, &quot;Message data payload: &quot; + test);

// Check if message contains a data payload.

 if (remoteMessage.getData().size() &gt; 0) {
if (remoteMessage.getNotification() != null) {
Log.d(TAG, &quot;Message Notification Body: &quot; + 
remoteMessage.getNotification().getBody());

huangapple
  • 本文由 发表于 2020年5月29日 04:20:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/62073845.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定