Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //This script required an asset: https://www.assetstore.unity3d.com/en/#!/content/45507
- //Unity 5.2+
- using System;
- using System.Collections.Generic;
- using Area730.Notifications;
- #if UNITY_IPHONE
- using NotificationServices = UnityEngine.iOS.NotificationServices;
- using LocalNotification = UnityEngine.iOS.LocalNotification;
- #endif
- public sealed class NotificationService
- {
- public enum RepeatIntervalType
- {
- None,
- Day,
- Week,
- }
- private const string ID_KEY = "id";
- public NotificationService()
- {
- #if UNITY_IOS
- UnityEngine.iOS.NotificationServices.RegisterForNotifications(UnityEngine.iOS.NotificationType.Alert |
- UnityEngine.iOS.NotificationType.Badge |
- UnityEngine.iOS.NotificationType.Sound);
- #endif
- }
- public void ScheduleNotification (int id, string title, string text, DateTime when, RepeatIntervalType repeat = RepeatIntervalType.None, string soundName = "")
- {
- #if UNITY_IPHONE
- var notification = new LocalNotification ();
- notification.alertAction = title;
- notification.alertBody = text;
- notification.fireDate = when;
- if (!string.IsNullOrEmpty (soundName))
- notification.soundName = soundName;
- else
- notification.soundName = LocalNotification.defaultSoundName;
- var data = new Dictionary<string,string> ();
- data.Add (ID_KEY, id + "");
- notification.userInfo = data;
- switch (repeat) {
- case RepeatIntervalType.Day:
- notification.repeatInterval = UnityEngine.iOS.CalendarUnit.Day;
- break;
- case RepeatIntervalType.Week:
- notification.repeatInterval = UnityEngine.iOS.CalendarUnit.Week;
- break;
- }
- NotificationServices.ScheduleLocalNotification (notification);
- #elif UNITY_ANDROID && !UNITY_EDITOR
- var now = DateTime.UtcNow;
- var diff = when - now;
- NotificationBuilder builder = new NotificationBuilder(id, title, text);
- builder
- .setDefaults(NotificationBuilder.DEFAULT_ALL)
- .setAlertOnlyOnce(true)
- .setDelay(diff)
- .setTicker(text)
- .setAutoCancel(true);
- switch(repeat)
- {
- case RepeatIntervalType.Day:
- builder.setRepeating(true).setInterval(TimeSpan.FromDays(1));
- break;
- case RepeatIntervalType.Week:
- builder.setRepeating(true).setInterval(TimeSpan.FromDays(7));
- break;
- }
- AndroidNotifications.scheduleNotification(builder.build());
- #endif
- }
- public void RemoveNotification (int id)
- {
- #if UNITY_IPHONE
- var arr = NotificationServices.scheduledLocalNotifications;
- var idStr = id + "";
- for (int i = 0; i < arr.Length; i++) {
- var notification = arr [i];
- if (notification.userInfo != null && notification.userInfo.Contains (ID_KEY) && notification.userInfo [ID_KEY].Equals (idStr)) {
- NotificationServices.CancelLocalNotification (notification);
- return;
- }
- }
- #elif UNITY_ANDROID && !UNITY_EDITOR
- AndroidNotifications.cancelNotification(id);
- AndroidNotifications.clear(id);
- #endif
- }
- // public void ClearAllNotifications()
- // {
- // #if UNITY_IPHONE
- // NotificationServices.ClearLocalNotifications();
- // NotificationServices.CancelAllLocalNotifications();
- // //#elif UNITY_ANDROID
- // AndroidNotifications.clearAll();
- // #endif
- // }
- }
Advertisement
Add Comment
Please, Sign In to add comment