On mobile devices, usually only one app is active, and if some other app receives new messages or changes and needs to be notified to the user, the notification mechanism can be used to inform the user. In addition, many of the apps installed on the device become zombie apps that are rarely turned on after installation, in order to avoid this situation, you can add notifications to the program to prompt the user at the specified moment.
In iOS development, the notification mechanism is broadly divided into two broad categories, local notification and remote notification, the two ways to achieve the same effect, are through a banner or pop-up reminders to inform the user, click on the notification can open the app, but they implement the principle is not the same. Here, mainly to explain the local notice.
Local notifications are issued by the local app itself, which is a form of notification based on time behavior, which is triggered after pressing the home key to exit the app or the lock screen key, and displays the number of notifications on the icon for the corresponding app. Local notifications are more practical, such as alarm clock timing, reminders, etc. are through it to achieve.
If the notification is implemented, the user will be prompted to open the notification when the app is opened for the first time, and if the user does not allow it, the notification function cannot be used unless the user takes the settings again. Although local notifications can prompt users to use the app, don't be notified frequently, otherwise it can backfire.
| A. Steps to use local notifications |
1. Create uilocationnotification
2. Set the time to process notifications Firedate
3. Configure the contents of the Notification: notification body, notification sound, icon text, etc.
4. Configure custom data for notification delivery (optional)
5. Invoke notification
| Second, the local notification code implementation |
(a) First go to the didfinishlaunchingwithoptions in APPDELEGATE.M
method to determine if you have obtained authorization to create a local notification, and if not, ask for authorization.
When the user opens the app for the first time, it will pop up the interface, ask if the notification is allowed, and if the user chooses not to allow it, the notification cannot be sent unless the settings are set and the interface is displayed:
Code:
if ([[UIApplication sharedapplication]currentusernotificationsettings].types==uiusernotificationtypenone) { [ [UIApplication sharedapplication]registerusernotificationsettings:[uiusernotificationsettings SettingsForTypes: Uiusernotificationtypealert| uiusernotificationtypebadge| Uiusernotificationtypesound Categories:nil]]; }
(ii) Add notification when you retire to the background
When you go back to the background, add a notification that can be implemented in Applicationdidenterbackground: in APPDELEGATE.M, where the method of adding notifications is called
(iii) Customizing how to add Notifications
Set the contents of the notification here and call the notification
code:
-(void) addlocalnotification{//define local notification object Uilocalnotification *notification=[[uilocalnotification Alloc]init]; Set the call time notification.firedate=[nsdate datewithtimeintervalsincenow:5.0];//notification trigger time, after 10s notification.repeatinterval=2;//notification repeat times/Nscalendar *calendar=[nscalendar Currentcalendar]; [Calendar Settimezone:[nstimezone Defaulttimezone]; notification.repeatcalendar=calendar;//the current calendar, it is best to set the time zone and other information to be able to automatically synchronize times//Set notification properties [email protected] "This is the notification body. "; Notifies the body notification.applicationiconbadgenumber=1;//the number of messages displayed in the upper-right corner of the application icon notification.alertact[email protected] " Open the App "; The sliding action hint of the standby interface [email protected] "Default";//click the notification to open the app when the start picture, here Use the program to start the picture//notification.soundname= uilocalnotificationdefaultsoundname;//The sound that is played when the notification is received, the default message sound//[email protected] "MSG.CAF";//Notification sound (requires the real machine to hear sound)//Set User information [email protected]{@ "id": @1,@ "User": @ "jredu"};//bind to other additional information on the notification//Invoke notification [[UIApplication Sharedapplicat ION] ScheduLelocalnotification:notification];}
(iv) Effect display
When the program is back in the background, the effect of the notification
When you lock the screen, the effect of the notification
(v) Click Notifications to open the application again
At this time, you should modify the application's icon, so that the top right corner of the unread notification bar number reset to 0, can be implemented in Applicationwillenterforeground:
Code:
-(void) Applicationwillenterforeground: (uiapplication *) application {[ [uiapplication sharedapplication] setapplicationiconbadgenumber:0];//enter foreground to cancel app message icon}
To learn more about the small partners, you can click to view the source code , run the test yourself.
Inquiries or technical exchanges, please join the official QQ Group: (452379712)
Jerry Education
Source:http://blog.csdn.net/jerehedu/
This article is the copyright of Yantai Jerry Education Technology Co., Ltd. and CSDN Common, welcome reprint, but without the author's consent must retain this paragraph statement, and in the article page obvious location to the original link, otherwise reserves the right to pursue legal responsibility.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
IOS Local Notifications