iOS Local notifications: uilocalnotification

Source: Internet
Author: User

Recently in an e-commerce app, saying this year e-commerce is very hot ah. A local notice was used to tidy up the

Add a local notification to the system with the following code:

Initialize local notification object uilocalnotification *notification = [[Uilocalnotification alloc] INIT];IF (notification) {//Set reminder time for notifications    NSDate *currentdate = [NSDate Date]; Notification.timezone = [Nstimezone defaulttimezone];         Use local time zone notification.firedate = [currentdate datebyaddingtimeinterval:5.0];         Set repeat interval notification.repeatinterval = kcfcalendarunitday;    Set the text content of the reminder notification.alertbody = @ "Wake up, man";         Notification.alertaction = nslocalizedstring (@ "Take medicine", nil);         Notification tones use the default notification.soundname= uilocalnotificationdefaultsoundname;         Set the number of reminders in the top right corner of the application notification.applicationiconbadgenumber++;    Sets the userinfo of the notification to identify the notification nsmutabledictionary *auserinfo = [[Nsmutabledictionary alloc] init];    Auserinfo[klocalnotificationid] = @ "Localnotificationid";         Notification.userinfo = Auserinfo; Add notifications to the system [[UIApplication sharedapplication] schedulelocalnotification:notification];}

Add code to accept local notifications in APPDELEGATE.M:

After receiving the notification, the following methods in the calling program delegate are processed://Of course the method can be jump to the specified page, and so on, here is a popup box

-(void) Application: (UIApplication *) application didreceivelocalnotification: (uilocalnotification *) notification{    NSLog (@ "Application did receive local notifications");         Uialertview *alert = [[Uialertview alloc] initwithtitle:@ "Hello" message:@ "Welcome" Delegate:nil cancelbuttontitle:@ " OK "otherbuttontitles:nil, nil";    [Alert show];
}

Recently read Daniel's blog, said to be local notice in the app uninstall, there will also exist in the system,

You can look at it, print it all, check it out.

12NSArray *localnotifications = [[UIApplication sharedapplication] scheduledlocalnotifications]; NSLog (@ "%@", localnotifications);

So how to delete local notifications, Daniel also gives a method

The cancellation method is divided into two types.

The first is to compare violence and cancel all local notifications directly:

[[UIApplication sharedapplication] cancelalllocalnotifications];

The second method is for a specific notification:

-(void) Cancellocalnotification: (uilocalnotification *) notification ns_available_ios (4_0);

It is then necessary to notify that there is an identity so that we can locate which notification is. can be specified in notification's userinfo (a dictionary).

For example:

-(void) Application: (UIApplication *) application didreceivelocalnotification: (uilocalnotification *) notification{    NSLog (@ "Application did receive local notifications");         Cancels a specific local notification for    (uilocalnotification *noti in [[UIApplication sharedapplication] scheduledlocalnotifications]) {        NSString *notiid = Noti.userinfo[klocalnotificationid];        NSString *receivenotiid = Notification.userinfo[klocalnotificationid];        if ([Notiid isequaltostring:receivenotiid]) {            [[uiapplication sharedapplication] Cancellocalnotification: Notification];            return;        }    }         Uialertview *alert = [[Uialertview alloc] initwithtitle:@ "Hello" message:@ "Welcome" Delegate:nil cancelbuttontitle:@ " OK "otherbuttontitles:nil, nil";    [Alert show];}

iOS Local notifications: 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.