即使传递了数据有效负载,当应用程序处于后台时也不会收到 Firebase 通知
Firebase Notifications not being received when app is in background even if data payload is passed
现在我知道如果我发送数据消息,即使应用程序在后台也会收到通知。但是即使在发送数据消息后我也没有收到任何通知。
我正在使用 Postman 进行测试,这是我的 Postman 请求正文:
{
"to" : (device token),
"data" : {
"body" : "great match!",
"title" : "Portugal vs. Denmark",
"content_available" : true,
"priority" : "high",
"sound": "default",
"time_to_live":"2419200"
}
}
这是我的 onMessageReceived()
功能:
public void onMessageReceived(RemoteMessage remoteMessage) {
Map<String,String> data=remoteMessage.getData();
String title=data.get("title");
String body=data.get("body");
if (remoteMessage.getData().size() > 0) {
Log.e(TAG, "Data Payload: " + remoteMessage.getData().toString());
try {
JSONObject json = new JSONObject(remoteMessage.getData().toString());
sendPushNotification(title,body);
} catch (Exception e) {
Log.e(TAG, "Exception: " + e.getMessage());
}
}
}
这就是我的 sendPushNotification()
函数的样子:
private void sendPushNotification(String title,String message) {
try {
String imageUrl="";
MyNotificationManager mNotificationManager = new MyNotificationManager(getApplicationContext());
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
if(imageUrl.equals("null")){
mNotificationManager.showSmallNotification(title, message, intent);
mNotificationManager.playNotificationSound();
}else{
mNotificationManager.showBigNotification(title, message, imageUrl, intent);
mNotificationManager.playNotificationSound();
}
} catch (Exception e) {
Log.e(TAG, "Json Exception: " + e.getMessage());
}
}
我在关注堆栈溢出问题:
P.S.: 当应用程序在前台时,可以正常接收通知。
您需要在通知负载中定义 click_action
。
"notification"{
"click_action":"YOUR_ACTIVITY_NAME"
}
同样在您的清单中编辑 activity 定义如下
<activity
android:name="com.demo.xyzActivity"
android:windowSoftInputMode="stateHidden">
<intent-filter>
<action android:name=".xyzActivity" /> //This should be one set into payload
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
当应用程序处于后台时,这将打开您的 activity。
正如您在我的 post 上看到的那样,对于特定的 ID,您应该使用 "registration_ids": ["{device-token}","{device2-token}","{device3-token}"]
键和值而不是 ”to”
。那是为了主题
您是否将通知负载与数据负载一起使用?该函数发现通知有效载荷,它直接将其发送到通知托盘,因此数据有效载荷被忽略。
改变
JSONObject json = new JSONObject(remoteMessage.getData().toString());
至
Map<String, String> bundleData = remoteMessage.getData();
这将删除 try catch 并让您知道它导致的异常。
创建广播接收器
在Manifest.xml
中声明
<uses-permission android:name="android.permission.WAKE_LOCK" />
<receiver
android:name=".OnBootBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
创建 OnBootBroadcastReceiver.java 文件并在其中调用 FCM 服务。
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class OnBootBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent i = new Intent("com.example.FirebaseMessagingReceiveService");
i.setClass(context, FirebaseMessagingReceiveService.class);
context.startService(i);
}
}
现在我知道如果我发送数据消息,即使应用程序在后台也会收到通知。但是即使在发送数据消息后我也没有收到任何通知。
我正在使用 Postman 进行测试,这是我的 Postman 请求正文:
{
"to" : (device token),
"data" : {
"body" : "great match!",
"title" : "Portugal vs. Denmark",
"content_available" : true,
"priority" : "high",
"sound": "default",
"time_to_live":"2419200"
}
}
这是我的 onMessageReceived()
功能:
public void onMessageReceived(RemoteMessage remoteMessage) {
Map<String,String> data=remoteMessage.getData();
String title=data.get("title");
String body=data.get("body");
if (remoteMessage.getData().size() > 0) {
Log.e(TAG, "Data Payload: " + remoteMessage.getData().toString());
try {
JSONObject json = new JSONObject(remoteMessage.getData().toString());
sendPushNotification(title,body);
} catch (Exception e) {
Log.e(TAG, "Exception: " + e.getMessage());
}
}
}
这就是我的 sendPushNotification()
函数的样子:
private void sendPushNotification(String title,String message) {
try {
String imageUrl="";
MyNotificationManager mNotificationManager = new MyNotificationManager(getApplicationContext());
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
if(imageUrl.equals("null")){
mNotificationManager.showSmallNotification(title, message, intent);
mNotificationManager.playNotificationSound();
}else{
mNotificationManager.showBigNotification(title, message, imageUrl, intent);
mNotificationManager.playNotificationSound();
}
} catch (Exception e) {
Log.e(TAG, "Json Exception: " + e.getMessage());
}
}
我在关注堆栈溢出问题:
P.S.: 当应用程序在前台时,可以正常接收通知。
您需要在通知负载中定义 click_action
。
"notification"{
"click_action":"YOUR_ACTIVITY_NAME"
}
同样在您的清单中编辑 activity 定义如下
<activity
android:name="com.demo.xyzActivity"
android:windowSoftInputMode="stateHidden">
<intent-filter>
<action android:name=".xyzActivity" /> //This should be one set into payload
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
当应用程序处于后台时,这将打开您的 activity。
正如您在我的 post 上看到的那样,对于特定的 ID,您应该使用 "registration_ids": ["{device-token}","{device2-token}","{device3-token}"]
键和值而不是 ”to”
。那是为了主题
您是否将通知负载与数据负载一起使用?该函数发现通知有效载荷,它直接将其发送到通知托盘,因此数据有效载荷被忽略。 改变
JSONObject json = new JSONObject(remoteMessage.getData().toString());
至
Map<String, String> bundleData = remoteMessage.getData();
这将删除 try catch 并让您知道它导致的异常。
创建广播接收器
在Manifest.xml
中声明 <uses-permission android:name="android.permission.WAKE_LOCK" />
<receiver
android:name=".OnBootBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
创建 OnBootBroadcastReceiver.java 文件并在其中调用 FCM 服务。
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class OnBootBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent i = new Intent("com.example.FirebaseMessagingReceiveService");
i.setClass(context, FirebaseMessagingReceiveService.class);
context.startService(i);
}
}