How to handle notification when app in background in Firebase(Firebase中应用程序在后台时如何处理通知)
问题描述
这是我的清单:
<service android:name=".fcm.PshycoFirebaseMessagingServices"><意图过滤器><action android:name="com.google.firebase.MESSAGING_EVENT"/></意图过滤器></服务><service android:name=".fcm.PshycoFirebaseInstanceIDService><意图过滤器><action android:name="com.google.firebase.INSTANCE_ID_EVENT"/></意图过滤器></服务>当应用程序在后台并且通知到达时,默认通知会到来并且不会运行我的 onMessageReceived 代码.
这是我的 onMessageReceived 代码.如果我的应用程序在前台运行,而不是在后台运行,则会调用此方法.当应用也在后台时如何运行此代码?
//[开始接收消息]@覆盖公共无效 onMessageReceived(RemoteMessage remoteMessage) {//TODO(developer):在此处处理 FCM 消息.//如果应用程序在前台,则在此处处理数据和通知消息.//此外,如果您打算根据收到的 FCM 生成自己的通知//消息,这里是应该启动的地方.请参阅下面的 sendNotification 方法.数据 = remoteMessage.getData();字符串标题 = remoteMessage.getNotification().getTitle();字符串消息 = remoteMessage.getNotification().getBody();String imageUrl = (String) data.get("image");String action = (String) data.get("action");Log.i(TAG, "onMessageReceived: title : "+title);Log.i(TAG, "onMessageReceived: message : "+message);Log.i(TAG, "onMessageReceived: imageUrl : "+imageUrl);Log.i(TAG, "onMessageReceived: action : "+action);if (imageUrl == null) {发送通知(标题、消息、操作);} 别的 {new BigPictureNotification(this,title,message,imageUrl,action);}}//[结束接收消息]1.为什么会这样?
FCM(Firebase Cloud Messaging)中有两种类型的消息:
- 显示消息:这些消息仅在您的应用处于前台 时触发
- 数据消息:如果您的应用处于前台/后台/已终止
onMessageReceived()回调<块引用>
注意: Firebase 团队尚未开发用于发送 data-messages 的 UI你的设备,但是.您应该使用您的服务器发送此类型!
2.怎么做?
为此,您必须向以下 URL 执行 POST 请求:
POST https://fcm.googleapis.com/fcm/send
标题
- 键:
Content-Type,值:application/json - 密钥:
授权,值:key=<your-server-key>
正文使用主题
<代码>{"to": "/topics/my_topic",数据": {"my_custom_key": "my_custom_value",my_custom_key2":真}}或者如果您想将其发送到特定设备
<代码>{数据": {"my_custom_key": "my_custom_value",my_custom_key2":真},"registration_ids": ["{device-token}","{device2-token}","{device3-token}"]}注意:确保您不添加 JSON 密钥通知
注意:要获取您的服务器密钥,您可以在 firebase 控制台中找到它:您的项目 ->设置->项目设置->云消息传递->服务器密钥
3.推送通知消息如何处理?
这是您处理收到的消息的方式:
@Override公共无效 onMessageReceived(RemoteMessage remoteMessage) {映射<字符串,字符串>数据 = remoteMessage.getData();字符串 myCustomKey = data.get("my_custom_key");//管理数据}Here is my manifest:
<service android:name=".fcm.PshycoFirebaseMessagingServices">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<service android:name=".fcm.PshycoFirebaseInstanceIDService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
</intent-filter>
</service>
When the app is in the background and a notification arrives, then the default notification comes and doesn't run my code of onMessageReceived.
Here is my onMessageReceived code. This is invoked if my app is running on the foreground, not when it is running in the background. How can I run this code when the app is in background too?
// [START receive_message]
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
// TODO(developer): Handle FCM messages here.
// If the application is in the foreground handle both data and notification messages here.
// Also if you intend on generating your own notifications as a result of a received FCM
// message, here is where that should be initiated. See sendNotification method below.
data = remoteMessage.getData();
String title = remoteMessage.getNotification().getTitle();
String message = remoteMessage.getNotification().getBody();
String imageUrl = (String) data.get("image");
String action = (String) data.get("action");
Log.i(TAG, "onMessageReceived: title : "+title);
Log.i(TAG, "onMessageReceived: message : "+message);
Log.i(TAG, "onMessageReceived: imageUrl : "+imageUrl);
Log.i(TAG, "onMessageReceived: action : "+action);
if (imageUrl == null) {
sendNotification(title,message,action);
} else {
new BigPictureNotification(this,title,message,imageUrl,action);
}
}
// [END receive_message]
1. Why is this happening?
There are two types of messages in FCM (Firebase Cloud Messaging):
- Display Messages: These messages trigger the
onMessageReceived()callback only when your app is in foreground - Data Messages: Theses messages trigger the
onMessageReceived()callback even if your app is in foreground/background/killed
NOTE: Firebase team have not developed a UI to send
data-messagesto your devices, yet. You should use your server for sending this type!
2. How to?
To achieve this, you have to perform a POST request to the following URL:
POSThttps://fcm.googleapis.com/fcm/send
Headers
- Key:
Content-Type, Value:application/json - Key:
Authorization, Value:key=<your-server-key>
Body using topics
{
"to": "/topics/my_topic",
"data": {
"my_custom_key": "my_custom_value",
"my_custom_key2": true
}
}
Or if you want to send it to specific devices
{
"data": {
"my_custom_key": "my_custom_value",
"my_custom_key2": true
},
"registration_ids": ["{device-token}","{device2-token}","{device3-token}"]
}
NOTE: Be sure you're not adding JSON key
notification
NOTE: To get your server key, you can find it in the firebase console:Your project -> settings -> Project settings -> Cloud messaging -> Server Key
3. How to handle the push notification message?
This is how you handle the received message:
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Map<String, String> data = remoteMessage.getData();
String myCustomKey = data.get("my_custom_key");
// Manage data
}
这篇关于Firebase中应用程序在后台时如何处理通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Firebase中应用程序在后台时如何处理通知
基础教程推荐
- Android Volley - 如何动画图像加载? 2022-01-01
- UIImage 在开始时不适合 UIScrollView 2022-01-01
- navigationItem.backBarButtonItem 不工作?为什么上一个菜单仍然显示为按钮? 2022-01-01
- iOS - UINavigationController 添加多个正确的项目? 2022-01-01
- 为什么姜饼模拟器方向卡在应用程序中? 2022-01-01
- 如何将图像从一项活动发送到另一项活动? 2022-01-01
- SwiftUI-ScrollViewReader的ScrollTo不滚动 2022-01-01
- Play 商店的设备兼容性问题 2022-01-01
- 如何比较两个 NSDate:哪个是最近的? 2022-01-01
- Xcode UIView.init(frame:) 只能在主线程中使用 2022-01-01
