Search Unity

Other I can access local delivered notifications on iOS only if connected to xCode

Discussion in 'iOS and tvOS' started by AndreaMar, Mar 19, 2023.

  1. AndreaMar

    AndreaMar

    Joined:
    Oct 29, 2019
    Posts:
    35
    Hello devs,

    I'm scheduling local notifications on a weekly base. At the app launch, I check if the weekly notification is delivered. If so, I want to reward the player whenever he comes back to the game.

    The problem (only on iOS) is that it can successfully deliver local notifications but, apparently, it doesn't recognize delivered notifications. The strangest thing is that this works perfectly if I'm running the app from xCode, but it's not working if I'm playing the game on a iOS device while not debugging to the xCode console. So I also don't have any convenient way to debug this out.

    Here's my code. If any of you has the slightest idea of what is going on I would appreciate.

    I'm using the Mobile Notifications package, version 2.1.0, and Unity Editor 2021.3.16f1.

    Code (CSharp):
    1. // This is called when the application starts.
    2.  
    3. public async void Check ()
    4.     {
    5.         await RequestAuthorization();
    6.  
    7.         CheckForDeliveredNotifications();
    8.  
    9.         ResetWeeklyNotification();
    10.     }
    Code (CSharp):
    1. private async Task RequestAuthorization()
    2.     {
    3.         var authorizationOption = AuthorizationOption.Alert | AuthorizationOption.Badge | AuthorizationOption.Sound;
    4.         using (var req = new AuthorizationRequest(authorizationOption, true))
    5.         {
    6.             while (!req.IsFinished)
    7.             {
    8.                 await Task.Yield();
    9.             };
    10.  
    11.             string res = "\n RequestAuthorization:";
    12.             res += "\n finished: " + req.IsFinished;
    13.             res += "\n granted :  " + req.Granted;
    14.             res += "\n error:  " + req.Error;
    15.             res += "\n deviceToken:  " + req.DeviceToken;
    16.             Debug.Log(res);
    17.         }
    18.     }
    Code (CSharp):
    1. private void CheckForDeliveredNotifications ()
    2.     {
    3.         iOSNotification[] deliveredNotifications = iOSNotificationCenter.GetDeliveredNotifications();
    4.  
    5.         Notification weeklyNotification = Notifications.weeklyNotification;
    6.  
    7.         print("Checking for delivered weekly notifications...");
    8.  
    9.         for (int i = 0; i < deliveredNotifications.Length; i++)
    10.         {
    11.             // if a weekly notification has been delivered...
    12.             if (string.Compare(deliveredNotifications[i].Identifier, weeklyNotification.id) == 0)
    13.             {
    14.                 print($"Found delivered weekly notification: id: {deliveredNotifications[i].Identifier} - title: {deliveredNotifications[i].Title} - body: {deliveredNotifications[i].Body}");
    15.  
    16.                 print("Rewarding player...");
    17.                 // reward the player accordingly
    18.                 DataManager.Instance.AddCoins(weeklyNotification.reward);
    19.          
    20.                 GameObject notification = Instantiate(Resources.Load<GameObject>("Reward Notification"));
    21.                 notification.GetComponentInChildren<TextMeshProUGUI>().text = "+" + weeklyNotification.reward.ToString();
    22.                 DontDestroyOnLoad(notification);
    23.                 Destroy(notification, 5f);
    24.  
    25.                 break;
    26.             }
    27.         }
    28.  
    29.         // finally, clear all delivered notifications and badge count
    30.         iOSNotificationCenter.RemoveAllDeliveredNotifications();
    31.         iOSNotificationCenter.ApplicationBadge = 0;
    32.     }
    Code (CSharp):
    1.     private void ResetWeeklyNotification()
    2.     {
    3.         Notification weeklyNotification = Notifications.weeklyNotification;
    4.  
    5.         // clean the last scheduled notification
    6.         iOSNotificationCenter.RemoveScheduledNotification(weeklyNotification.id);
    7.  
    8.         // setup a trigger
    9.         // If the player doesn't respond to it, keep repeating every week
    10.         var trigger = new iOSNotificationTimeIntervalTrigger();
    11.         trigger.Repeats = true;
    12.         trigger.TimeInterval = weeklyNotification.timeSpan;
    13.  
    14.         var notification = new iOSNotification()
    15.         {
    16.             // You can specify a custom identifier which can be used to manage the notification later.
    17.             // If you don't provide one, a unique string will be generated automatically.
    18.             Identifier = weeklyNotification.id,
    19.             Title = weeklyNotification.title,
    20.             Body = weeklyNotification.body,
    21.             Badge = 1,
    22.             ShowInForeground = false,
    23.             SoundName = "chicken_call.caf",         // The file must be in the .caf format and located in the app's main bundle (in xCode)
    24.             Trigger = trigger
    25.         };
    26.  
    27.         iOSNotificationCenter.ScheduleNotification(notification);
    28.     }
    29. }
     
    Last edited: Mar 19, 2023
  2. AndreaMar

    AndreaMar

    Joined:
    Oct 29, 2019
    Posts:
    35
    Oh, I've discovered now the useful Console tool that I can use to see debug logs of my iPhone without necessarily running it from xCode. What is even stranger is that even with the mere Console tool it's working properly, and it's still not working if my iPhone is instead not sending logs anywhere...
     
    Last edited: Mar 19, 2023
  3. AndreaMar

    AndreaMar

    Joined:
    Oct 29, 2019
    Posts:
    35
    Update: I've found out that it actually works on device alone as well, but very rarely. :confused:
     
  4. AndreaMar

    AndreaMar

    Joined:
    Oct 29, 2019
    Posts:
    35
    Update: I had to build a custom UI Console to debug directly on the device.

    The problem is that there are no delivered notifications most of the times, even if it's scheduled, delivered and shown successfully by the iPhone.
     
    Last edited: Mar 20, 2023
  5. AndreaMar

    AndreaMar

    Joined:
    Oct 29, 2019
    Posts:
    35
    Solution: It's not a random "most of the times", but it's only when I open the app by clicking on the notification on the lockscreen. If instead I open the app from the home page the delivered notification results in the iOSNotificationCenter.

    So I've asked ChatGPT what this could be and this is the answer:
    So, if I want to know if the player has received the notification in case s/he opens the app from the lock screen notification then I should use this method instead:
    iOSNotificationCenter.GetLastRespondedNotification();


    I want to say a final thank you for the amazing support I've received in this forum and in the Answer section, which is getting increasingly animated.
     
    Last edited: Mar 20, 2023