FirebasePushNotificationPlugin icon indicating copy to clipboard operation
FirebasePushNotificationPlugin copied to clipboard

iOS: OnTokenRefresh will NEVER fire if autoRegistration=false

Open zizusoft opened this issue 5 years ago • 7 comments

Hi there,

great plugin, thanks.

Quick question.

I assume that OnTokenRefresh Event:

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

Is only called when the token gets refreshed and NOT when the app is launched / token is initially generated?

Would the correct method be to check for and save the initial token on app launch using CrossFirebasePushNotification.Current.Token

In the RegisteredForRemoteNotifications override of AppDeligate on iOS?

Thanks

zizusoft avatar Mar 15 '20 19:03 zizusoft

So on further investigation, it appears that OnTokenRefresh event is called on Android after initially registering for notifications, but is NOT called on iOS after initially registering for notifications.

Therefore I am assuming that on iOS the correct procedure is to update the token to the remote server in RegisteredForRemoteNotifications and OnTokenRefresh

Would be greatful is someone else could confirm this behaviour? Thanks

Lydecker avatar Apr 11 '20 13:04 Lydecker

You should save it just on OnTokenRefresh event

rdelrosario avatar Apr 11 '20 15:04 rdelrosario

Hi @rdelrosario - I've done some further investigation, and have found the issue.

Using plugin 3.1.6 on iOS 13.4 does NOT call OnTokenRefresh if the plugin is initialised as:

FirebasePushNotificationManager.Initialize(options, autoRegistration: false); Even if at a later date you call CrossFirebasePushNotification.Current.RegisterForPushNotifications(); - OnTokenRefresh will still never be invoked.

However if you initialise the plug in as: FirebasePushNotificationManager.Initialize(options);

The whole plugin works as expected on iOS and OnTokenRefresh is called as expected.

So the bug is with the autoRegistration argument on iOS being set to false.

Hope this helps.

Lydecker avatar Apr 12 '20 15:04 Lydecker

So I was having a read here: https://github.com/xamarin/GoogleApisForiOSComponents/blob/master/docs/Firebase/CloudMessaging/GettingStarted.md#about-firebase-cloud-messaging

It says that if you add in <key>FirebaseMessagingAutoInitEnabled</key> <false/>

To your Info.plist - that should disable registration by default.

However it doesn't.....!

zizusoft avatar Oct 06 '20 18:10 zizusoft

In our case we had to do this to fix the issue:

Messaging.SharedInstance.Delegate = CrossFirebasePushNotification.Current as IMessagingDelegate;
FirebasePushNotificationManager.Initialize(options, false);

ilnur-nazmutdinov-spark avatar Nov 19 '20 13:11 ilnur-nazmutdinov-spark

In our case we had to do this to fix the issue:


Messaging.SharedInstance.Delegate = CrossFirebasePushNotification.Current as IMessagingDelegate;

FirebasePushNotificationManager.Initialize(options, false);

If this works - you will have solved the only outstanding issue we have with this library! Thanks - will test hopefully this weekend and report back.

Lydecker avatar Nov 19 '20 16:11 Lydecker

This works for me:

..
FirebasePushNotificationManager.Initialize(options, false);
Messaging.SharedInstance.AutoInitEnabled = true;
Messaging.SharedInstance.Delegate = CrossFirebasePushNotification.Current as IMessagingDelegate;

LoadApplication(new App(new iOSInitializer()));
return base.FinishedLaunching(app, options);

timahrentlov avatar Aug 02 '22 08:08 timahrentlov