CrossGeeks / AzurePushNotificationPlugin

Azure Push Notification Plugin for Xamarin iOS and Android
MIT License
67 stars 34 forks source link

Token not get picked up on app restart #56

Open mohammedmsadiq opened 3 years ago

mohammedmsadiq commented 3 years ago

when receiving the notification for the 1st time it works fine but when the app restarts the token is empty and the app crashes see below for screenshots;

Screenshot 2020-11-16 at 23 21 10

my implementation below;

on appdelgete

        UIApplication.SharedApplication.SetStatusBarStyle(UIStatusBarStyle.LightContent, true); // <--- copy this instruction

        LoadApplication(new App(new iOSInitializer()));

        AzurePushNotificationManager.CurrentNotificationPresentationOption = UNNotificationPresentationOptions.Alert | UNNotificationPresentationOptions.Sound;
        AzurePushNotificationManager.Initialize(NotificationContstants.ListenConnectionString, NotificationContstants.NotificationHubName, launchOptions, true);
        return base.FinishedLaunching(uiApplication, launchOptions);
    }

    public override void RegisteredForRemoteNotifications(UIApplication application, NSData deviceToken)
    {
        AzurePushNotificationManager.DidRegisterRemoteNotifications(deviceToken);
    }

    public override void FailedToRegisterForRemoteNotifications(UIApplication application, NSError error)
    {
        AzurePushNotificationManager.RemoteNotificationRegistrationFailed(error);

    }

    // To receive notifications in foregroung on iOS 9 and below.
    // To receive notifications in background in any iOS version
    public override void DidReceiveRemoteNotification(UIApplication application, NSDictionary userInfo, Action<UIBackgroundFetchResult> completionHandler)
    {
        AzurePushNotificationManager.DidReceiveMessage(userInfo);
    }

on the app.xaml.cs file my implementation; onstart();

        // Handle when your app starts
        CrossAzurePushNotification.Current.OnTokenRefresh += (s, p) =>
        {
            Debug.WriteLine($"PushNotification TOOKEN REC: {p.Token}");
        };

        Debug.WriteLine($"PushNotification TOOKEN: {CrossAzurePushNotification.Current.Token}");

        CrossAzurePushNotification.Current.OnNotificationOpened += (s, p) =>
        {
            //System.Diagnostics.Debug.WriteLine(p.Identifier);

            Debug.WriteLine("Opened");
            foreach (var data in p.Data)
            {
                Debug.WriteLine($"{data.Key} : {data.Value}");
            }

            if (p.Data["aps.type"].ToString() == "Ecards")
            {
                Settings.WasLaunchedFromPush = true;
                Preferences.Set("IsFromHomePage", true);
                var instanceID = p.Data["aps.parameters"];
                Debug.WriteLine(instanceID);

                var navigationParams = new NavigationParameters();
                navigationParams.Add("questionInstanceID", instanceID);
                NavigationService.NavigateAsync("ECardQuestionPage", navigationParams, true, false);

            }
            else
            {
                Settings.WasLaunchedFromPush = true;
            }
        };

        CrossAzurePushNotification.Current.OnNotificationDeleted += (s, p) =>
        {
            Debug.WriteLine("Dismissed");
        };