alexsosnovskiy

Local Notification Service

Apr 29th, 2016
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.21 KB | None | 0 0
  1. //This script required an asset: https://www.assetstore.unity3d.com/en/#!/content/45507
  2. //Unity 5.2+
  3. using System;
  4. using System.Collections.Generic;
  5. using Area730.Notifications;
  6.  
  7. #if UNITY_IPHONE
  8. using NotificationServices = UnityEngine.iOS.NotificationServices;
  9. using LocalNotification = UnityEngine.iOS.LocalNotification;
  10. #endif
  11.  
  12. public sealed class NotificationService
  13. {
  14.     public enum RepeatIntervalType
  15.     {
  16.         None,
  17.         Day,
  18.         Week,
  19.     }
  20.  
  21.     private const string ID_KEY = "id";
  22.  
  23.     public NotificationService()
  24.     {
  25.         #if UNITY_IOS
  26.         UnityEngine.iOS.NotificationServices.RegisterForNotifications(UnityEngine.iOS.NotificationType.Alert |
  27.         UnityEngine.iOS.NotificationType.Badge |
  28.         UnityEngine.iOS.NotificationType.Sound);
  29.         #endif 
  30.     }
  31.  
  32.     public void ScheduleNotification (int id, string title, string text, DateTime when, RepeatIntervalType repeat = RepeatIntervalType.None, string soundName = "")
  33.     {
  34. #if UNITY_IPHONE
  35.         var notification = new LocalNotification ();
  36.         notification.alertAction = title;
  37.         notification.alertBody = text;
  38.         notification.fireDate = when;
  39.  
  40.         if (!string.IsNullOrEmpty (soundName))
  41.             notification.soundName = soundName;
  42.         else
  43.             notification.soundName = LocalNotification.defaultSoundName;
  44.            
  45.         var data = new Dictionary<string,string> ();
  46.         data.Add (ID_KEY, id + "");
  47.         notification.userInfo = data;
  48.            
  49.         switch (repeat) {
  50.         case RepeatIntervalType.Day:
  51.             notification.repeatInterval = UnityEngine.iOS.CalendarUnit.Day;
  52.             break;
  53.         case RepeatIntervalType.Week:
  54.             notification.repeatInterval = UnityEngine.iOS.CalendarUnit.Week;
  55.             break;
  56.         }
  57.  
  58.         NotificationServices.ScheduleLocalNotification (notification);
  59.  
  60. #elif UNITY_ANDROID && !UNITY_EDITOR
  61.             var now = DateTime.UtcNow;
  62.             var diff = when - now;
  63.  
  64.             NotificationBuilder builder = new NotificationBuilder(id, title, text);
  65.             builder
  66.                 .setDefaults(NotificationBuilder.DEFAULT_ALL)
  67.                 .setAlertOnlyOnce(true)
  68.                 .setDelay(diff)
  69.                 .setTicker(text)
  70.                 .setAutoCancel(true);
  71.  
  72.             switch(repeat)
  73.             {
  74.                 case RepeatIntervalType.Day:
  75.                     builder.setRepeating(true).setInterval(TimeSpan.FromDays(1));
  76.                     break;
  77.                 case RepeatIntervalType.Week:
  78.                     builder.setRepeating(true).setInterval(TimeSpan.FromDays(7));
  79.                     break;
  80.             }
  81.  
  82.             AndroidNotifications.scheduleNotification(builder.build());
  83.  
  84. #endif
  85.     }
  86.  
  87.     public void RemoveNotification (int id)
  88.     {
  89. #if UNITY_IPHONE
  90.         var arr = NotificationServices.scheduledLocalNotifications;
  91.         var idStr = id + "";
  92.  
  93.         for (int i = 0; i < arr.Length; i++) {
  94.             var notification = arr [i];
  95.  
  96.             if (notification.userInfo != null && notification.userInfo.Contains (ID_KEY) && notification.userInfo [ID_KEY].Equals (idStr)) {
  97.                 NotificationServices.CancelLocalNotification (notification);
  98.                 return;
  99.             }
  100.                
  101.         }
  102. #elif UNITY_ANDROID && !UNITY_EDITOR
  103.             AndroidNotifications.cancelNotification(id);
  104.             AndroidNotifications.clear(id);
  105. #endif
  106.     }
  107.  
  108.     //      public void ClearAllNotifications()
  109.     //      {
  110.     //          #if UNITY_IPHONE
  111.     //          NotificationServices.ClearLocalNotifications();
  112.     //          NotificationServices.CancelAllLocalNotifications();
  113.     //          //#elif UNITY_ANDROID
  114.     //          AndroidNotifications.clearAll();
  115.     //          #endif
  116.     //      }
  117. }
Advertisement
Add Comment
Please, Sign In to add comment