Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

PushNotificationsService OnNotificationReceived not being called

Discussion in 'iOS and tvOS' started by jbassking, Jul 7, 2023.

  1. jbassking

    jbassking

    Joined:
    Feb 27, 2014
    Posts:
    106
    I'm testing out the PushNotificationsService on iOS and noticed that OnNotificationReceived is not being called. We do get a token which is what we use to send the notifications.

    I do receive notification on the device.

    In my splash script
    Code (CSharp):
    1.    
    2. async void Start()
    3.     {
    4.         await UnityServices.InitializeAsync();
    5.         await Unity.Services.Analytics.AnalyticsService.Instance.CheckForRequiredConsents();
    6.     }
    7.  
    In another scene after the user logs into our app.

    Code (CSharp):
    1.        
    2. private void OnEnable()
    3. {    
    4.      PushNotificationsService.Instance.OnNotificationReceived += Instance_OnNotificationReceived;
    5.  
    6.      _ = RegisterForNotifications();
    7. }
    8.  
    9. public static void Instance_OnNotificationReceived(System.Collections.Generic.Dictionary<string, object> obj)
    10. {
    11.      Debug.Log($"Recevied notification: {JsonConvert.SerializeObject(obj)}");
    12. }
    13.  
    14. private async Task RegisterForNotifications()
    15. {
    16.     try
    17.     {
    18.         string pushToken = await PushNotificationsService.Instance.RegisterForPushNotificationsAsync();
    19.         await SaveToken(pushToken);              
    20.     }
    21.     catch (Exception e)
    22.     {
    23.         Debug.Log("Failed to retrieve a push notification token.");
    24.     }
    25.  
    26. }
    27.  
     
  2. thanyalukj_unity

    thanyalukj_unity

    Unity Technologies

    Joined:
    Apr 4, 2020
    Posts:
    7
    Hi @jbassking
    Thank you for letting us know. We will investigate your issue. Could you tell us what is the state of the app when receiving notifications?
    1. The app is running in the foreground
    2. The app is running in the background
    3. The app is not running.

    Thank you.
     
  3. jbassking

    jbassking

    Joined:
    Feb 27, 2014
    Posts:
    106
    I tried all the different cases, 1-3 all with the same results.
     
  4. thanyalukj_unity

    thanyalukj_unity

    Unity Technologies

    Joined:
    Apr 4, 2020
    Posts:
    7
    Thanks for more information. We are looking into it.
     
  5. jbassking

    jbassking

    Joined:
    Feb 27, 2014
    Posts:
    106
    I'm using Unity 2022.3.4f1 with XCode 14.3.1
     
  6. thanyalukj_unity

    thanyalukj_unity

    Unity Technologies

    Joined:
    Apr 4, 2020
    Posts:
    7
    Hi @jbassking

    To give you an update. We are still working on it. We might find an issue. This is because the notification is received before the OnNotificationReceived event is set up. We are working on a solution/a way around it. Thanks.
     
    vd_unity and jbassking like this.
  7. jbassking

    jbassking

    Joined:
    Feb 27, 2014
    Posts:
    106
  8. thanyalukj_unity

    thanyalukj_unity

    Unity Technologies

    Joined:
    Apr 4, 2020
    Posts:
    7
    Hi @jbassking, We managed to fix it and will release the new version in the coming weeks. The release process may take time. I'll give you another update once we release it. Thank you.
     
  9. thanyalukj_unity

    thanyalukj_unity

    Unity Technologies

    Joined:
    Apr 4, 2020
    Posts:
    7
    Hi @jbassking,

    Is there a reason to make these calls on the second scene rather than the first scene right after initialising Analytics Package?
    `
    PushNotificationsService.Instance.OnRemoteNotificationReceived += Instance_OnNotificationReceived;
    and
    await PushNotificationsService.Instance.RegisterForPushNotificationsAsync();
    `

    I'm looking into simplify the workflow in the SDK that PushNotificationsService.Instance.OnRemoteNotificationReceived is called before before
    RegisterForPushNotificationsAsync.

    This is what it will look like

    Code (CSharp):
    1.  
    2. await UnityServices.InitializeAsync();
    3. await AnalyticsService.Instance.CheckForRequiredConsents();
    4. PushNotificationsService.Instance.OnRemoteNotificationReceived += Instance_OnNotificationReceived;
    5.  
    6. try {
    7.     string token = await PushNotificationsService.Instance.RegisterForPushNotificationsAsync();
    8. }
    9. catch (Exception e)
    10. {
    11.     throw;
    12. }
    13.  

    Will it affect your game?

    Thank you.
    Thanyaluk