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

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

1 Like

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.

void _EnableLocalNotificationIOS8()
{
    UIApplication *app = [UIApplication sharedApplication];

    if ([app respondsToSelector:@selector(registerUserNotificationSettings:)])
    {
        UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil];
        [app registerUserNotificationSettings:settings];
        [app registerForRemoteNotifications];
    }
}

It should not be necessary to do this manually, but I guess it’s required until Unity fixes their Notification API.

1 Like

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?

I just did it:

extern "C" {
    void *_EnableLocalNotificationIOS8();
}

void *_EnableLocalNotificationIOS8()
{
    UIApplication *app = [UIApplication sharedApplication];
  
    if ([app respondsToSelector:@selector(registerUserNotificationSettings:)])
    {
        UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil];
        [app registerUserNotificationSettings:settings];
        [app registerForRemoteNotifications];
    }
}

Then in Unity create a script and do:

[DllImport("__Internal")]
private static extern void _EnableLocalNotificationIOS8();

void RegisterNotificationService(){
_EnableLocalNotificationIOS8();
}
2 Likes

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?

Do you still have this problem with 4.5.5p1 ?

I have this issue with 4.5.5p3. Any solutions?

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.

1 Like

This is still not fixed. Just tested with the sample code from the documentation in Unity 4.6.0f3 with iOS 8.
http://docs.unity3d.com/ScriptReference/NotificationServices.ScheduleLocalNotification.html

Pretty sure most of the iOS fixes from 4.5.5f* are supposed to come in 4.6.1, not 4.6.0, according to this: http://blogs.unity3d.com/2014/11/24/sustained-engineering-plan-for-unity-4-5-and-4-6/

Ahh I see! Good to know! Hm, think I have checked most of the 4.5-versions since iOS 8 release with the same results though… Has anyone got local notifications to work with any Unity version on iOS 8?

We got it working using code similar to liven’s… although it’s ridiculous that Unity hasn’t fixed this themselves.

I hear you! It was 2.5 months since iOS8 was released…

How do you name these files and where do you put them?

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.

@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.

Damn! I have this issue in unity 4.6.1p1 (latest patch build)
I’m getting a decent list of Xcode alterations now :frowning:
Thankfully liven’s code looks like it’s working for me.

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.

2 Likes

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+!

@Mantas-Puida this problem still exists in Unity 4.6.1p3 =/

And, yes, @liven 's fix is working! Thanks!