Babybus-u3d技術(shù)交流-Unity在iOS的本地推送功能
Unity內(nèi)部封裝了在iOS下的本地推送功能淳梦,可以很方便的實(shí)現(xiàn)在iOS設(shè)備上的簡(jiǎn)單本地推送。
命名空間為:UnityEngine.iOS
在Unity中的代碼和直接用swift的代碼基本類似葵腹。
具體代碼實(shí)現(xiàn):
static void NotificationMessage(string message,System.DateTime newDate,bool isRepeatDay)
{ //推送時(shí)間需要大于當(dāng)前時(shí)間
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)
{ //是否每天定期循環(huán)
localNotification.repeatCalendar = UnityEngine.iOS.CalendarIdentifier.C hineseCalendar;//中國(guó)日歷
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);
}
} //游戲退出時(shí)調(diào)用
void OnApplicationQuit()
{ //每天中午12點(diǎn)推送
NotificationMessage("每天中午12點(diǎn)推送",12,true);
} //游戲進(jìn)入后臺(tái)時(shí)或者從后臺(tái)進(jìn)入前臺(tái)時(shí)調(diào)用
void OnApplicationPause(bool paused)
{ //程序進(jìn)入后臺(tái)時(shí)
if(paused)
{ //每天中午12點(diǎn)推送
NotificationMessage("每天12點(diǎn)推送",12,true);
} else
{ //程序從后臺(tái)進(jìn)入前臺(tái)時(shí)
CleanNotification();
}
} //清除推送消息,在Awake中調(diào)用
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 (); }