Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

local notification not working in ios 8. #unity4.5.4 #Xcode6.0.1

Discussion in 'iOS and tvOS' started by liven410, Sep 30, 2014.

  1. liven410

    liven410

    Joined:
    Dec 23, 2010
    Posts:
    36
    Hi

    I was using unity's "LocalNotification" class to create a local notification and "NotificationServices" class to schedule notification.

    My code works fine on ios 7 or ios 6 and both local and remote notifications works.
    BUT on ios8 both the local and remote notifications are not coming.
    And while scheduling a local notification on ios 8, I get the following warning in the console log.

    Below is the console log from xcode 6.0.1
    2014-09-30 16:49:04.144 abcapp[588:79364] Attempting to schedule a local notification <UIConcreteLocalNotification: 0x1b0cf6a0>{fire date = Tuesday, 30 September 2014 4:49:34 pm India Standard Time, time zone = (null), repeat interval = 0, repeat count = UILocalNotificationInfiniteRepeatCount, next fire date = Tuesday, 30 September 2014 5:49:34 pm India Standard Time, user info = {
    "id" : "currency_offer"
    }} with an alert but haven't received permission from the user to display alerts
    -> applicationWillResignActive()
    -> applicationDidEnterBackground()

    Do anybody know, how to fix this, any way to give permission for notification on ios 8?

    Thanks
    liven
     
    WalidKhairy likes this.
  2. Micke_D

    Micke_D

    Joined:
    Jul 29, 2013
    Posts:
    1
    I solved the issue by calling the following native plugin code when the app is starting.

    This code must be placed inside an .m or .mm file in the Assets/Plugins/iOS folder.

    Code (csharp):
    1.  
    2. void _EnableLocalNotificationIOS8()
    3. {
    4.     UIApplication *app = [UIApplication sharedApplication];
    5.  
    6.     if ([app respondsToSelector:@selector(registerUserNotificationSettings:)])
    7.     {
    8.         UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil];
    9.         [app registerUserNotificationSettings:settings];
    10.         [app registerForRemoteNotifications];
    11.     }
    12. }
    13.  
    It should not be necessary to do this manually, but I guess it's required until Unity fixes their Notification API.
     
    CorwinOfAmberX likes this.
  3. siradam

    siradam

    Joined:
    Jan 26, 2014
    Posts:
    6
    This is a bit confusing for someone who has never done any native plugin development. I followed your link and am referring to the Bonjour example found there. I'm pretty sure you're not saying I just need to put that code into a .m or .mm file in Assets/Plugins/iOS because there's still more I need to do to connect all the dots right? It's not entirely clear to me just from your post and from the documentation in that link how it all fits together. Can you give some more info on this?
     
  4. Joskym

    Joskym

    Joined:
    Oct 2, 2013
    Posts:
    39
    I just did it:

    Code (ObjC):
    1. extern "C" {
    2.     void *_EnableLocalNotificationIOS8();
    3. }
    4.  
    5. void *_EnableLocalNotificationIOS8()
    6. {
    7.     UIApplication *app = [UIApplication sharedApplication];
    8.  
    9.     if ([app respondsToSelector:@selector(registerUserNotificationSettings:)])
    10.     {
    11.         UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil];
    12.         [app registerUserNotificationSettings:settings];
    13.         [app registerForRemoteNotifications];
    14.     }
    15. }
    Then in Unity create a script and do:
    Code (CSharp):
    1. [DllImport("__Internal")]
    2. private static extern void _EnableLocalNotificationIOS8();
    3.  
    4. void RegisterNotificationService(){
    5. _EnableLocalNotificationIOS8();
    6. }
     
    hamokshaelzaki and WalidKhairy like this.
  5. dttngan91

    dttngan91

    Joined:
    Nov 21, 2013
    Posts:
    80
    I don't get it! What is the difference between ios 8 and ios 6,7? Why can't we use LocalNotification built-in Unity rather than writing it via native code?
     
  6. Mantas-Puida

    Mantas-Puida

    Joined:
    Nov 13, 2008
    Posts:
    1,864
    Do you still have this problem with 4.5.5p1 ?
     
  7. thinkerton5

    thinkerton5

    Joined:
    Apr 16, 2014
    Posts:
    5
    I have this issue with 4.5.5p3. Any solutions?
     
  8. liven410

    liven410

    Joined:
    Dec 23, 2010
    Posts:
    36
    FIXED!

    Hi friends, I fixed the issue by doing some little editing to the UnityAppController.mm in the exported xcode project.

    The steps are :
    Step-1:
    Open UnityAppController.mm and locate the function “didFinishLaunchingWithOptions”

    Step-2:
    Inside the “DidFinishLaunchingWithOptions” function. Add the following lines at the beginning of the function.
    -----------------
    float version = [[[UIDevice currentDevice] systemVersion] floatValue];
    if (version >= 8.0)
    {
    //printf_console("---> Running on iOS 8.0 > didFinishLaunchingWithOptions() Added my code //av \n");
    UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes: (UIRemoteNotificationTypeBadge
    |UIRemoteNotificationTypeSound
    |UIRemoteNotificationTypeAlert) categories:nil];
    [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
    }

    -----------------

    Step-3:
    Add the below function next to where the “DidFinishLaunchingWithOptions” function ends.
    -----------------
    - (void)application: (UIApplication *)application didRegisterUserNotificationSettings: (UIUserNotificationSettings *)notificationSettings
    {
    float version = [[[UIDevice currentDevice] systemVersion] floatValue];
    if (version >= 8.0)
    {
    //printf_console("---> didRegisterUserNotificationSettings() //av \n");
    [application registerForRemoteNotifications];
    }
    }

    -----------------

    I am using unity 4.5.5f1 & Xcode 6.1. And tested this on ios 8.0 and 8.1.


    Thanks,
    liven.
     
    Last edited: Nov 6, 2014
    laurentlavigne likes this.
  9. iamanders

    iamanders

    Joined:
    Oct 19, 2014
    Posts:
    5
  10. MattRix

    MattRix

    Joined:
    Aug 23, 2011
    Posts:
    121
  11. iamanders

    iamanders

    Joined:
    Oct 19, 2014
    Posts:
    5
  12. MattRix

    MattRix

    Joined:
    Aug 23, 2011
    Posts:
    121
    We got it working using code similar to liven's... although it's ridiculous that Unity hasn't fixed this themselves.
     
  13. iamanders

    iamanders

    Joined:
    Oct 19, 2014
    Posts:
    5
    I hear you! It was 2.5 months since iOS8 was released..
     
  14. Umai

    Umai

    Joined:
    Jun 18, 2013
    Posts:
    74
    How do you name these files and where do you put them?
     
  15. Joskym

    Joskym

    Joined:
    Oct 2, 2013
    Posts:
    39
    You can name the Objective C file "whateveryouwant.mm" and you must put it in Assets/Plugins/iOS.
    You can name the CSharp file "whateveryouwant.cs" and place it where ever you want.
    You just have to make RegisterNotificationService public and static and call it when you cant to register for notification.
     
  16. andymads

    andymads

    Joined:
    Jun 16, 2011
    Posts:
    1,614
    @Mantas Puida I have this issue with 4.5.5p5 but it only happens on one of two apps. The other app (a simple notifications test app) works fine. Both apps built this morning with 4.5.5p5. The problem app does not show up in Settings|Notifications.

    Reported (Case 654628)

    EDIT:

    Was emailed to say that the bug was reproduced and would be passed on to developers.
     
    Last edited: Dec 17, 2014
  17. petey

    petey

    Joined:
    May 20, 2009
    Posts:
    1,816
    Damn! I have this issue in unity 4.6.1p1 (latest patch build)
    I'm getting a decent list of Xcode alterations now :(
    Thankfully liven's code looks like it's working for me.
     
  18. Mantas-Puida

    Mantas-Puida

    Joined:
    Nov 13, 2008
    Posts:
    1,864
    Thanks! We identified that there is missing extra API in our NotificationServices API when application is using only local notifications. We are working on a fix.
     
    TeorikDeli and andymads like this.
  19. ImranZahid

    ImranZahid

    Joined:
    Aug 14, 2012
    Posts:
    8
    I've implemented it and it is working fine but sound is not coming. I've added permission for sound as well but still no luck. Its only happening in ios8+!

     
  20. TeorikDeli

    TeorikDeli

    Joined:
    Apr 6, 2014
    Posts:
    146
    @Mantas Puida this problem still exists in Unity 4.6.1p3 =/
    And, yes, @...liven... 's fix is working! Thanks!
     
  21. Mantas-Puida

    Mantas-Puida

    Joined:
    Nov 13, 2008
    Posts:
    1,864
    New method NotificationServices.RegisterForLocalNotificationTypes was added to this patch release, you should call it before scheduling first notification.
     
  22. andymads

    andymads

    Joined:
    Jun 16, 2011
    Posts:
    1,614
    Do you know that this isn't stated in the release notes?

    Will this fix be included in the next point release?
     
  23. TeorikDeli

    TeorikDeli

    Joined:
    Apr 6, 2014
    Posts:
    146
    Thank you! Also it's missing in the documentation too (at least, I couldn't find it). Is it similar to NotificationServices.RegisterForRemoteNotificationTypes ?
     
  24. rainbowegg

    rainbowegg

    Joined:
    May 6, 2014
    Posts:
    12
    RegisterForLocalNotificationTypes doesn't appear to be working for me.

    In one of my game's Start() functions, I'm calling this:

    Code (CSharp):
    1.         NotificationServices.RegisterForLocalNotificationTypes(LocalNotificationType.Alert);
    2.         NotificationServices.RegisterForLocalNotificationTypes(LocalNotificationType.Badge);
    And then later, when the event that needs notifications happens, I have this:
    Code (CSharp):
    1.         var newNotif = new LocalNotification();
    2.         newNotif.fireDate = DateTime.Now + CalculateTimeToReturn();
    3.         newNotif.alertBody = currentHero.Name + "has returned home!";
    4.         newNotif.applicationIconBadgeNumber = 1;
    5.         NotificationServices.ScheduleLocalNotification(newNotif);
    When I build and run on iOS, I get a message saying "Game would like to send you notifications Ok/Cancel". So far, so good. However, when I perform the action that sends the notification, I get the same Xcode error that everyone's been getting above about not having permission. If I check the app in the Notification Center, Allow Notification is true. Badge App Icon is set to false, and if I set it to true, it shows icon on the app. I'm not getting any alerts though.

    Thoughts? Still a bug or am I missing something?

    Thanks!
     
  25. andymads

    andymads

    Joined:
    Jun 16, 2011
    Posts:
    1,614
    Try combining the flags.

    Code (CSharp):
    1. NotificationServices.RegisterForLocalNotificationTypes(LocalNotificationType.Alert|LocalNotificationType.Badge);
     
    rainbowegg likes this.
  26. Mantas-Puida

    Mantas-Puida

    Joined:
    Nov 13, 2008
    Posts:
    1,864
    Yes, we missed to add that to Release Notes. Next patch-release will have it in notes.
     
  27. rainbowegg

    rainbowegg

    Joined:
    May 6, 2014
    Posts:
    12
    That was it, thank you! I did not realize they were flags.
     
  28. andymads

    andymads

    Joined:
    Jun 16, 2011
    Posts:
    1,614
    I actually meant will the fix be in the next non-patch version, e.g. 4.6.2?
     
  29. Mantas-Puida

    Mantas-Puida

    Joined:
    Nov 13, 2008
    Posts:
    1,864
    All the patch release fixes are brought to next public release, including this one.
     
  30. fafase

    fafase

    Joined:
    Jul 3, 2012
    Posts:
    161
    I get an error stating the method does not exist.

    NotificationServices.RegisterForLocalNotificationTypes(LocalNotificationType.Alert|LocalNotificationType.Badge);

    It is in MonoBehaviour class with the using UnityEngine;
    I can use the remote version of the same method but Local goes wrong.

    Wondering how others got it working. I have the latest update 4.6.1f1.

    Missing something?

    EDIT: Yep I was missing the fact that it is part of a recent patch...but I still cna't seem to get it working.

    here is what I have:
    Code (CSharp):
    1.  
    2. void Awake(){
    3.     NotificationServices.RegisterForLocalNotificationTypes(LocalNotificationType.Alert|LocalNotificationType.Badge);
    4.      }
    5. void Start(){
    6.    Helper.DisplayNotification("Message", this);
    7. }
    8.  
    9. public static class Helper{
    10. #if !UNITY_WEBPLAYER
    11.      public static void DisplayNotification(string message, MonoBehaviour obj){
    12.         LocalNotificationnotif = newLocalNotification();
    13.         notif.alertBody = message;
    14.         NotificationServices.PresentLocalNotificationNow(notif);
    15.         Debug.Log ("Calling");
    16.         obj.StartCoroutine(NotificationCoroutine());
    17.      }
    18.      staticIEnumeratorNotificationCoroutine(){
    19.            Debug.Log("Coroutine running");
    20.            yield return null;  // Placed that one in case the running of the alert would happen late
    21.            while(NotificationServices.localNotificationCount > 0){
    22.                 Debug.Log("In here");
    23.                 yield return null;
    24.            }
    25.            Debug.Log("Closing");
    26.            NotificationServices.CancelAllLocalNotifications();
    27.       }
    28. #endif
    29. }
     
    Last edited: Jan 22, 2015
  31. rainbowegg

    rainbowegg

    Joined:
    May 6, 2014
    Posts:
    12
    What's not working exactly? What's the output? NotificationServices.localNotificationCount is the number of received LocalNotifications, not scheduled ones. CancelAllLocalNotifications cancels scheduled ones. Since you use PresentLocalNotificationNow the call to CancelAllLocalNotifications isn't doing anything.
     
  32. fafase

    fafase

    Joined:
    Jul 3, 2012
    Posts:
    161
    Yep so, I don't see any alert on screen, and the count remains 0. So it just close the coroutine and I do not egt the alert.

    Cheers.
     
  33. andymads

    andymads

    Joined:
    Jun 16, 2011
    Posts:
    1,614
    @fafase if you use code tags when posting code it'll be a lot easier for people to read and you may be more likely to get help.
     
  34. fafase

    fafase

    Joined:
    Jul 3, 2012
    Posts:
    161
    Yep I was looking for the 010101 button, figured out it was tag in here.
     
  35. Mantas-Puida

    Mantas-Puida

    Joined:
    Nov 13, 2008
    Posts:
    1,864
    I think you should yield at least for one frame between registering for notifications and scheduling one.
     
  36. fafase

    fafase

    Joined:
    Jul 3, 2012
    Posts:
    161
    Well, I actually figured out that local notification is not what I am after...
    I wanted to display an alert in app but those seem to be notification once you quit the app.

    So all in all, I realised I got it working but I need something else.
     
  37. andymads

    andymads

    Joined:
    Jun 16, 2011
    Posts:
    1,614
    @Mantas Puida can you clarify this?

    The docs for RegisterForLocalNotificationTypes state:

    Note: calling this method will set the requested notification types for both local and remote notifications, overriding any previous calls. Call to NotificationServices.RegisterForRemoteNotificationTypes() is still required to enable remote notifications.

    The docs for RegisterFoRemoteNotificationTypes state:

    Note: calling this method will set the requested notification types for both local and remote notifications, overriding any previous calls.

    So basically only one or the other is needed if both local and remote are to be registered?

    Would it be more appropriate to have a single RegisterForNotificationTypes method as they both do the same thing now?

    Also could enabledRemoteNotificationTypes be simply enabledNotificationTypes?
     
  38. Mantas-Puida

    Mantas-Puida

    Joined:
    Nov 13, 2008
    Posts:
    1,864
    Yes, if your app is using both types of notifications calling any of them is enough.

    Yes, that's what we did for Unity 5.0. Though Unity 4.x lacks script updater, so we had to go minor API tweak rather than bigger refactoring.
     
    andymads likes this.
  39. andymads

    andymads

    Joined:
    Jun 16, 2011
    Posts:
    1,614
    @Mantas Puida, I've identified more issues with the local notifications (in 4.6.2).

    (1) When the unlocked alert style is set to none in settings there is an error and a notification is not scheduled even though it should still be possible to see it when the device is locked.

    Code (CSharp):
    1. Attempting to schedule a local notification <UIConcreteLocalNotification: 0x187c5f70>{fire date = Monday, 9 February 2015 08:46:29 Greenwich Mean Time, time zone = (null), repeat interval = 0, repeat count = UILocalNotificationInfiniteRepeatCount, next fire date = Monday, 9 February 2015 08:46:29 Greenwich Mean Time, user info = {
    2.     ID = 6;
    3.     Timestamp = "02/09/2015 08:46:19";
    4. }} with an alert but haven't received permission from the user to display alerts
    IMG_0219 (1).jpg

    (2) enabledRemoteNotificationTypes is returning RemoteNotificationType.None even after calling RegisterForLocalNotificationTypes, yet notifications are enabled in settings.

    Also, can I suggest that the docs expand on what's valid for userInfo dictionary key/value types.
     
    Last edited: Feb 9, 2015
  40. Mantas-Puida

    Mantas-Puida

    Joined:
    Nov 13, 2008
    Posts:
    1,864
    Could you please post some code how you are doing this?
     
  41. andymads

    andymads

    Joined:
    Jun 16, 2011
    Posts:
    1,614
    @Mantas Puida, I assume you're referring to issue (1).

    This is pretty much the simplest code that will do it.

    Code (CSharp):
    1.         LocalNotification n = new LocalNotification();
    2.         n.fireDate = DateTime.Now.AddSeconds(fireInSeconds);
    3.         n.alertBody = "Simple";
    4.         n.hasAction = false;
    5.         NotificationServices.ScheduleLocalNotification(n);
    6.  
    I've submitted a bug with a test project and clear steps to reproduce.

    Bug #672014

    In that test app you'll also see issue (2):
    NotificationServices.enabledRemoteNotificationTypes is returning None (second and third lines of text in GUI). I was under the impression local and remote were the same now?

    I've submitted a separate bug report for this. #672021.
     
    Last edited: Feb 13, 2015
  42. hamokshaelzaki

    hamokshaelzaki

    Joined:
    Nov 6, 2012
    Posts:
    19
    That was very helpful,
    We placed the Objective-C code in the UnityAppController.mm (anywhere)
    Additionally we called the Unity's Script function "RegisterNotificationService()" in the "Start()" routine (In case anyone was wondering)

     
  43. DarkPixel

    DarkPixel

    Joined:
    Sep 13, 2013
    Posts:
    79
    It seem fixed in 4.6.2 but I can't find release notes for Unity5, it's fixed too?
     
  44. laurentlavigne

    laurentlavigne

    Joined:
    Aug 16, 2012
    Posts:
    6,203
    Thanks .. liven..

    What's super weird is my test app works without this .mm editing.
     
  45. Mantas-Puida

    Mantas-Puida

    Joined:
    Nov 13, 2008
    Posts:
    1,864
    DarkPixel likes this.
  46. at0mbomb

    at0mbomb

    Joined:
    May 20, 2014
    Posts:
    1
    I'm using version 4.6.4f1 on iOS device using iOS 8.1.2. I'm running into issues trying to get the local notifications to work. In my Awake method, I have the following:

    Code (CSharp):
    1.  
    2. public void Awake()
    3. {
    4.         NotificationServices.RegisterForRemoteNotificationTypes (RemoteNotificationType.Alert | RemoteNotificationType.Badge | RemoteNotificationType.Sound);
    5.             NotificationServices.RegisterForLocalNotificationTypes(LocalNotificationType.Alert | LocalNotificationType.Badge | LocalNotificationType.Sound);
    6. }
    7.  
    When the app goes to background, I create the local notification and schedule it as follows:

    Code (CSharp):
    1.  
    2. LocalNotification notification = new LocalNotification();
    3. notification.alertBody = "World";
    4. notification.alertAction = "Hello";
    5. notification.applicationIconBadgeNumber = 1;
    6. notification.hasAction = true;
    7. notification.fireDate = DateTime.Now.AddSeconds(totalTime);
    8. NotificationServices.ScheduleLocalNotification(notification);
    9.  
    The end result is that the local notification never fires, and, just as importantly, the prompt to allow notifications is never displayed. If I go into Settings > Notifications > My App > Allow Notifications, then I'll start to see them.

    Shouldn't there be a prompt allowing the user to choose whether or not they want to see notifications? Am I doing something else wrong?

    Thanks.

    HM
     
  47. Mantas-Puida

    Mantas-Puida

    Joined:
    Nov 13, 2008
    Posts:
    1,864
    If at least once user clicked "not allow" for notifications then no prompts will be shown in the future. If you want to cleanly test this, you need to manually remove app from device and then redeploy it from Xcode.
     
    kreso likes this.
  48. AtticusMarkane

    AtticusMarkane

    Joined:
    Aug 6, 2014
    Posts:
    18
    Is there a way to query NotificationServices to determine if the user has allowed the use of local notifications?

    Also, does anyone know of a way to reset a device so that I can test the requesting of notification permissions again?
     
    Last edited: May 4, 2015
  49. Tarenar

    Tarenar

    Joined:
    Sep 20, 2013
    Posts:
    2
    I'd also like to know if we can query NotificationServices to see if the user has allowed local notifications. I need to know this so I can allow the user to turn off local notifications in the settings

    Also, I do not get the notification permission again even if I remove my app from my iphone and redeploy from Xcode
     
  50. NHSeyha

    NHSeyha

    Joined:
    Dec 7, 2015
    Posts:
    2
    publicstaticvoidScheduleNotificationInNextMinutes (intminutes) {
    LocalNotificationIphonenotifi=newLocalNotificationIphone();
    UnityEngine.iOS.NotificationTypenotifiType=newUnityEngine.iOS.NotificationType();
    notifiType = UnityEngine.iOS.NotificationType.Alert;
    notifiType = UnityEngine.iOS.NotificationType.Badge;
    notifiType = UnityEngine.iOS.NotificationType.Sound;

    //notifi.userInfo=

    notifi.alertAction="OK";
    notifi.alertBody="Test notificaiton in " + minutes + " mn.";
    notifi.fireDate=System.DateTime.Now.AddMinutes(minutes);
    notifi.applicationIconBadgeNumber= -1;
    notifi.soundName=LocalNotificationIphone.defaultSoundName;

    NotificationServices.RegisterForNotifications(notifiType);
    NotificationServices.ScheduleLocalNotification(notifi);
    }