Uilocalnotification implement local notification implementation reminders in IOS development

Source: Internet
Author: User
Tags set time

These two days in a calendar to remind the function, the use of local notification function, record the relevant knowledge as follows:

1, the definition and use of local notification:

A local notification is an instance of Uilocalnotification, which has three main types of properties:

Scheduled time, which is used to specify the date and time the iOS system sends notifications;

Notification type, the notification type, including warning messages, the caption of the action button, the badge (digital markers) on the application icon, and the sound to play;

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

For local notifications, the maximum number of recent local alerts to iOS is 64, and local alerts exceeding the limit will be ignored by iOS.

The code is as follows Copy Code

Uilocalnotification *localnotification = [[Uilocalnotification alloc] init];
if (localnotification = = nil) {
Return
}
Set the trigger time for local notifications (if you want to trigger immediately, no setting), set to 20.
Localnotification.firedate = [NSDate datewithtimeintervalsincenow:20];
Set 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 a reminder to add a sound file yourself, set as the default beep
Localnotification.soundname = Uilocalnotificationdefaultsoundname;
Set up information about notifications, which is important to add some markup content to make it easier to distinguish and get informed 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 notification on a specified date
[[UIApplication sharedapplication] schedulelocalnotification:localnotification];

Triggers an alert immediately
[[UIApplication sharedapplication] presentlocalnotificationnow:localnotification];
[Localnotification release];

2, cancel the local notice:

The code is as follows Copy Code

Cancel a notification
Nsarray *notificaitons = [[UIApplication sharedapplication] scheduledlocalnotifications];
Get all current local alerts
if (!notificaitons | | notificaitons.count <= 0) {
Return
}
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 alerts
[[UIApplication sharedapplication] cancelalllocalnotifications];

3, the local notification response:

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

A, when the application is in the background, local notifications will send the device a reminder of the same as the remote notification, 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 it will take the method in application delegate:

The code is as follows Copy Code

-(void) Application: (UIApplication *) application didreceivelocalnotification: (uilocalnotification *) Notification {

}

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

The code is as follows Copy Code

if ([[Notification.userinfo objectforkey:@ "id"] isequaltostring:@ "Affair.schedule"]) {
Uialertview *alert = [[Uialertview alloc] initwithtitle:@ "test" message:notification.alertBody cancelbuttontitle:@ "Close" otherButtonTitles:notification.alertAction, nil Nil];
[Alert show];
}

It should be noted that in situation a, if the user clicks the reminder to enter the application, will also perform a callback method to receive local notifications, in which case if you add the above code, there will be two consecutive prompts, in order to solve this problem, modify the code as follows:

  code is as follows copy code

if ([ Notification.userinfo objectforkey:@ "id"] isequaltostring:@ "Affair.schedule") { 
       //To determine the current running state of the application and, if it is activated, to alert, or not to alert  
       if ( Application.applicationstate = = uiapplicationstateactive) { 
            Uialertview *alert = [[Uialertview alloc] initwithtitle:@ "test" message:notification.alertBody Delegate:nil cancelbuttontitle:@ "Close" otherButtonTitles:notification.alertAction, nil nil]; 
            [Alert show]; 
      }  
  } 

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.