Eduardo Coelho
Eduardo Coelho

Reputation: 1963

Detecting whether iOS App was launched by a Local Notification in Unity

I'm using the NotificationServices to schedule local notifications for my Unity game on the iOS platform. I'm successfully scheduling and presenting the local notifications, however I haven't find a way to detect whether the user has opened (launched or resumed) my app as a result of opening the local notification (not the app directly).

I tried to check for NotificationServices.localNotificationCount > 0 in OnApplicationPause(false) (when the app is entering foreground) but sometimes NotificationServices.localNotificationCount is > 0 even though the user hasn't opened the local notification (maybe NotificationServices.localNotificationCount was updated before, when a local notification was received when the app was in foreground, etc.).

I need a safe way to detect that my app was opened by a local notification. Does anyone know how can I achieve that in Unity?

Upvotes: 3

Views: 1982

Answers (1)

Hugo Scott-Slade
Hugo Scott-Slade

Reputation: 897

I was having issues finding a reliable way to do this so I just wrote one. It's not the most advanced but you can have it for free.

I have a class called GLTNotificationResponder I wrote, just put the two files (.h + .m) in Assets/Plugins/iOS inside your Unity project.

You'll have to add a few lines to UnityAppController.mm inside XCode, but only if you do a fresh build ( either new build or a 'REPLACE' ), not for subsequent rebuilds of an app.

And in Unity, create a GameObject and add NotificationResponder.cs to it.

I hope it helps!


http://pastebin.com/v2DLcZb3 - GLTNotificationResponder.h

http://pastebin.com/tupUvABx - GLTNofiticationResponder.m

http://pastebin.com/CC6qA7Sz - NotificationResponder.cs

http://pastebin.com/PuyDtL5z - UnityAppController.mm changes


Upvotes: 2

Related Questions