2015年11月24日 星期二

Unity - 使用iOS推播功能

需要先將LocalNotification實例化

UnityEngine.iOS.LocalNotification localNotification = new UnityEngine.iOS.LocalNotification();




  •  localNotification.fireDate  = 發送時間,APP開啟時不會出現推播訊息
  • localNotification.alertBody = 推播內容
  • localNotification.applicationIconBadgeNumber = APP圖示上的數字
  • localNotification.soundName = 提示聲音
記得要將權限開給APP

完整CODE如下



    public static void NotificationMessage(string message, System.DateTime newDate, bool isRepeatDay)
    {
        if (newDate > System.DateTime.Now)
        {
            UnityEngine.iOS.LocalNotification localNotification = new UnityEngine.iOS.LocalNotification();
            localNotification.fireDate = newDate;
            localNotification.alertBody = message;
            localNotification.applicationIconBadgeNumber++;

            localNotification.soundName = UnityEngine.iOS.LocalNotification.defaultSoundName;
            UnityEngine.iOS.NotificationServices.ScheduleLocalNotification(localNotification);
        }
    }


    public void Awake()
    {
        UnityEngine.iOS.NotificationServices.RegisterForNotifications(UnityEngine.iOS.NotificationType.Alert | UnityEngine.iOS.NotificationType.Badge | UnityEngine.iOS.NotificationType.Sound);
        CleanNotification();
    }