iOS Local notifications

Source: Internet
Author: User

Added local push function to the game, write it down as a record

Local notifications are simpler than remote push.

Ios:

Add local push

/*Name: The unique name of the notification, which can be used as the ID of the notification to use message: the content of the notification time: When the notification is triggered (countdown time)*/voidSdkhelper::addlocalnotication (std::stringName, std::stringMessageintTime ) {uilocalnotification*localnotification =[[Uilocalnotification alloc] init]; if(Localnotification = =Nil) {        return; }    //Cancel the system notification of the same name first (avoid duplicate notifications)canclelocalnotication (name); //set the trigger time for local notifications (if triggered immediately, without setting), such as 20 seconds after triggeringLocalnotification.firedate =[NSDate Datewithtimeintervalsincenow:time]; //duplicate type of notificationLocalnotification.repeatinterval =Nscalendarunitday; //set the time zone for local notificationsLocalnotification.timezone =[Nstimezone Defaulttimezone]; //set the contents of a notificationLocalnotification.alertbody =[NSString StringWithUTF8String:message.c_str ()]; //set the caption of the Notification action buttonLocalnotification.alertaction =@"OK"; //set the sound of the reminder, you can add your own sound file, which is set as the default prompt soundLocalnotification.soundname =Uilocalnotificationdefaultsoundname; //set up information about the notification, this is important, you can add some of the marked content, easy to distinguish and get the notification information laternsstring* pushname = [NSString stringWithFormat:@"%s", Name.c_str ()]; Nsdictionary*infodic = [Nsdictionary dictionarywithobjectsandkeys:pushname,@"ID", [NSNumber Numberwithinteger:345635342],@" Time", [NSNumber Numberwithinteger:345635342],@"Affair.aid", nil]; Localnotification.userinfo=Infodic; //trigger a notification on a specified date[[UIApplication sharedapplication] schedulelocalnotification:localnotification]; [Localnotification release]; }voidSdkhelper::canclelocalnotication (std::stringname) {    //get an array of all the pushes that existNsarray * Array =[[UIApplication sharedapplication] scheduledlocalnotifications]; //facilitate this array according to key to get the uilocalnotification we want     for(Uilocalnotification * LocinchArray) {        if([[Loc.userinfo Objectforkey:@"ID"] Isequaltostring:[nsstring stringWithFormat:@"%s", Name.c_str ()]) {            //Cancel local Push[[UIApplication sharedapplication] cancellocalnotification:loc]; }    }}

When the notification is triggered, if the app is running in the foreground, this method will be triggered, if the app is in the background, click on the notification will trigger,

- (void) Application: (UIApplication *) application didreceivelocalnotification: (uilocalnotification*) notification{NSLog (@"application did receive local notifications"); //cancel a specific local notification     for(Uilocalnotification *notiinch[[UIApplication sharedapplication] scheduledlocalnotifications]) {NSString*NOTIID = noti.userinfo[@"ID"]; NSString*RECEIVENOTIID = notification.userinfo[@"ID"]; if([notiid isequaltostring:receivenotiid]) {[[UIApplication sharedapplication] Cancellocalnotification:noti            Fication]; return; }} Application.applicationiconbadgenumber=0;}

Where Notification.userinfo is the custom content

iOS Local notifications

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.