[go: nahoru, domu]

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OnTokenRefresh on iOS fires with the same token until i delete the app #242

Closed
vasiliy-s opened this issue Jun 21, 2019 · 6 comments
Closed

Comments

@vasiliy-s
Copy link

Push notificatios works, they are received but only when i launch the app for the first time.
After that i get error on server saying that token is not registered (and that is true) because OnTokenRefresh is being fired with the same token.
The only thing that helps is to delet an app an install again.

Nothing specific in AppDelegate.

LoadApplication(new App());
FirebasePushNotificationManager.Initialize(options);

and

public override bool OpenUrl(UIApplication app, NSUrl url, NSDictionary options)
{
    AuthenticationContinuationHelper.SetAuthenticationContinuationEventArgs(url);
    return true;
}

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);
}
@vasiliy-s
Copy link
Author

That is the firebase-ios issue.
firebase/firebase-ios-sdk#2438

If you have Xamarin.Firebase.iOS.Core 5.2.0 and Xamarin.Firebase.iOS.InstanceId 3.4.0 (or maybe on of them, not sure) then youll get that issue. Downgrading to 5.1.3 and 3.2.1 solved the issue

@vinsant
Copy link
vinsant commented Oct 15, 2019

I have the same problem.
I use this plugins as well but I can't see Xamarin.Firebase.iOS.Core in my nugets.
Could you tell me how I can downgrade them?

@vasiliy-s
Copy link
Author
vasiliy-s commented Oct 15, 2019 via email

@vinsant
Copy link
vinsant commented Oct 15, 2019

Oh! not yet.
I will check it.

@vinsant
Copy link
vinsant commented Oct 15, 2019

I already check them.
they have changed to 5.1.3 and 3.2.1
But currently, When I try to run the app, token is ""
sorry, Could you tell why token become blank value?
How you get token value for using?

thank you

@vinsant
Copy link
vinsant commented Oct 15, 2019

here is my code

using System;
using System.Collections.Generic;
using System.Linq;

using Foundation;
using ImageCircle.Forms.Plugin.iOS;
using UIKit;
using Lottie.Forms.iOS.Renderers;
using System.Threading.Tasks;
using UserNotifications;
using Newtonsoft.Json.Linq;
using Microsoft.WindowsAzure.MobileServices;
using Xamarin.Forms;
using Plugin.FirebasePushNotification;
using Firebase.CloudMessaging;
using Plugin.FirebasePushNotification.Abstractions;
//using QRTrack.Helper;

//using WindowsAzure.Messaging;

namespace QRTrack.iOS
{
    // The UIApplicationDelegate for the application. This class is responsible for launching the 
    // User Interface of the application, as well as listening (and optionally responding) to 
    // application events from iOS.
    [Register("AppDelegate")]
    public partial class AppDelegate : global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate
    {
        NSDictionary _launchOptions;


        public const string ListenConnectionString = "Endpoint=.....myValue...........";
        public const string NotificationHubName = "........myValue..........";

        //private SBNotificationHub Hub { get; set; }
        //
        // This method is invoked when the application has loaded and is ready to run. In this 
        // method you should instantiate the window, load the UI into it and then make the window
        // visible.
        //
        // You have 17 seconds to return from this method, or iOS will terminate your application.
        //
        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;
                    Xamarin.Forms.Application.Current.Properties["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;
                Xamarin.Forms.Application.Current.Properties["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);
        }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants