Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice

Bug [push-notifications 3.0.1] Game will freeze indefinitely if user does not accept Push Notifications.

Discussion in 'Player Engagement' started by skipculture, Jun 26, 2023.

  1. skipculture

    skipculture

    Joined:
    Apr 9, 2023
    Posts:
    8
    I'm seeing an issue with the RegisterForPushNotificationsAsync (iOS) - when a user has notifications off (no permission) the TaskCompletionSource is never resolved and the await will never be returned, freezing the application.

    When the user has Notifications off, RegisterForPushNotificationsAsync still makes it to calling:

    NativeRegisterForPushNotifications(NotificationRegistrationTokenReceived);


    and then will need to wait on the NotificationRegistrationTokenReceived handler to resolve the Task, even though it will never receive that callback.
     
    Last edited: Jun 26, 2023
  2. skipculture

    skipculture

    Joined:
    Apr 9, 2023
    Posts:
    8
    This may happen when a user manually turns off notifications after initially accepting.

    "This method reflects only the successful completion of the remote registration process that begins when you call the registerForRemoteNotifications method". This means if the user allows notifications when prompted, then goes into settings and disables them, the isRegisteredForRemoteNotifications will still return true."
     
  3. skipculture

    skipculture

    Joined:
    Apr 9, 2023
    Posts:
    8
    I was able to resolve this from the package-side by making the following modifications (after copying the package from /Library/PackageCache to /Packages):


    1. In Plugins/iOS/PushNotificationManager.mm, add the following helper method:

    Code (csharp):
    1.  
    2. - (BOOL)areNotificationsDisabled
    3. {
    4.     UIUserNotificationSettings *settings = [[UIApplication sharedApplication] currentUserNotificationSettings];
    5.     return settings.types == UIUserNotificationTypeNone;
    6. }
    7.  
    2. Update the registerForRemoteNotifications method:

    Code (csharp):
    1.  
    2. - (void) registerForRemoteNotifications : (REGISTRATION_CALLBACK) callback
    3. {
    4.     // If we've already registered for a token previously, and have one ready, then just return it without
    5.     // requesting authorisation again.
    6.  
    7.     if (_deviceToken != NULL && _deviceToken.length > 0) {
    8.         callback((char *)_deviceToken.UTF8String);
    9.         return;
    10.     }
    11.  
    12.     _registrationCallback = callback;
    13.  
    14.    // here is the update: this will send a null token in the callback, which will create an exception on the Runtime end
    15.     if ([self areNotificationsDisabled]) {
    16.         _registrationCallback(nil);
    17.         return;
    18.     }
    19.  
    20.     // .. rest of the method ...
    21. }
    22.  
    await PushNotificationsService.Instance.RegisterForPushNotificationsAsync() will then throw an Exception, as the callback handler has passed a null token (no changes are required on the package runtime side).
     
    Last edited: Jun 26, 2023