IOS local notifications and IOS notifications
On mobile devices, only one application is active. If some other applications receive new messages or change at this time, they must be notified to users, you can use the notification mechanism to notify users. In addition, many apps installed on the device become zombie applications and are rarely used after installation. To avoid this situation, you can add notifications to the program, prompt the user at the specified time.
In IOS development, the notification mechanism is generally divided into two categories: local notification and remote notification. The two methods achieve the same effect and both use banners or pop-up notifications to notify users, you can open apps when you click notifications, but they do not work in the same way. Here, we will mainly explain the local notifications.
A local notification is sent by a local App. It is a time-based notification. It is triggered after you press the home Key to exit the App or press the screen lock key to exit, display the number of notifications on the corresponding App icon. Local notifications are more practical, such as alarm clock timing and Agent reminders.
If the notification is implemented, the user will prompt whether to enable the notification when opening the App for the first time. If the user chooses not to allow the notification, the user cannot use the notification function, unless the user takes the settings and sets it again. Although local notifications can prompt users to use the App, do not send frequent notifications. Otherwise, they will be counterproductive.
| 1. Use local notifications |
1. Create UILocationNotification
2. Set fireDate for Processing notifications
3. Configure the notification content: Notification subject, notification sound, icon text, etc.
4. Configure custom data for notification transmission (optional)
5. Call Notification
| 2. Local notification code implementation |
(1) Go to didfinishlaunchingwitexceptions in AppDelegate. m.
Method to Determine whether you have been authorized to create a local notification. If not, request authorization.
When a user opens the App for the first time, an interface will pop up asking whether to allow notifications. If the user chooses not to allow notifications, the notification cannot be sent unless it is set in settings. The page is displayed:
Code:
if ([[UIApplication sharedApplication]currentUserNotificationSettings].types==UIUserNotificationTypeNone){ [[UIApplication sharedApplication]registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil]]; }(2) Add a notification when you return to the background.
When you return to the background, add a notification, which can be implemented in applicationDidEnterBackground: In AppDelegate. m. The method for adding a notification is called here.
(3) customize the method for adding notifications
Set the notification content here and call the notification
Code:
-(Void) addLocalNotification {// defines the local notification object UILocalNotification * notification = [[UILocalNotification alloc] init]; // sets the call time notification. fireDate = [NSDate dateWithTimeIntervalSinceNow: 5.0]; // notification trigger time after 10 s. repeatInterval = 2; // Number of notification repetitions // NSCalendar * calendar = [NSCalendar currentCalendar]; // [calendar setTimeZone: [NSTimeZone defatimetimezone]; // notification. repeatCalendar = calendar; // the current calendar. It is best to set the time zone and other information before use to automatically synchronize the time // set the notification attribute notification. alertBody = @ "this is the notification subject"; // notification body. applicationIconBadgeNumber = 1; // notification of the number of messages displayed in the upper right corner of the application icon. alertAction = @ "open application"; // notification of the sliding action on the standby interface. alertLaunchImage = @ "Default"; // The launch image when the application is opened by clicking the notification. Here, use the program to start the image // notification. soundName = uilocalnotificationdefadefasoundname; // The Playing sound when the notification is received. The default message sound // notification. soundName = @ "msg. caf "; // notification sound (requires a real machine to hear the sound) // set user information notification. userInfo = @ {@ "id": @ 1, @ "user": @ "jredu "}; // additional information bound to the notification // call the notification [[UIApplication sharedApplication] scheduleLocalNotification: notification];}(4) effect display
Notification effect when the program is returned to the background
Notification effect when locking Screen
(5) Click notification to open the application again
At this time, you should modify the application icon to set the number of unread notifications in the upper right corner to 0, which can be implemented in applicationWillEnterForeground:
Code:
-(Void) applicationWillEnterForeground :( UIApplication *) application {[[UIApplication sharedApplication] setApplicationIconBadgeNumber: 0]; // enter the foreground to cancel the application message icon}
For more information, clickView Source CodeRun the test in person.
For questions or technical exchanges, please join the official QQ group: (452379712)
Author: Jerry Education
Source: http://www.cnblogs.com/jerehedu/
The copyright of this article belongs to Yantai Jerry Education Technology Co., Ltd. and the blog Park. You are welcome to repost it. However, you must keep this statement without the author's consent and provide the original article connection on the article page, otherwise, you are entitled to pursue legal liability.