我正在使用Xamarin.Forms
中的FirebasePushNotificationPlugin
实现FCM推送通知。在iOS项目中,在AppDelegate
中,当RegisteredForRemoteNotifications
方法调用deviceToken
生成时,但是当我通过Postman发送生成的token
的通知时,却出错了。
{“ multicast_id”:8631208504861228784,“成功”:0,“失败”: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中的“配置”配置文件设置
我该如何解决这个问题?
答案 0 :(得分:1)
我对Xamarin不熟悉。但是我与FCM合作很多。
我认为您获得了错误的令牌。使用deviceToken
不适用于FCM的推送通知。我进行了搜索,也许您必须从
var fcmToken = FirebaseInstanceId.Instance.Token;
答案 1 :(得分:-1)
编辑是因为我对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_的替代方法,您可以执行以下操作:
实施以下方法:
//每当生成新令牌时都会触发此回调-为了被调用,AppDelegate必须是IMessagingDelegate
[Export(“ messaging:didReceiveRegistrationToken:”)]
公共异步无效DidReceiveRegistrationToken(消息传递,字符串令牌) {
// 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}'");
}