Uilocalnotification enables local notification implementation reminders in IOS development

Source: Internet
Author: User

These two days in a schedule reminder function, the use of the local notification function, record the relevant knowledge as follows:

1. Definition and use of local notifications:

Local notifications are instances of uilocalnotification, and there are three main types of properties:

Scheduled time, which specifies the date and time when the iOS system sends notifications;

Notification type, which includes the warning message, the caption of the action button, the badge (digital marker) on the app icon, and the sound being played;

Custom data, local notifications can contain a dictionary type of local data.

Limits on the number of local notifications, iOS allows up to 64 recent local notifications, and local notifications exceeding the limit will be ignored by iOS.

Code to copy code as follows
Uilocalnotification *localnotification = [[Uilocalnotification alloc] init];
if (localnotification = = nil) {
Return
}
Set the trigger time for local notifications (if you want to trigger immediately, without setting), this is set to 20 wonderful after
Localnotification.firedate = [NSDate datewithtimeintervalsincenow:20];
Set the time zone for local notifications
Localnotification.timezone = [Nstimezone defaulttimezone];
Set the contents of a notification
Localnotification.alertbody = Affair.title;
Set the caption of the Notification action button
Localnotification.alertaction = @ "View";
Set the sound of the reminder, you can add your own sound file, which is set as the default prompt sound
Localnotification.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 later
Nsdictionary *infodic = [nsdictionary dictionarywithobjectsandkeys:local_notify_schedule_id,@ "ID", [NSNumber numberwithinteger:time],@ "Time", [NSNumber numberwithint:affair.aid],@ "Affair.aid", nil];
Localnotification.userinfo = Infodic;
Trigger a notification on a specified date
[[UIApplication sharedapplication] schedulelocalnotification:localnotification];

Trigger a notification immediately
[[UIApplication sharedapplication] presentlocalnotificationnow:localnotification];
[Localnotification release];

2. Cancel Local notification:

Code to copy code as follows
Cancel a notification
Nsarray *notificaitons = [[UIApplication sharedapplication] scheduledlocalnotifications];
Get all current local notifications
if (!notificaitons | | notificaitons.count <= 0) {
return; (www.111cn.net)
}
For (Uilocalnotification *notify in notificaitons) {
if ([[[Notify.userinfo objectforkey:@] id "] isequaltostring:local_notify_schedule_id]) {
To cancel a specific notification
[[UIApplication sharedapplication] cancellocalnotification:notify];
Break
}
}

Cancel all local notifications
[[UIApplication sharedapplication] cancelalllocalnotifications];

3, the response of local notification:

If a local notification has been registered, when the client responds to the notification:

A, the application in the background, the local notification will be sent to the device with a remote notification of the same reminder, the style of the reminder is set by the user in the phone settings

b, the application is running, the device will not receive a reminder, but will walk the method in application delegate:

Code to copy code as follows
-(void) Application: (UIApplication *) application didreceivelocalnotification: (uilocalnotification *) Notification {

}

, if you want to implement the program in the background of the kind of reminder effect, you can add the relevant code in the above method, sample code:

Code to copy code as follows
if ([[[Notification.userinfo objectforkey:@] id "] isequaltostring:@" Affair.schedule "]) {
Uialertview *alert = [[Uialertview alloc] initwithtitle:@ "test" Message:notification.alertBody Delegate:nil cancelbuttontitle:@ "Off" otherButtonTitles:notification.alertAction, nil Nil];
[Alert show];
}

It is important to note that in case a, if the user clicks on the reminder to enter the application, the callback method that receives the local notification will also be executed, in which case if you add the above code, there will be two consecutive prompts, in order to solve the problem, the code is as follows:

Code to copy code as follows
if ([[[Notification.userinfo objectforkey:@] id "] isequaltostring:@" Affair.schedule "]) {
Determine the current running state of the application, and if it is active, make a reminder, or do not alert
if (application.applicationstate = = uiapplicationstateactive) {
Uialertview *alert = [[Uialertview alloc] initwithtitle:@ "test" Message:notification.alertBody Delegate:nil cancelbuttontitle:@ "Off" otherButtonTitles:notification.alertAction, nil Nil];
[Alert show];
}
}

From:http://www.111cn.net/sj/ios/60372.htm

Uilocalnotification enables local notification implementation reminders in IOS development

Related Article

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.