上个项目使用的简易版iOS推送解决方案,记录一下,以备后用
该方案需要注意iOS推送权限 Push Notifications
using UnityEngine;
using System.Collections;
#if UNITY_IPHONE
using NotificationServices = UnityEngine.iOS.NotificationServices;
using NotificationType = UnityEngine.iOS.NotificationType;
#endif
public class Nofication : MonoBehaviour {
public static void NotificationMessage(string message,int hour ,int min,int sec,bool isRepeatDay,string title)
{
int year = System.DateTime.Now.Year;
int month = System.DateTime.Now.Month;
int day= System.DateTime.Now.Day;
System.DateTime newDate = new System.DateTime(year,month,day,hour,min,sec);
NotificationMessage(message,newDate,isRepeatDay,title);
}
public static void NotificationMessage(string message,System.DateTime newDate,bool isRepeatDay,string title)
{
#if UNITY_IPHONE
if(newDate > System.DateTime.Now)
{
UnityEngine.iOS.LocalNotification localNotification = new UnityEngine.iOS.LocalNotification();
localNotification.fireDate =newDate;
localNotification.alertBody = message;
localNotification.applicationIconBadgeNumber = 1;
localNotification.hasAction = true;
localNotification.alertAction = title;
localNotification.alertTitle = title;
if(isRepeatDay)
{
localNotification.repeatCalendar = UnityEngine.iOS.CalendarIdentifier.ChineseCalendar;
localNotification.repeatInterval = UnityEngine.iOS.CalendarUnit.Day;
}
localNotification.soundName = UnityEngine.iOS.LocalNotification.defaultSoundName;
UnityEngine.iOS.NotificationServices.ScheduleLocalNotification(localNotification);
}
#endif
}
void Awake()
{
DontDestroyOnLoad(gameObject);
#if UNITY_IPHONE
UnityEngine.iOS.NotificationServices.RegisterForNotifications(
NotificationType.Alert |
NotificationType.Badge |
NotificationType.Sound);
#endif
CleanNotification();
}
void OnApplicationPause(bool paused)
{
if(paused)
{
NotificationMessage("推送内容1",11,55,0,true,"推送标题");
NotificationMessage("推送内容2",18,55,0,true,"推送标题");
}
else
{
CleanNotification();
}
}
public static void CleanNotification()
{
#if UNITY_IPHONE
UnityEngine.iOS.LocalNotification l = new UnityEngine.iOS.LocalNotification ();
l.applicationIconBadgeNumber = -1;
UnityEngine.iOS.NotificationServices.PresentLocalNotificationNow (l);
UnityEngine.iOS.NotificationServices.CancelAllLocalNotifications ();
UnityEngine.iOS.NotificationServices.ClearLocalNotifications ();
#endif
}
}
|