1. 程式人生 > >Unity IOS 訊息推送

Unity IOS 訊息推送

using UnityEngine.iOS;
using System.Collections;


public class SCR_MsgPush : UnityEngine.MonoBehaviour
{
    //本地推送
    public static void NotificationMessage(string message, int hour, bool isRepeatDay)
    {
        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, 0
, 0); NotificationMessage(message, newDate, isRepeatDay); } //本地推送 你可以傳入一個固定的推送時間 public static void NotificationMessage(string message, System.DateTime newDate, bool isRepeatDay) { if (isRepeatDay && newDate <= System.DateTime.Now) { newDate = newDate.AddDays(1
); } //推送時間需要大於當前時間 if (newDate > System.DateTime.Now) { LocalNotification localNotification = new LocalNotification(); localNotification.fireDate = newDate; localNotification.alertBody = message; localNotification.applicationIconBadgeNumber = NotificationServices.scheduledLocalNotifications.Length + 1
; localNotification.hasAction = true; if (isRepeatDay) { //是否每天定期迴圈 localNotification.repeatCalendar = CalendarIdentifier.ChineseCalendar; localNotification.repeatInterval = CalendarUnit.Day; } localNotification.soundName = LocalNotification.defaultSoundName; NotificationServices.ScheduleLocalNotification(localNotification); } } void Awake() { NotificationServices.RegisterForNotifications(NotificationType.Badge | NotificationType.Alert | NotificationType.Sound); //第一次進入遊戲的時候清空,有可能使用者自己把遊戲衝後臺殺死,這裡強制清空 CleanNotification(); } void OnApplicationPause(bool paused) { //程式進入後臺時 if (paused) { //10秒後傳送 NotificationMessage("訊息1 : 10秒後傳送", System.DateTime.Now.AddSeconds(10), false); //每天中午12點推送 NotificationMessage("訊息2 : 每天中午12點推送", 12, true); } else { //程式從後臺進入前臺時 CleanNotification(); } } //清空所有本地訊息 void CleanNotification() { LocalNotification l = new LocalNotification(); l.applicationIconBadgeNumber = -1; NotificationServices.PresentLocalNotificationNow(l); Invoke("WaitOneFrameClear", 0); } // 延遲一幀執行,不然沒法清理 void WaitOneFrameClear() { NotificationServices.CancelAllLocalNotifications(); NotificationServices.ClearLocalNotifications(); } } ... prompt'''