Java 当应用程序处于后台时,即使传递了数据负载,也不会收到Firebase通知
现在我知道,如果我发送数据消息,即使应用程序在后台,通知也会出现。但即使在发送数据消息之后,我也没有收到任何通知 我使用邮递员进行测试,以下是我的邮递员请求:Java 当应用程序处于后台时,即使传递了数据负载,也不会收到Firebase通知,java,android,firebase,firebase-notifications,Java,Android,Firebase,Firebase Notifications,现在我知道,如果我发送数据消息,即使应用程序在后台,通知也会出现。但即使在发送数据消息之后,我也没有收到任何通知 我使用邮递员进行测试,以下是我的邮递员请求: { "to" : (device token), "data" : { "body" : "great match!", "title" : "Portugal vs. Denmark", "content_available" : true, "priority" : "high",
{
"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());
}
}
}
我一直在关注关于堆栈溢出的问题:
注意:当应用程序位于前台时,通知会被很好地接收。您需要在通知负载中定义
单击\u action
"notification"{
"click_action":"YOUR_ACTIVITY_NAME"
}
另外,在清单中编辑活动定义,如下所示
<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>
//这应该是有效载荷中的一组
这将在应用程序位于后台时打开您的活动。正如您在我的帖子上看到的,对于特定ID,您应该使用
“注册ID”:[“{device token}”、“{device2 token}”、“{device3 token}”]
键和值,而不是”到“
”。这是主题您是否将通知负载与数据负载一起使用?由于函数查找通知有效负载,它直接将其发送到通知托盘,因此忽略了数据有效负载。
改变
到
Map 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>
检查这个谢谢你的回复!但是我只使用了一个数据负载..这个问题也需要使用通知负载。此外,我在任何地方都看不到通知。好的,我想澄清一下,您想要
数据
和通知
?或者只有一个?当应用程序在后台时,我想要一个通知…我真的很困惑…我看到了其他类似问题的答案,他们说我需要使用数据负载,只有这样,当应用程序在后台时才会收到通知。你能帮我吗?嘿!感谢您的关注和回答!我尝试了你的建议,但问题是,尽管设备令牌是正确的,我还是在说“InvalidRegistration”时出错。哦,我忘了问:你在使用什么设备?这很重要我用的是联想K5嗨,伙计!非常感谢你的帮助!这些通知在Moto、Redmi、Samsung和大多数其他设备上都能正常工作,除了我的是联想Vibe K5和其他安卓Oreo设备,这些设备非常易于管理。再次表示衷心的感谢!你真的帮了大忙!:嘿!我很高兴能帮上忙。我希望你以后能为你的手机解决这个问题。这应该是FCM或联想实施的问题。如果我能在其他方面提供帮助,请告诉我。快乐代码!什么是FirebaseMessagingReceiveService.class
?FirebaseMessagingReceiveService是从FCM接收任何消息时调用的类,其名称在您的情况下可能不同。例如,扩展FirebaseMessagingService并具有onMessageReceived()方法的类。
JSONObject json = new JSONObject(remoteMessage.getData().toString());
Map<String, String> bundleData = remoteMessage.getData();
<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>
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);
}
}