IOS Development Series-notification and message mechanisms

Source: Internet
Author: User
Tags notification center

IOS Development Series-notification and message mechanisms
Overview In most mobile applications, only one application can be active at any time, if some users are interested in other applications at the moment, the notification mechanism can be used to tell users what happened at this time. In iOS, the notification mechanism is also called the message mechanism. There are two types: Local notifications and remote notifications. The two types of notifications are consistent in iOS. You can use banners or pop-up reminders to notify users. clicking a notification can open the application, but the implementation principle is completely different. Today, let's take a look at how to implement these two mechanisms in iOS. The content of the notification center will be added later in the article to avoid confusion between the two concepts. Local notifications are triggered by local applications. Local notifications are based on time behaviors, such as scheduled alarms and to-do reminders, or if an application is not used for a period of time, it usually prompts the user to use this application and so on. It is a local notification. To create a local notification, follow these steps: Create a UILocalNotification. Set the time when the notification is processed by fireDate. Configure the notification content: Notification body, notification sound, icon number, and so on. Configure the Custom Data parameter userInfo for notification transmission (this step is optional ). Call Notification. You can use scheduleLocalNotification to schedule a notification as planned, or use presentLocalNotificationNow to call the notification immediately. Next we will take a reminder that the user has not been using the program for a long time as an example to give a simple understanding of local notifications. This process does not involve too many interface operations, and all logic is in AppDelegate: if no notification is registered after the application is entered, you must first register the notification to request the user to allow the notification; once the registration method is called, the application's-(void) application :( UIApplication *) application didRegisterUserNotificationSettings :( UIUserNotificationSettings *) notificationSettings proxy method will be called no matter whether the user chooses to allow the notification, in this method, based on the user's choice: if the notification is allowed, the notification will be created according to the previous steps and executed after a certain period of time. AppDelegate. m /// AppDelegate. m // LocalNotification /// Created by Kenshin Cui on 14/03/28. // Copyright (c) 2014 Kenshin Cui. all rights reserved. // # import "AppDelegate. h "# import" KCMainViewController. h "@ interface AppDelegate () @ end @ implementation AppDelegate # pragma mark-application proxy method-(BOOL) application :( UIApplication *) application didfinishlaunchingwitexceptions :( NSDictionary *) launchOptions {_ w Indow = [[UIWindow alloc] initWithFrame: [UIScreen mainScreen]. bounds]; _ window. backgroundColor = [UIColor colorwithred: 249/255. 0 green: 249/255. 0 blue: 249/255. 0 alpha: 1]; // set the global navigation bar style and color [[UINavigationBar appearance] setBarTintColor: [UIColor colorWithRed: 23/255. 0 green: 180/255. 0 blue: 237/255. 0 alpha: 1]; [[UINavigationBar appearance] setBarStyle: UIBarStyleBlack]; KCMainViewController * mainController = [[K CMainViewController alloc] init]; _ window. rootViewController = mainController; [_ window makeKeyAndVisible]; // if you have been authorized to send notifications, create a local notification; otherwise, request authorization (note: if no authorization is requested, there is no corresponding notification setting item in the settings. That is to say, if no request has been sent, the message can not be opened even through the settings) if ([[UIApplication sharedApplication] currentUserNotificationSettings]. types! = Notice) {[self addLocalNotification];} else {[[UIApplication sharedApplication] registerUserNotificationSettings: [UIUserNotificationSettings failed: Required | UIUserNotificationTypeBadge | uiusernotiftypetypesound categories: nil];} return YES ;} # pragma mark is executed after calling the user registration notification method (that is, after calling registerUserNotificationSettings: method)-(void) application :( UIApplicat Ion *) application didRegisterUserNotificationSettings :( UIUserNotificationSettings *) icationicationsettings {if (notificationSettings. types! = UIUserNotificationTypeNone) {[self addLocalNotification] ;}# pragma mark sets the message information after entering the foreground-(void) applicationWillEnterForeground :( UIApplication *) application {[UIApplication sharedApplication] setApplicationIconBadgeNumber: 0]; // enter the foreground to cancel the application message icon} # pragma mark-Private method # pragma mark add local notification-(void) addLocalNotification {// defines the local notification object UILocalNotification * notification = [[UILocalNotification alloc] init]; // sets the call time of the specified IC. Ation. fireDate = [NSDate dateWithTimeIntervalSinceNow: 10.0]; // notification trigger time after 10 s. repeatInterval = 2; // Number of notification repetitions // notification. repeatCalendar = [NSCalendar currentCalendar]; // the current calendar. It is best to set the time zone and other information before use so that the time can be automatically synchronized // set the notification attribute notification. alertBody = @ "recently added many interesting features. Do you want to try it now? "; // Notification body notification. 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 = UILocalNotificationDefaultSoundName; // The Playing sound when the notification is received. The default message sound notification is used. soundName = @ "msg. caf "; // notification sound (requires a real machine to hear the sound) // set user information notification. userInfo = @ {@ "id": @ 1, @ "user": @ "Kenshin Cui "}; // other additional information bound to the notification // call the notification [[[UIApplication sharedApplication] scheduleLocalNotification: notification] ;}# pragma mark remove the local notification, if you do not need this notification, remember to remove-(void) removeNotification {[[UIApplication sharedApplication] cancelAllLocalNotifications];}

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.