IOS Local Notification Uilocalnotification

Source: Internet
Author: User

1. Add a local push//set 20 seconds after  NSDate *date = [NSDate dateWithTimeIntervalSinceNow:20];     //chuagjian a local push     uilocalnotification *noti = [[[ uilocalnotification alloc] init] autorelease];    if  (Noti)  {         //Set push Time          noti.firedate = date;        //Setting the time zone          noti.timeZone = [NSTimeZone defaultTimeZone];         //set the recurrence interval         noti.repeatinterval =  nsweekcalendarunit;        //Push Sound          noti.soundName = UILocalNotificationDefaultSoundName;         //content         noti.alertbody = @ "Push content";         //the number of         noti.applicationiconbadgenumber in the red circle displayed on the icon  = 1;        //settings userinfo  Easy to use when you need to undo later          NSDictionary *infoDic = [NSDictionary  dictionarywithobject:@ "name"  forkey:@ "key"];        noti.userinfo  = infodic;        //Add push to uiapplication                 uiapplication *app =  [UIApplication sharedApplication];        [app  SCHEDULELOCALNOTIFICATION:NOTI];      }2, program runtime received local push message-  (void) application :(uiapplication *) application didreceIvelocalnotification: (uilocalnotification*) Notification{uialertview *alert = [[uialertview  alloc] initwithtitle:@ "received local reminder  in app" message:notification.alertbody    delegate:nil  cancelbuttontitle:@ "OK"   otherButtonTitles:nil]; [alert show];//here, you can go through notification's useinfo and do something you want to do application.applicationiconbadgenumber -=  1;} 3. Cancel a local push uiapplication *app = [uiapplication sharedapplication];    / /get local push array     NSArray *localArr = [app scheduledLocalNotifications];         //declaring a local notification object     uilocalnotification * localnoti;        if  (Localarr)  {         for  (Uilocalnotification *noti in localarr)  {             nsdictionary *dict = noti.userinfo;             if  (dict)  {                 nsstring *inkey = [dict objectforkey:@ "Key"];                 if  ([Inkey  isequaltostring:key])  {                     if  (Localnoti) {                         [ localnoti release];                         localNoti = nil;                     }                     localNoti = [noti retain];                      break;                 }            }         }                 //determine whether to find the same key that already exists push         if  (!localnoti)  {             //does not exist   initialize              localNoti = [[UILocalNotification alloc] init];         }                 if  (localnoti && !state)  {             //do not push   cancel push              [app cancelLocalNotification:localNoti];             [localNoti release];             return;        }}




When there is a phone coming in or lock screen, then your application will hang, at this time, Uiapplicationdelegate delegate will be notified, call the Applicationwillresignactive method, you can override this method, do the work before the hang, For example, close the network and save the data.

[Java] view Plaincopyprint?

-(void) Applicationwillresignactive: (uiapplication*) application{/* Add your own pending pre-prepare code */}




when your program is suspended, he will not run in the background.
Ii. Rehabilitation
When the program is restored, another named Applicationdidbecomeactive delegate method is called, where you can recover your application by saving data before suspending it:

[Java] view Plaincopyprint?

-(void) Applicationdidbecomeactive: (uiapplication*) application{/* Add your recovery code */}



Note: When the application starts, the Applicationdidbecomeactive method is called after the Applicationdidfinishlaunching method is called, so make sure that your code can distinguish between recovery and startup, Avoid logically occurring bugs.
Third, termination
when the user presses the button or shuts down, the program will be terminated. The Applicationwillterminate method is called when a program will terminate normally. However, if the long Master button is forced to exit, the method is not called. This method should perform the rest of the cleanup, for example, all connections will gracefully shut down and perform any other necessary work before the program exits:

[Java] view Plaincopyprint?

-(void) Applicationwillterminate: (uiapplication*) application{/* Add the cleanup code before exiting and other work code */}


This article is from "Xiao Liu _blog" blog, please be sure to keep this source http://liuyafang.blog.51cto.com/8837978/1566444

IOS Local Notification Uilocalnotification

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.