IOS Local Notification Uilocalnotification

Source: Internet
Author: User

iOS local notifications are using OC's Uilocalnotification object, adding a notification is simple, creating a Uilocalnotification object to set some corresponding parameters on the line.

Add Local Notifications

1 //Add local push2NSDate *date = [NSDate datewithtimeintervalsincenow:Ten];3Uilocalnotification *noti =[[Uilocalnotification alloc] init];4     if(Noti) {5         //Set the time6Noti.firedate =date;7         //Setting the time zone8Noti.timezone =[Nstimezone Defaulttimezone];9         //set the recurrence intervalTenNoti.repeatinterval =Nsweekdaycalendarunit; One         //Push Sound ANoti.soundname = Uilocalnotificationdefaultsoundname;//Add a sound clip, such as @ "BEIJING.CAF" -         //content -Noti.alertbody =@"Push Content"; the         //real numbers in the red circle on icon -Noti.applicationiconbadgenumber = [[[UIApplication Sharedapplication] scheduledlocalnotifications] count] +1; -         //add userinfo for ease of use when undoing -Nsdictionary *infodic = [[Nsdictionary alloc] Initwithobjectsandkeys:@"name",@"Key", nil]; +Noti.userinfo =Infodic; -         //add push to UIApplication + [[UIApplication sharedapplication] schedulelocalnotification:noti]; A}

in the non-arc, created must learn to release. If you do not add this sentence, notice to the time, found the top of the notification bar tips where there is, then you go through the notification bar, and then you find the notification bar inside there is this hint, unless you manually clear

[Notification release];

}

Delete, modify notification

A question that prompts you in the top right corner. You triggered 5 notifications, the background is running in case, 3 has arrived, so your app in the upper right corner is now showing 3. Then you go through the notification bar or the app, the top right corner of the app should change to 0. According to the previous settings in the AddNotification, wait until the 4th notification to the next, the app in the upper right corner will show 4, the fifth to the future, will show 5, it is obvious that this is wrong. So we need to go in through the notification bar, or directly into the app after the first to set the fourth fifth notice to the next, the app in the upper right corner of the number. The two methods in this rewrite appdelegate are: Didreceivelocalnotification and applicationdidbecomeactive. Didreceiveldidreceivelocalnotification is the app that runs in the foreground, notifies the time to call the method. If the program is running in the background, the time will not go this way. Applicationdidbecomeactive is the app running in the background, the notification time, you enter from the notification bar, or directly click the app icon to enter, the way to go. Ocalnotification and Applicationdidbecomeactiv
1- (void) Applicationdidbecomeactive: (UIApplication *) Application2 {3Application.applicationiconbadgenumber=0;4     intCount =[[[UIApplication Sharedapplication] scheduledlocalnotifications] count];5     if(count>0)6     {7Nsmutablearray *newarry= [Nsmutablearray arraywithcapacity:0];8          for(intI=0; i<count; i++)9         {TenUilocalnotification *notif=[[[ uiapplication sharedapplication] scheduledlocalnotifications] objectatindex:i]; Onenotif.applicationiconbadgenumber=i+1; A [Newarry addobject:notif]; -         } - [[UIApplication sharedapplication] cancelalllocalnotifications]; the         if(newarry.count>0) -         { -              for(intI=0; i<newarry.count; i++) -             { +Uilocalnotification *notif =[Newarry objectatindex:i]; - [[UIApplication sharedapplication] schedulelocalnotification:notif]; +             } A         } at     } -}

1- (void) Application: (UIApplication *) application didreceivelocalnotification: (Uilocalnotification *) Notification2 {3Uialertview *view = [[Uialertview alloc] Initwithtitle:@"Accept Local Notifications"Message:notification.alertBodyDelegate: Self Cancelbuttontitle:@"Determine"Otherbuttontitles:nil];4 [view show];5Application.applicationiconbadgenumber-=1;6     7     //3. Cancel a local push8UIApplication *app =[UIApplication sharedapplication];9     //get local push arrayTenNsarray *localarr =[app Scheduledlocalnotifications]; One      A     //declaring a local notification object -Uilocalnotification *Localnoti; -      the     if(Localarr) { -          for(Uilocalnotification *notiinchLocalarr) { -Nsdictionary *dict =Noti.userinfo; -             if(dict) { +NSString *inkey = [Dict objectforkey:@"Key"]; -                 if([Inkey isequaltostring:@"Key"]) { +                     if(Localnoti) { ALocalnoti =Nil; at                     } -                      Break; -                 } -             } -         } -          in         //determine if a push is found for the same key that already exists -         if(!Localnoti) { to             //There is no initialization +Localnoti =[[Uilocalnotification alloc] init]; -         } the          *         if(Localnoti) { $             //do not push cancel pushPanax Notoginseng [app Cancellocalnotification:localnoti]; -             return; the         } +     } A}

Another problem is that local notifications are cached by the system, and notifications are present regardless of whether the application is running in front of the background. But when we shut down the application and rerun it, some notifications may be added repeatedly by application logic, which is not the result we want. Therefore, the simplest way is to clear all local notifications when the program starts, and then add them again by applying logic. This adds a clear notification and resets the application hint number in the first start callback letter of the program didfinishlaunchingwithoptions.

- (BOOL)application:(uiapplication *)Application Didfinishlaunchingwithoptions:(nsdictionary *)launchoptions
{

    About notification
Application. =0;
[[uiapplication sharedapplication] cancelalllocalnotifications];

   return YES;
}

Of course, there is also a workaround is to determine whether the ID already exists each time it is added, if there is a delete plus. Personally think this is a comparison of the method of egg pain, as well as the program when the start of emptying after the re-added good. Reference to: http://zengwu3915.blog.163.com/blog/static/27834897201392895626868/

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.