我正在使用FirebasePushNotificationPlugin
中的Xamarin.Forms
实现FCM推送通知。在iOS项目中,在AppDelegate
当RegisteredForRemoteNotifications
方法调用deviceToken
生成但当我发送邮件生成token
的通知时,我收到错误。
{“multicast_id”:8631208504861228784,“success”:0,“failure”:1,“canonical_ids”:0,“results”:[{“error”:“InvalidRegistration”}]}
这是AppDelegate
从here得到的代码:
public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
global::Xamarin.Forms.Forms.Init();
LoadApplication(new App());
FirebasePushNotificationManager.Initialize(options, new NotificationUserCategory[]
{
new NotificationUserCategory("message",new List<NotificationUserAction> {
new NotificationUserAction("Reply","Reply",NotificationActionType.Foreground)
}),
new NotificationUserCategory("request",new List<NotificationUserAction> {
new NotificationUserAction("Accept","Accept"),
new NotificationUserAction("Reject","Reject",NotificationActionType.Destructive)
})
});
return base.FinishedLaunching(app, options);
}
public override void RegisteredForRemoteNotifications(UIApplication application, NSData deviceToken)
{
FirebasePushNotificationManager.DidRegisterRemoteNotifications(deviceToken);
Console.WriteLine("Token- - - : "+deviceToken);
}
public override void FailedToRegisterForRemoteNotifications(UIApplication application, NSError error)
{
FirebasePushNotificationManager.RemoteNotificationRegistrationFailed(error);
}
public override void DidReceiveRemoteNotification(UIApplication application, NSDictionary userInfo, Action<UIBackgroundFetchResult> completionHandler)
{
FirebasePushNotificationManager.DidReceiveMessage(userInfo);
System.Console.WriteLine(userInfo);
completionHandler(UIBackgroundFetchResult.NewData);
}
发送样本通知时邮递员中的数据object
{
"to":"79f64b43339859a329a935f7a3e417ecc1599fbb5d6935afbooa3b4291c07fa7",
"notification" : {
"body" : "New task",
"content_available" : true,
"priority" : "high",
"color":"Page1",
"title":"Announcement"
},
"data" : {
"color":"Page1",
"title":"title",
"content_available" : true,
"body" : "New Announcement ad"
}
}
邮递员的身体
这些是Visual Studio中的Provisioning profiles设置
我该如何解决这个问题?
我对Xamarin并不熟悉。但我和FCM合作很多。
我觉得你的错误令牌。使用deviceToken
不适用于FCM的推送通知。我做了一个搜索,也许你必须得到它
var fcmToken = FirebaseInstanceId.Instance.Token;
编辑,因为我关于RegisteredForRemoteNotifications的初步答案是错误的
如记录的here,实际令牌位于收到的令牌的描述中:
public override void RegisteredForRemoteNotifications (
UIApplication application, NSData deviceToken)
{
// Get current device token
var DeviceToken = deviceToken.Description;
if (!string.IsNullOrWhiteSpace(DeviceToken)) {
DeviceToken = DeviceToken.Trim('<').Trim('>');
}
// Get previous device token
var oldDeviceToken = NSUserDefaults.StandardUserDefaults.StringForKey("PushDeviceToken");
// Has the token changed?
if (string.IsNullOrEmpty(oldDeviceToken) || !oldDeviceToken.Equals(DeviceToken))
{
//TODO: Put your own logic here to notify your server that the device token has changed/been created!
}
// Save new device token
NSUserDefaults.StandardUserDefaults.SetString(DeviceToken, "PushDeviceToken");
}
所以令牌是上面的DeviceToken。
作为实现_ RegisteredForRemoteNotifications_的替代方法,您可以执行以下操作:
// Subscribe to a 'news' topic so we can send to just those subscribed to this topic
messaging.Subscribe("news");
// Log this to debug output so that we can capture it for testing
Debug.WriteLine($"DidReceiveRegistration Token:'{token}'");
}