Local Push Notifications for iOS: Unity3D

Local Push Notifications for iOS: Unity3D

Unity3D comes with an in-built setup for Local Push Notifications for iOS which is easy and efficient.

With local push notifications, your app arranges the notification details locally and passes those details to the system which then handles the delivery of the notification when your app is not running i.e. it is not in foreground.

Let us learn how we can integrate notifications in Unity3D and compel the users to come back and play!


Register a Notification

The user would not receive any notification unless he registers in the app to receive it. So, make sure you register the user before timing a push notification.

using NotificationServices = UnityEngine.iOS.NotificationServices; 
using NotificationType = UnityEngine.iOS.NotificationType; 

void registerNotificaton()
{
     NotificationServices.RegisterForNotifications(NotificationType.Alert | NotificationType.Badge | 
                                                      NotificationType.Sound);
}

Here, Alert lets you display messages directly to the user. It appears at the top of the screen while the device is in use and stays there until manually dismissed.
Badges always contain a numerical value and are commonly used to indicate the number of notifications that the user has still not seen.

image: ../Art/badged_app.jpg
App icon with badge

The Sound indicates that notification has is an alert sound.

Schedule a Notification

What is scheduling? It means to time notification to appear after a certain time interval. After scheduling a notification, the system takes on the responsibility of delivering the notification to the user at the appropriate time. Your app does not need to be running for a notification to be delivered.

 void scheduleNotification
 {
     #if UNITY_IOS && !UNITY_EDITOR
     UnityEngine.iOS.LocalNotification notification = new UnityEngine.iOS.LocalNotification();
     notification.fireDate = DateTime.Now.AddHours(24);
     notification.alertBody = message;
               UnityEngine.iOS.NotificationServices.ScheduleLocalNotification(notification);
     #endif
 }

To access all the notifications that have been received by the app, we make use of localNotifications property.

Adding Custom Property to Notification

To add custom information to a notification, for example, an id, make use of userInfo property. This property is of type IDictionary. Here is an example:

     IDictionary newDict = new Dictionary<string, string>();
     newDict.Add("ID", id.ToString());
     notification.userInfo = newDict as IDictionary;

This property can be used in canceling the notification which we would get to know subsequently.

Cancel Notifications

We may need to cancel all the notifications or a specific notification. To cancel the delivery of all scheduled local notifications, use:

UnityEngine.iOS.NotificationServices.CancelAllLocalNotifications();

In order to cancel a specific notification, we can make use of the userInfo property as stated above. It can be done as follows:

 foreach (UnityEngine.iOS.LocalNotification notif in UnityEngine.iOS.NotificationServices.scheduledLocalNotifications)
        { 
            if (notif.userInfo["ID"].Equals(id.ToString()))
            {
                UnityEngine.iOS.NotificationServices.CancelLocalNotification(notif);
            }
        }

Note: CancelAllLocalNotifications or CancelLocalNotification do not clear those notifications from the screen that had been already delivered. They only cancel the notifications that are yet to be pushed.

That’s all with push notifications! Happy pushing!

Share This Post

2 Responses to "Local Push Notifications for iOS: Unity3D"

    1. Hi Jeff
      Yes, this is an inbuilt method for Unity iOS. You do not need to add separate packages.
      This method doesn’t work for Android. Maybe that is the reason that people use external packages. Also to skip writing codes, maybe!

      Reply

Post Comment