PushNotificationsService OnNotificationReceived not being called

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

async void Start()
    {
        await UnityServices.InitializeAsync();
        await Unity.Services.Analytics.AnalyticsService.Instance.CheckForRequiredConsents();
    }

In another scene after the user logs into our app.

private void OnEnable()
{     
     PushNotificationsService.Instance.OnNotificationReceived += Instance_OnNotificationReceived;

     _ = RegisterForNotifications();
}

public static void Instance_OnNotificationReceived(System.Collections.Generic.Dictionary<string, object> obj)
{
     Debug.Log($"Recevied notification: {JsonConvert.SerializeObject(obj)}");
}

private async Task RegisterForNotifications()
{
    try
    {
        string pushToken = await PushNotificationsService.Instance.RegisterForPushNotificationsAsync();
        await SaveToken(pushToken);               
    }
    catch (Exception e)
    {
        Debug.Log("Failed to retrieve a push notification token.");
    }

}

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.

I tried all the different cases, 1-3 all with the same results.

Thanks for more information. We are looking into it.

I’m using Unity 2022.3.4f1 with XCode 14.3.1

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.

2 Likes

Hi @thanyalukj_unity

Were you able to make any progress?

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.

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

await UnityServices.InitializeAsync();
await AnalyticsService.Instance.CheckForRequiredConsents();
PushNotificationsService.Instance.OnRemoteNotificationReceived += Instance_OnNotificationReceived;

try {
    string token = await PushNotificationsService.Instance.RegisterForPushNotificationsAsync();
}
catch (Exception e)
{
    throw;
}

Will it affect your game?

Thank you.
Thanyaluk

  • Is there an update? The question remains