发送推送通知时出现“错误”:“InvalidRegistration”

问题描述 投票:8回答:2

我正在使用FirebasePushNotificationPlugin中的Xamarin.Forms实现FCM推送通知。在iOS项目中,在AppDelegateRegisteredForRemoteNotifications方法调用deviceToken生成但当我发送邮件生成token的通知时,我收到错误。

{“multicast_id”:8631208504861228784,“success”:0,“failure”:1,“canonical_ids”:0,“results”:[{“error”:“InvalidRegistration”}]}

这是AppDelegatehere得到的代码:

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"

}
}

邮递员的身体

enter image description here

这些是Visual Studio中的Provisioning profiles设置

enter image description here

我该如何解决这个问题?

ios xamarin xamarin.forms xamarin.ios firebase-cloud-messaging
2个回答
1
投票

我对Xamarin并不熟悉。但我和FCM合作很多。

我觉得你的错误令牌。使用deviceToken不适用于FCM的推送通知。我做了一个搜索,也许你必须得到它

var fcmToken = FirebaseInstanceId.Instance.Token;

更多细节:https://docs.microsoft.com/en-us/xamarin/android/data-cloud/google-messaging/remote-notifications-with-fcm?tabs=macos


-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_的替代方法,您可以执行以下操作:

  1. 使您的AppDelegate实现IMessagingDelegate接口。
  2. 实现以下方法: //每当生成新令牌时都会触发此回调 - 为了调用它,AppDelegate必须是IMessagingDelegate [导出( “消息:didReceiveRegistrationToken:”)] public async void 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}'"); }
© www.soinside.com 2019 - 2024. All rights reserved.