IOS message mechanism

Source: Internet
Author: User
Tags home screen

IOS message mechanism
1. Local push

IOS push notifications can be divided into local push and remote push notifications. Remote push notifications are similar to messages sent to us by friends even when we lock the screen, then an alertview is displayed on the home screen. Remote push requires support from the remote server, which is complicated. local push is relatively simple and does not require server support.

Local notifications are implemented by NSLocalNotification. By instantiating an NSLocalNotification type notification, you can also set the fireDate attribute of the notification, that is, the trigger time of the notification. You can also set the timeZone attribute and the instant zone, and set the alertBody, display content; Set alertAction; Set soundName, that is, the sound when the push occurs; Set applicationIconBadgeNumber, that is, the number on the icon; Set userInfo attribute, which is a NSDictionary type variable. Then, use the instance method scheduleLocalNotification: Or presentLocalNotificationNow: push notification in UIApplication.

* 1. Create a local push *

// Create a local push UILocalNotification * notification = [[[UILocalNotification alloc] init] autoreldate]; // set NSDate * pushDate = [NSDate dateWithTimeIntervalSinceNow: 10] after 10 seconds; if (notification! = Nil) {// set the push time notification. fireDate = pushDate; // push time zone settings: found from the Internet // timeZone indicates whether the UILocalNotification excitation time is changed according to the time zone. If it is set to nil, // then, UILocalNotification will be triggered after a period of time, instead of a specific time. Notification. timeZone = [NSTimeZone defaultTimeZone]; // sets the repetition interval. If this parameter is not set, only one notification is pushed. repeatInterval = kCFCalendarUnitDay; // push the sound. (If this parameter is not set, no sound is generated during system push.) notification. soundName = UILocalNotificationDefaultSoundName; // push content. (If this parameter is not set, text is not displayed in the push center. If there is a sound prompt, the notification is enabled. alertBody = @ push content; // set the push time icon. PS: I don't know how to use notification. alertLaunchImage = [[NSBundle mainBundle] pathForResource: @ 3 ofType: @ jpg]; // The number of notifications displayed in the Red Circle of the icon. applicationIconBadgeNumber = 1; // set userinfo to enable NSDictionary * info = [NSDictionary dictionaryWithObject: @ nameforKey: @ key]; notification. userInfo = info; // Add push settings and information to UIApplication * app = [UIApplication sharedApplication]; BOOL status = YES; for (UILocalNotification * notification in app. scheduledlocalconfigurications) {if ([notification. userInfo objectForKey: @ key]) {status = NO ;}} if (status) {// Add to push (only once) [app scheduleLocalNotification: notification] ;}}

* 2. Receive local push *

-(Void) application :( UIApplication *) application didReceiveLocalNotification :( UILocalNotification *) notification {UIAlertView * alert = [[UIAlertView alloc] initWithTitle: @ iweio message: notification. alertBody delegate: nil cancelButtonTitle: @ confirm otherButtonTitles: nil]; [alert show]; // the number on the icon minus 1 application. applicationIconBadgeNumber-= 1 ;}

* 3. Release local push *

// Obtain UIApplication * app = [UIApplication sharedApplication]; // obtain the local push array NSArray * localArray = [app scheduledlocalconfigurations]; // declare the local notification object UILocalNotification * localNotification; if (localArray) {for (UILocalNotification * noti in localArray) {NSDictionary * dict = noti. userInfo; if (dict) {NSString * inKey = [dict objectForKey: @ key]; if ([inKey isEqualToString: @ key value]) {if (loca LNotification) {[localNotification release]; localNotification = nil;} localNotification = [noti retain]; break ;}}// determine whether to find the push if (! LocalNotification) {// initialization localNotification = [[UILocalNotification alloc] init] does not exist;} if (localNotification) {// do not push cancel push [app cancelLocalNotification: localNotification]; [localNotification release]; return ;}}
 

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.