From: http://www.xuanyusong.com/archives/2632
There are usually local messages in the game today, such as 12 points a day or 6 o'clock in the afternoon to tell the player to pick up the game. This kind of thing does not need the server to push, the client can complete. Unity provides the functionality of local tasks but only iOS support, at first I'm a little puzzled why Android does not support, when I put the Android local notification done, I understand. iOS Source API support fixed-time loop push, and Android need to open a services, start a Alarmmanager timer task, fortunately I have developed Android, We'll talk about local notifications on iOS today.
The code is actually very simple, I first say the principle behind the implementation steps.
1. Register a local notification when the game enters the background
2. Turn off local notifications when the game enters the foreground
The code below.
usingUnityengine;usingSystem.Collections; Public classNewbehaviourscript:monobehaviour {//Local Push Public Static voidNotificationmessage (stringMessageintHour,BOOLisrepeatday) { intYear =System.DateTime.Now.Year; intmonth =System.DateTime.Now.Month; intday=System.DateTime.Now.Day; System.DateTime newdate=NewSystem.DateTime (Year,month,day,hour,0,0); Notificationmessage (Message,newdate,isrepeatday); } //local push You can pass in a fixed push time Public Static voidNotificationmessage (stringMessage,system.datetime Newdate,BOOLisrepeatday) { //push time needs to be greater than current time if(Newdate >System.DateTime.Now) {localnotification localnotification=Newlocalnotification (); Localnotification.firedate=newdate; Localnotification.alertbody=message; Localnotification.applicationiconbadgenumber=1; Localnotification.hasaction=true; if(isrepeatday) {//whether to cycle regularly every dayLocalnotification.repeatcalendar =Calendaridentifier.chinesecalendar; Localnotification.repeatinterval=Calendarunit.day; } localnotification.soundname=Localnotification.defaultsoundname; Notificationservices.schedulelocalnotification (localnotification); } } voidAwake () {//the first time to enter the game empty, it is possible for the user to kill the game backstage, here forced to emptycleannotification (); } voidOnapplicationpause (BOOLpaused) { //when the program enters the background if(paused) {//send after 10 secondsNotificationmessage ("Rain pine momo:10 seconds after send", System.DateTime.Now.AddSeconds (Ten),false); //Daily 12 O'Clock NoonNotificationmessage ("Rain pine Momo: 12 o'clock noon every day to push", A,true); } Else { //when the program enters the foreground from the backgroundcleannotification (); } } //Clear all local messages voidcleannotification () {localnotification L=Newlocalnotification (); L.applicationiconbadgenumber= -1; Notificationservices.presentlocalnotificationnow (l); Notificationservices.cancelalllocalnotifications (); Notificationservices.clearlocalnotifications (); }}
: http://vdisk.weibo.com/s/qDm4IY-bnMQb