using
UnityEngine;
using System.Collections;
public class
NewBehaviourScript
: 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(newDate
> System.DateTime.Now)
{
LocalNotification
localNotification
= new
LocalNotification();
localNotification.fireDate
=newDate;
localNotification.alertBody
= message;
localNotification.applicationIconBadgeNumber
= 1;
localNotification.hasAction
= true;
if(isRepeatDay)
{
//是否每天定期循環
localNotification.repeatCalendar
= CalendarIdentifier.ChineseCalendar;
localNotification.repeatInterval
= CalendarUnit.Day;
}
localNotification.soundName
= LocalNotification.defaultSoundName;
NotificationServices.ScheduleLocalNotification(localNotification);
}
}
void
Awake()
{
//第一次進入遊戲的時候清空,有可能用戶自己把遊戲衝後臺殺死,這裏強制清空
CleanNotification();
}
void
OnApplicationPause(bool
paused)
{
//程序進入後臺時
if(paused)
{
//10秒後發送
NotificationMessage("雨鬆MOMO : 10秒後發送",System.DateTime.Now.AddSeconds(10),false);
//每天中午12點推送
NotificationMessage("雨鬆MOMO : 每天中午12點推送",12,true);
}
else
{
//程序從後臺進入前臺時
CleanNotification();
}
}
//清空所有本地消息
void
CleanNotification()
{
LocalNotification
l =
new LocalNotification
();
l.applicationIconBadgeNumber
= -1;
NotificationServices.PresentLocalNotificationNow
(l);
NotificationServices.CancelAllLocalNotifications
();
NotificationServices.ClearLocalNotifications
();
}
}