Search Unity

iOS Local Notifications

Discussion in 'iOS and tvOS' started by lweller2020, Jun 30, 2017.

  1. lweller2020

    lweller2020

    Joined:
    Jun 19, 2017
    Posts:
    2
    I also posted this question to Stack Overflow at:

    https://stackoverflow.com/questions/44835507/local-notification-in-unity

    I'm currently developing an application in Unity (to be built in iOS) in which I would like a local notification to appear when a certain in-game event is triggered. But before I set up that trigger event, I'd like to test that I can create a local notification and customize the various components.

    Right now I'm stuck on creating a local notification in Unity of any kind.

    These two documentation pages on the Unity website seem to contain information pertaining to my situation, but NO explanation is included, and I have limited C# experience:

    https://docs.unity3d.com/ScriptReference/iOS.LocalNotification.html

    https://docs.unity3d.com/ScriptReference/iOS.NotificationServices.ScheduleLocalNotification.html

    Specific Questions:

    1. How do I create a local notification through Unity?

    2. Where would that script/section of code be placed in the scene?

    3. What does the syntax look like to customize the local notification?
     
  2. andymads

    andymads

    Joined:
    Jun 16, 2011
    Posts:
    1,614
    1. There is a code sample in the docs.

    2. Put the code wherever you want. At the basic level all you're doing is instantiating an object and calling a static method.

    3. What sort of customisation are you referring to? See the docs for the list of variables.

    Note that you'll need to call RegisterForNotifications first to bring up the native iOS popup which asks you if you want notifications.
     
  3. rahul_noob

    rahul_noob

    Joined:
    Oct 3, 2017
    Posts:
    26
    Hi, I tried this for many time but didn't get any response from the device, I'm scheduling the notification but not receiving any drop alert on my iPhone.

    This is my Code:

    Code (CSharp):
    1. IEnumerator Start()
    2.     {
    3. //        NotificationServices.CancelAllLocalNotifications ();      // To cancel all Notifications (If Needed).
    4.         #if UNITY_IOS
    5.         UnityEngine.iOS.NotificationServices.RegisterForNotifications (UnityEngine.iOS.NotificationType.Alert );
    6.         UnityEngine.iOS.NotificationServices.RegisterForNotifications (UnityEngine.iOS.NotificationType.Badge );
    7.         UnityEngine.iOS.NotificationServices.RegisterForNotifications (UnityEngine.iOS.NotificationType.Sound );
    8.         #endif
    9.         yield return new WaitForSeconds (10f);
    10.         #if UNITY_IOS
    11.         ScheduleNotificationForiOSWithMessage ("It Works!", System.DateTime.Now.AddSeconds (5f));
    12.         #endif
    13.     }
    14.     void ScheduleNotificationForiOSWithMessage (string text, System.DateTime fireDate)
    15.     {
    16.         if (Application.platform == RuntimePlatform.IPhonePlayer) {
    17.             print ("Scheduling.....");
    18.             UnityEngine.iOS.LocalNotification notification = new UnityEngine.iOS.LocalNotification ();
    19.             notification.fireDate = fireDate;
    20.             notification.alertAction = "Alert";
    21.             notification.alertBody = text;
    22.             notification.hasAction = false;
    23.             UnityEngine.iOS.NotificationServices.ScheduleLocalNotification (notification);
    24.         }          
    25.     }
     
  4. andymads

    andymads

    Joined:
    Jun 16, 2011
    Posts:
    1,614
    I just tested on an iOS 12 device and the local notifications worked fine.

    This is what I suggest:

    Call this first to query the current enabled notification types. The result enum value is a collection of flags.

    Code (CSharp):
    1.                 var types = NotificationServices.enabledNotificationTypes;
    2.  
    If it returns 0 i.e. NotificationType.None then either
    (1) You've never tried to register for notifications.
    (2) You have declined them when asked.
    (3) You've turned them off in Settings.

    Have a look in Settings to see if your app is listed under Notifications. If it's listed then underneath the app name it will tell you the notification types enabled (or Off).

    If your app is listed but Off then enable the notification types you want in Settings.

    If your app is not listed then call this to display the popup, supplying the types you want.

    Code (CSharp):
    1.                     NotificationServices.RegisterForNotifications(NotificationType.Alert|NotificationType.Badge|NotificationType.Sound);
    2.  
    If you accept then your app will now appear in the list in Settings.

    As I understand it, the register for notifications popup will only be shown once, no matter how many times you call it.

    Also, it does not need to be called every time you start your app, although it seems you need to call it again if you want to access the device token.

    If the returned notification types is non-zero then you can check each type like this.

    Code (CSharp):
    1.  
    2. if((types&NotificationType.Alert)!=0)
    3. {
    4.     // Alert type enabled
    5. }
    6. if((types&NotificationType.Badge)!=0)
    7. {
    8.     // Badge type enabled
    9. }
    10. if((types&NotificationType.Sound)!=0)
    11. {
    12.     // Sound type enabled
    13. }
    14.  
    Next, schedule an alert notification then call this.

    Code (CSharp):
    1. var count = NotificationServices.scheduledLocalNotifications.Length;
    2. print(count);
    3.  
    The count should be 1.

    Close your app before the fire time.

    When you see the alert click on it to return to your app.

    The scheduled count will now be 0, and if you query the received count it should be 1.

    Code (CSharp):
    1. var count = NotificationServices.localNotificationCount;
    2. print(count);
     
  5. andymads

    andymads

    Joined:
    Jun 16, 2011
    Posts:
    1,614
  6. rahul_noob

    rahul_noob

    Joined:
    Oct 3, 2017
    Posts:
    26
    Yes it's working now thank you very much!
     
  7. studentutu

    studentutu

    Joined:
    Oct 22, 2017
    Posts:
    121
    Also, could tell us if there is a built-in support for Unity player to change IOS status bar - show/hide? It is available at the build time, but is it available at runtime?
    If nothing is available, could you recommend IOS plugin that will do the job?
     
  8. andymads

    andymads

    Joined:
    Jun 16, 2011
    Posts:
    1,614
  9. studentutu

    studentutu

    Joined:
    Oct 22, 2017
    Posts:
    121