Use of uilocalnotification in IOS

Source: Internet
Author: User

Notification is a very common information transfer mechanism in smartphone application programming. It can save resources and check information status without consuming resources ), apps in IOS are divided into two different notification types: local and remote. Local notification is centrally managed by notificationmanager in IOS. You only need to add encapsulated local notification objects to the queue of the system notification management mechanism. The system will trigger local notification at the specified time, the application only needs to design the method for processing notification to complete the whole notification process.

The object used by local notification is uilocalnotification. The properties of uilocalnotification cover all the content required to process notification. Uilocalnotification attributes include firedate, timezone, repeatinterval, repeatcalendar, alertbody, alertaction, hasaction, alertlaunchimage, applicationiconbadgenumber, soundname, and userinfo.

Scheduling of uilocalnotification

Firedate, timezone, repeatinterval, and repeatcalendar are used for uilocalnotification scheduling. Firedate is the exact time when uilocalnotification is triggered. Timezone indicates whether the excitation time of uilocalnotification changes according to the time zone. If it is set to nil, uilocalnotification will be triggered after a period of time, rather than a specific time. Repeatinterval is the time difference between the repeated calls of uilocalnotification. However, the time difference is based entirely on the calendar unit (nscalendarunit), such as the unit of the weekly calls and nsweekcalendarunit. If this parameter is not set, the call will not be repeated. Repeatcalendar is the reference calendar for the calendar unit used by the uilocalnotification repeated excitation. If it is not set, the default calendar will be used as the reference calendar.

Reminder content of uilocalnotification

Alertbody, alertaction, hasaction, and alertlaunchimage are processed by the system when the application is not running.

The uilocalnotification reminder is required. Alertbody is a string (nsstring) of the actual reminder content. If alertbody is not set, it will be unrealistic when notification is triggered. Alertaction is also a string (nsstring). The content of alertaction is used as the text on the action button in the reminder. If not set, the action button in the reminder information is displayed in the text form of "View. Alertlaunchimage is the image that is waiting for the application to be loaded when you click the action button ("View") in the reminder box. This will replace the image that was originally set by the application. Hasaction is a Boolean value that controls whether to display the action button in the prompt box. The default value is yes.

Other parts of uilocalnotification

Applicationiconbadgenumber, soundname, and userinfo make uilocalnotification more complete. Applicationiconbadgenumber is the number displayed in the upper-right corner of the application icon, so that you can directly understand the notification to be processed by the application. Soundname is another method used by uilocalnotification to remind users. After notification is triggered, this sound will be played to remind users that a notification needs to be processed. If soundname is not set, notification is triggered so that no sound is played. In addition to the specially crafted sound, you can also set soundname to uilocalnotificationdefasoundname to use the system default sound notification. Userinfo is the nsdictionary that notification uses to transmit data.

Register uilocalnotification

After setting the uilocalnotification object, the application needs to register the configured uilocalnotification object in the system notification processing queue. The method for registering uilocalnotification * localnotification is:

[[UIApplication
sharedApplication]  scheduleLocalNotification:localNotification];

In some cases, the application may need to directly trigger a notification instead of waiting for a period of time. The application can directly trigger a notification in the following ways:

[[UIApplication
sharedApplication] presentLocalNotificationNow:localNotification];

Process uilocalnotification

After the action button in the prompt box is clicked, You can process it in the-(bool) Application: didfinishlaunchingwitexceptions: Application delegate method when the application starts to run. You can load the recently unprocessed notification as follows:

UILocalNotification
* localNotif=[launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];

If the application is running, you can process the notification by overwriting the method-(void) Application: didreceivelocalnotification: In application delegate. The second parameter of the method is the uilocalnotification object. You only need to process the userinfo carried by the object to process the response action.

Cancel uilocalnotification

You can use either of the following methods to cancel a registered notification. The first method can directly cancel a specified notification, and the second method will cancel the registered notification of the application.

[[UIApplication
sharedApplication] cancelLocalNotification:localNotification];

[[UIApplication
sharedApplication] cancelAllLocalNotification];

Summary

The local notification mechanism is very effective in application development and can help developers Manage events that need to occur at a specified time, such as alarm applications. In addition, because the system manages the notification in a unified manner, the same task can be processed very easily without wasting resources for the application to wait for the event to trigger.

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.