Babybus-u3d技术交流-Unity在iOS的本地推送功能
Unity内部封装了在iOS下的本地推送功能,可以很方便的实现在iOS设备上的简单本地推送。
命名空间为:UnityEngine.iOS
在Unity中的代码和直接用swift的代码基本类似。
具体代码实现:
static void NotificationMessage(string message,System.DateTime newDate,bool isRepeatDay)
{ //推送时间需要大于当前时间
if(newDate > System.DateTime.Now)
{ UnityEngine.iOS.LocalNotificationlocalNotification = new UnityEngine.iO S.LocalNotification();
localNotification.fireDate =newDate;
localNotification.alertBody = message;
localNotification.applicationIconBadgeNumber = 1;
localNotification.hasAction = true;
if(isRepeatDay)
{ //是否每天定期循环
localNotification.repeatCalendar = UnityEngine.iOS.CalendarIdentifier.C hineseCalendar;//中国日历
localNotification.repeatInterval = UnityEngine.iOS.CalendarUnit.Day;//每日推送
}
localNotification.soundName = UnityEngine.iOS.LocalNotification.defaultSound Name;
UnityEngine.iOS.NotificationServices.RegisterForNotifications(UnityEngine.iOS. NotificationType.Alert| UnityEngine.iOS.NotificationType.Badge | UnityEngine.iOS.NotificationType.Sound);
//以特定的类型推送
UnityEngine.iOS.NotificationServices.ScheduleLocalNotification(localNotification);
}
} //游戏退出时调用
void OnApplicationQuit()
{ //每天中午12点推送
NotificationMessage("每天中午12点推送",12,true);
} //游戏进入后台时或者从后台进入前台时调用
void OnApplicationPause(bool paused)
{ //程序进入后台时
if(paused)
{ //每天中午12点推送
NotificationMessage("每天12点推送",12,true);
} else
{ //程序从后台进入前台时
CleanNotification();
}
} //清除推送消息,在Awake中调用
void CleanNotification()
{ UnityEngine.iOS.LocalNotification ln = new UnityEngine.iOS.LocalNotification();
ln.applicationIconBadgeNumber = -1;
UnityEngine.iOS.NotificationServices.PresentLocalNotificationNow (ln);
UnityEngine.iOS.NotificationServices.CancelAllLocalNotifications ();
UnityEngine.iOS.NotificationServices.ClearLocalNotifications (); }