Xamarin формирует CFB-уведомление: как обновить токен после однократной регистрации устройства? - PullRequest
0 голосов
/ 22 сентября 2019

Я создал приложение с Xamarin.forms.iOS и использую Plugin.FirebasePushNotification для отправки уведомления через firebase.

после регистрации устройства проблем с получением токена не возникает, но после удаления приложения ипроверьте снова, приложение не получает токен.Я больше не могу получать уведомления.

AppDelegate.cs

[Register("AppDelegate")]
public partial class AppDelegate : global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate
{
    NSDictionary _launchOptions;
    public override bool FinishedLaunching(UIApplication app, NSDictionary options)
    {
        global::Xamarin.Forms.Forms.Init();
        ImageCircleRenderer.Init();
        LoadApplication(new App());
        AnimationViewRenderer.Init();

        _launchOptions = options;

        Microsoft.WindowsAzure.MobileServices.CurrentPlatform.Init();
        SQLitePCL.Batteries.Init();

        ZXing.Net.Mobile.Forms.iOS.Platform.Init();
        FirebasePushNotificationManager.Initialize(options, true);

        if (UIDevice.CurrentDevice.CheckSystemVersion(10, 0))
        {
            UNUserNotificationCenter.Current.RequestAuthorization(UNAuthorizationOptions.Alert | UNAuthorizationOptions.Sound | UNAuthorizationOptions.Sound,
                                                                    (granted, error) =>
                                                                    {
                                                                        if (granted)
                                                                            InvokeOnMainThread(UIApplication.SharedApplication.RegisterForRemoteNotifications);
                                                                    });
        }
        else if (UIDevice.CurrentDevice.CheckSystemVersion(8, 0))
        {
            var pushSettings = UIUserNotificationSettings.GetSettingsForTypes(
                    UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound,
                    new NSSet());

            UIApplication.SharedApplication.RegisterUserNotificationSettings(pushSettings);
            UIApplication.SharedApplication.RegisterForRemoteNotifications();
        }
        else
        {
            UIRemoteNotificationType notificationTypes = UIRemoteNotificationType.Alert | UIRemoteNotificationType.Badge | UIRemoteNotificationType.Sound;
            UIApplication.SharedApplication.RegisterForRemoteNotificationTypes(notificationTypes);
        }


        CrossFirebasePushNotification.Current.OnTokenRefresh += (s, p) =>
        {
            System.Diagnostics.Debug.WriteLine($"TOKEN : {p.Token}");
            UIPasteboard clipboard = UIPasteboard.General;
            clipboard.String = p.Token;
            Helper.Settings.Token = p.Token;

        };

        CrossFirebasePushNotification.Current.OnNotificationReceived += (s, p) =>
        {
            Dictionary<string, object> dic = p.Data as Dictionary<string, object>;
            if (dic["targetUserId"].ToString() != null && Helper.Settings.UserId == dic["targetUserId"].ToString())
            {
                System.Diagnostics.Debug.WriteLine("Received");
                Helper.Settings.userWhoSentNotiId = dic["userWhoSentNotiId"].ToString();
                Helper.Settings.callingStatus = true;                 FirebasePushNotificationManager.CurrentNotificationPresentationOption = UNNotificationPresentationOptions.Alert;
                MessagingCenter.Send<object, string>(this, "CallingNotifications", dic["callingName"].ToString());
            }
        };

        CrossFirebasePushNotification.Current.OnNotificationOpened += (s, p) =>
        {
            Dictionary<string, object> dic = p.Data as Dictionary<string, object>;
            Helper.Settings.userWhoSentNotiId = dic["userWhoSentNotiId"].ToString();
            Helper.Settings.callingStatus = true;
            MessagingCenter.Send<object, string>(this, "CallingNotifications", dic["callingName"].ToString());
        };
        return base.FinishedLaunching(app, options);
    }

    public override void RegisteredForRemoteNotifications(UIApplication application, NSData deviceToken)
    {
        FirebasePushNotificationManager.DidRegisterRemoteNotifications(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);
        completionHandler(UIBackgroundFetchResult.NewData);
    }
}

Я заметил, что приложение не запускается CrossFirebasePushNotification.Current.OnTokenRefresh

Как приложение может обновитьтокенов

...