应用关闭时未收到fcm通知 [英] Not receive fcm notification when app is closed
问题描述
您好,我正在尝试在Xamarin表单应用中实施FCM. 我安装了 Plugin.FirebasePushNotification 插件
Hello I am trying tyo implement FCM in xamarin forms app. i Installed Plugin.FirebasePushNotification Plugin
我实现了刷新令牌类
[Service]
[IntentFilter(new[] { "com.google.firebase.INSTANCE_ID_EVENT" })]
public class MyFirebaseIIDService : FirebaseInstanceIdService
{
const string TAG = "MyFirebaseIIDService";
public override void OnTokenRefresh()
{
var refreshedToken = FirebaseInstanceId.Instance.Token;
Log.Debug(TAG, "Refreshed token: " + refreshedToken);
SendRegistrationToServer(refreshedToken);
}
void SendRegistrationToServer(string token)
{
// Add custom implementation, as needed.
}
}
该类用于接收通知,但在应用关闭时不会接收通知
and this class for receive notification but not get notification when app is close
[IntentFilter(new[] { "com.google.firebase.MESSAGING_EVENT" })]
public class MyFirebaseMessagingService : FirebaseMessagingService
{
const string TAG = "MyFirebaseMsgService";
// [START receive_message]
public override void OnMessageReceived(RemoteMessage message)
{
}
}
我用了这个有效载荷
{至":"APA91bHun4MxP5egoKMwt2KZFBaFUH-1RYqx ...",通知" :{ "body":非常匹配!", "title":葡萄牙vs.丹麦"}}
{ "to" : "APA91bHun4MxP5egoKMwt2KZFBaFUH-1RYqx...", "notification" : { "body" : "great match!", "title" : "Portugal vs. Denmark" } }
推荐答案
从Android官方文档中,FirebaseMessaging
具有方法subscribeToTopic
.
From Android Official document,FirebaseMessaging
has method of subscribeToTopic
.
public Task subscribeToTopic (String topic)
在后台订阅主题. 订阅操作将保持不变,并将重试直到成功. 这使用Firebase实例ID令牌来识别应用程序实例,并定期将数据发送到Firebase后端.
Subscribes to topic in the background. The subscribe operation is persisted and will be retried until successful. This uses a Firebase Instance ID token to identify the app instance and periodically sends data to the Firebase backend.
这是 Android FCM远程通知的官方示例.测试此项目,如果不订阅通知,则应用停止后将不会收到通知.
Here is a official sample for Android FCM Remote Notifications.After testing this project,If not subscribing notification, notification will not receive when app stopped.
在加载应用程序时,您可以尝试使用以下代码:
You can have a try.Code as follow when App load:
FirebaseMessaging.Instance.SubscribeToTopic("news");
这篇关于应用关闭时未收到fcm通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!