iOS Local Notifications

520516eea711b001018c68dacd8ff0d5ff2e6db8ad5c9bc014e8ac2b48057ec7

This will only cover local notification since remote notification requires app ID and remote server notification system.

The local notification allows to remind a user about your app. A regular usage would be to warn the user of some events in your app, for instance, the player has refilled its energy or a building is fully built.

You could couple the following script with the Candy Crush regeneration system.

public enum SDNotification
{
    None, Seconds, Minutes, Hours, Days
}
public static class iOSNotification 
{
 #if UNITY_IOS
     private static string NotificationName = "NotificationName";
 #endif
     public static string GetNotificationInfo(string notifName, string info)
     {
 #if UNITY_IOS
         if(NotificationServices.localNotificationCount > 0)
         {
            UnityEngine.iOS.LocalNotification ln = null;
            for(int i = 0; i < NotificationServices.localNotifications.Length; i++)
            {
               ln = NotificationServices.GetLocalNotification(i);
               if(ln.userInfo.Contains(NotificationName) == true)
               {
                  string name = (string)ln.userInfo[NotificationName];
                  if(name == notifName){ break; }
               } 
            }
            if(ln.userInfo.Contains(info) == true) { return (string)ln.userInfo[info]; }
        }
 #endif
         return null;
     }
 #if UNITY_IOS 
     public static void RegisterNotification(NotificationType notificationType)
     {
        NotificationServices.RegisterForNotifications(notificationType);
     }
 #endif
     public static void ClearNotification(string notifName)
     {
 #if UNITY_IOS
        UnityEngine.iOS.LocalNotification ln = null;
        if(NotificationServices.localNotifications.Length > 0)
        { 
           for(int i = 0; i < NotificationServices.localNotifications.Length; i++)
           {
              ln = NotificationServices.GetLocalNotification(i);
              if(ln.userInfo.Contains(NotificationName))
              {
                   break;
              }
           }
           if(ln != null){ NotificationServices.CancelLocalNotification (ln); }
           }
 #endif
      }

      public static void RegisterLocalNotification(string notifName, int amount,
string message, SDNotification notificationType,
params KeyValuePair<string, string>[] info)
      {
 #if UNITY_IOS 
            Func<double, DateTime>func = null;
            DateTime dt = DateTime.Now;
            switch(notificationType)
            {
                case SDNotification.Seconds:
                func = dt.AddSeconds;
                break;
                case SDNotification.Minutes:
                func = dt.AddMinutes;
                break;
                case SDNotification.Hours:
                func = dt.AddHours;
                break;
                case SDNotification.Days:
                func = dt.AddDays;
                break;
            }
            UnityEngine.iOS.LocalNotification ln = null;
            if(NotificationServices.localNotifications.Length > 0)
            {
                for(int i = 0; i < NotificationServices.localNotifications.Length; i++)
                {
                   ln = NotificationServices.GetLocalNotification(i);
                   IDictionary<string,string> dict = ln.userInfo as IDictionary<string,string>;
                   string name = null; 
                   if(dict.TryGetValue(NotificationName, out name) == true)
                   {
                       if(name == notifName){ break; }
                   }
                }
                if(ln != null){ NotificationServices.CancelLocalNotification (ln); }
             } else  {
                ln = new UnityEngine.iOS.LocalNotification();
             }
             IDictionary newDict = new Dictionary<string, string>();
             newDict.Add(NotificationName, notifName);
             foreach(KeyValuePair <string,string> kvp in info) { newDict.Add(kvp.Key, kvp.Value); }
             ln.userInfo = newDict as IDictionary;
             ln.alertBody = message;
             ln.soundName = UnityEngine.iOS.LocalNotification.defaultSoundName;
             if(func!= null) { 
                 DateTime fireDate = func(amount); 
                 if(notificationType == SDNotification.Days) { fireDate = fireDate + new TimeSpan(17, 00 ,00); }
                 ln.fireDate = fireDate;
             }
             NotificationServices.ScheduleLocalNotification (ln);
 #endif
       }
}

There are only three methods, the first will register the type of notifications, whether you want the badge (red dot with number) and/or the sound (the bell) and/or the alert (the text box).

The second removes an alert that was previously registered and the last one creates a new alert. You have a limitation of 64 alerts. Above that, only the closest one in time are kept.

You can also change the sound of the alert. You would provide the name of the file to the LocalNotification object. That sound file should be stored in the xcode project which is why I won’t cover it.

The icon of the app is used by default to illustrate the alert.

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s