In iOS development, UILocalNotification provides a simple reminder function for local notifications.

Source: Internet
Author: User

In iOS development, UILocalNotification provides a simple reminder function for local notifications.

During this period, the project requires a similar alarm reminder function. If I am not familiar with the notification, I decided to first try the local notification provided by xcode, and finally successfully implemented the function, click here to share it.

Features:

When the app is disabled, it can receive and display notifications.

When the app is in the background, it can also receive notifications.

When the app is on the frontend, it can receive but cannot be displayed, but it will follow the method in the app delegate

Specific creation method:

-Create a local notification object, UILocalNotification

-Set the fireDate, AlertBody, AlertAction, soundName, applicationBadgeNumber, repeatInterval, and alertLanuchImage attributes.

-"Configure the notification parameter userInfo. And notification content. We can get this object in the method of receiving notifications.

-Call the notification. Use schedulelocalpolicicaiton, a single instance object of the UIApplication, to start the notification as planned.

Note that user notification is required since iOS8. If you agree, create UIUerNotificationSettings and then registerUserNotificationSettings. The maximum number of local notifications allowed by iOS is 64. Local notifications that exceed the limit will be ignored by iOS.

The detailed code is as follows:

1. Registration notification, which also applies to iOS10

Call the following method in application: didfinishlaunchingwitexceptions: Of appdelegate.

   

 

In addition, to adapt to iOS10, the above Code should be replaced with the following

  

2. Definitions and usage of local notifications

The controller that requires local notifications is defined. to define an alarm clock after 5s easily, you can change it to any time point and convert it to the NSDate type to replace [NSDate dateWithTimeIntervalSinceNow: 5.

  

To distinguish between local notifications, you can define the following attributes at the same time.

// Set notification-related information. This is very important. You can add some labeled content to facilitate future distinguishing and obtaining notification information.
NSDictionary * infoDic = [NSDictionary dictionaryWithObjectsAndKeys: local_policy_schedule_id, @ "id", nil];
LocalNotification. userInfo = infoDic;

3. Cancel local notification

Note: You must cancel all local notifications each time you do not need or refresh all local notifications. Otherwise, duplicate notifications will appear.

// Cancel a notification
NSArray * notificaitons = [[UIApplication sharedApplication] scheduledlocalconfigurications];
// Obtain all current local notifications
If (! Notificaitons | notificaitons. count <= 0 ){
Return;
}
For (UILocalNotification * policy in policicaitons ){
If ([[policy. userInfo objectForKey: @ "id"] isEqualToString: local_policy_schedule_id]) {
// Cancel a specific notification
[[UIApplication sharedApplication] cancelLocalNotification: Policy];
Break;
}
}

// Cancel all local notifications
[[UIApplication sharedApplication] cancelalllocalconfigurications];

4. local notification response

If you have registered a local notification, when the client responds to the notification:

A. When the application is in the background, the local notification will send the same reminder to the device as the remote notification.

B. If the application is running, the device will not receive a notification, but will follow the method in the application delegate:

-(Void) application :( UIApplication *) application didReceiveLocalNotification :( UILocalNotification *) notification {
}

If you want to implement the reminder effect of the program in the background, you can add the relevant code in the above method.

If ([[notification. userInfo objectForKey: @ "id"] isEqualToString: local_policy_schedule_id]) {
UIAlertView * alert = [[UIAlertView alloc] initWithTitle: @ "test" message: notification. alertBody delegate: nil cancelButtonTitle: @ "close" Events: notification. alertAction, nil];
[Alert show];
}

Note that in case a, if the user clicks the reminder to enter the application, the callback method that receives the local notification will also be executed. In this case, if you add the above Code, there will be two consecutive prompts. To solve this problem, modify the Code as follows:

If ([[notification. userInfo objectForKey: @ "id"] isEqualToString: local_policy_schedule_id]) {
// Determines the current running status of the application. If the application is activated, a reminder is sent. Otherwise, no reminder is sent.
If (application. applicationState = UIApplicationStateActive ){
UIAlertView * alert = [[UIAlertView alloc] initWithTitle: @ "prompt" message: notification. alertBody delegate: nil cancelButtonTitle: @ "close" otherButtonTitles: nil, nil];
[Alert show];
}
}

If iOS10 is adapted, add the following

  

  

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.