Unity3d Institute of iOS local messaging notification localnotification use (67)

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.