看了雨松的博客,写了这样一个工具类,再lua里面调用
using UnityEngine;
using System;using System.Collections;
#if UNITY_IPHONE
using NotificationServices = UnityEngine.iOS.NotificationServices;
using NotificationType = UnityEngine.iOS.NotificationType;
#endif
public static class NotifyMessage {
//初始化推送
public static void InitNotification()
{
#if UNITY_IPHONE
UnityEngine.iOS.NotificationServices.RegisterForNotifications (
NotificationType.Alert |
NotificationType.Badge |
NotificationType.Sound);
#endif
//第一次进入游戏的时候清空,有可能用户自己把游戏冲后台杀死,这里强制清空
//CleanNotification();
}
//本地推送固定的推送时间Fixed time
public static void NotificationMessageFixedTime(string message, string title, 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,title, newDate, isRepeatDay);
}
//本地推送固定的推送时间传一个时间戳
public static void NotificationMessageTS(string message, string title, long ts, bool isRepeatDay)
{
System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1)); // 当地时区
System.DateTime newDate = startTime.AddSeconds(ts);
NotificationMessage(message,title, newDate, isRepeatDay);
}
//本地推送 多少秒后
public static void NotificationMessage(string message,string title, System.DateTime newDate, bool isRepeatDay)
{
#if UNITY_IPHONE
//推送时间需要大于当前时间
if(newDate > System.DateTime.Now)
{
UnityEngine.iOS.LocalNotification localNotification = new UnityEngine.iOS.LocalNotification();
localNotification.fireDate =newDate;
localNotification.alertBody = message;
localNotification.applicationIconBadgeNumber = UnityEngine.iOS.NotificationServices.localNotificationCount+1; //
localNotification.hasAction = true;
localNotification.alertAction = 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
}
//清空所有本地消息
public static void CleanNotification()
{
#if UNITY_IPHONE
UnityEngine.iOS.LocalNotification l = new UnityEngine.iOS.LocalNotification ();
l.applicationIconBadgeNumber = -1;
UnityEngine.iOS.NotificationServices.PresentLocalNotificationNow (l);
#endif
}
//清空所有本地消息延迟一帧执行 这里不好写在lua里面延时一帧
public static void CleanNotificationDelay()
{
#if UNITY_IPHONE
//方法 等待一帧执行 否则会出现小红点删不掉
UnityEngine.iOS.NotificationServices.CancelAllLocalNotifications ();
UnityEngine.iOS.NotificationServices.ClearLocalNotifications ();
#endif
}
}