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: How do I create a local notification through Unity? Where would that script/section of code be placed in the scene? What does the syntax look like to customize the local notification?
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.
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): IEnumerator Start() { // NotificationServices.CancelAllLocalNotifications (); // To cancel all Notifications (If Needed). #if UNITY_IOS UnityEngine.iOS.NotificationServices.RegisterForNotifications (UnityEngine.iOS.NotificationType.Alert ); UnityEngine.iOS.NotificationServices.RegisterForNotifications (UnityEngine.iOS.NotificationType.Badge ); UnityEngine.iOS.NotificationServices.RegisterForNotifications (UnityEngine.iOS.NotificationType.Sound ); #endif yield return new WaitForSeconds (10f); #if UNITY_IOS ScheduleNotificationForiOSWithMessage ("It Works!", System.DateTime.Now.AddSeconds (5f)); #endif } void ScheduleNotificationForiOSWithMessage (string text, System.DateTime fireDate) { if (Application.platform == RuntimePlatform.IPhonePlayer) { print ("Scheduling....."); UnityEngine.iOS.LocalNotification notification = new UnityEngine.iOS.LocalNotification (); notification.fireDate = fireDate; notification.alertAction = "Alert"; notification.alertBody = text; notification.hasAction = false; UnityEngine.iOS.NotificationServices.ScheduleLocalNotification (notification); } }
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): var types = NotificationServices.enabledNotificationTypes; 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): NotificationServices.RegisterForNotifications(NotificationType.Alert|NotificationType.Badge|NotificationType.Sound); 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): if((types&NotificationType.Alert)!=0) { // Alert type enabled } if((types&NotificationType.Badge)!=0) { // Badge type enabled } if((types&NotificationType.Sound)!=0) { // Sound type enabled } Next, schedule an alert notification then call this. Code (CSharp): var count = NotificationServices.scheduledLocalNotifications.Length; print(count); 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): var count = NotificationServices.localNotificationCount; print(count);
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?
I've heard of this plugin. Not used it myself though. https://github.com/HuaYe1975/UnityStatusBarForiOS