Use of iOS local message notification Localnotification Unity3d Institute

Source: Internet
Author: User

Original address: 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.

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 666768 using unityengine; using System. Collections; public class newbehaviourscript : monobehaviour { //local push 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         Systemdatetime newdate =< Span class= "crayon-h" > new Systemdatetime (year,< Span class= "crayon-v" >month,day,hour,0 0) notificationmessage(message,newdate,isrepeatday);     } //local push you can pass in a fixed push time Public static void notificationmessage(string message, System. DateTime newdate,bool isrepeatday)     { //push time needs to be greater than current time if(newdate &gt; System. DateTime. Now)         { localnotification localnotification = new localnotification() ; localnotification.     Firedate =newdate; localnotification. Alertbody = message; localnotification. Applicationiconbadgenumber = 1; localnotification. Hasaction = true; if(isrepeatday)             { //whether to cycle regularly every day localnotification. Repeatcalendar = calendaridentifier. Chinesecalendar; localnotification. Repeatinterval = calendarunit. Day;             } localnotification. Soundname = localnotification. Defaultsoundname; notificationservices. Schedulelocalnotification(localnotification);         }    } void awake()     { //When entering the game for the first time, it is possible for the user to kill the game in the background, forcing the empty cleannotification();     } void onapplicationpause(bool paused)     { //When the program enters the background if(paused)         { //10 seconds after delivery notificationmessage("momo:10 seconds after the rain is sent",System. DateTime. Now. AddSeconds(ten),false); //Every day 12 o'clock Noon to push notificationmessage("Rain pine Momo: Push 12 o'clock noon every day",(), true);         } Else         { //When the program enters the foreground from the background cleannotification();         }    } //Clear all local messages void cleannotification()     { localnotification l = new localnotification (); l. Applicationiconbadgenumber = -1; notificationservices. Presentlocalnotificationnow (l); notificationservices. Cancelalllocalnotifications (); notificationservices. Clearlocalnotifications ();     }}

Pop-up message notification.

Finally is the project, the Rain Pine Momo wish everyone to study happily.

Http://vdisk.weibo.com/s/qDm4IY-bnMQb

    • This article fixed link: http://www.xuanyusong.com/archives/2632

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.