iOS消息机制

一、本地推送

iOS 推送通知分为本地推送和远程推送通知。远程推送通知就相似于我们平时使用微信时。即使锁屏了,也能收到好友发送给我们的消息。然后在主屏幕显示一个alertview。远程推送须要远程服务端的支持。比較复杂. 本地推送相对照较简单,不须要服务端的支持。

本地通知是NSLocalNotification 实现的,通过实例化一个NSLocalNotification类型的通知,同一时候设置通知的fireDate 属性,即通知的触发时间。设置timeZone属性。即时区;设置alertBody,显示的内容。设置alertAction;设置soundName,即推送发生时的声音;设置applicationIconBadgeNumber。即图标上的数字;设置userInfo属性,该属性是一个NSDictionary类型的变量。然后在使用UIApplication 的 实例方法scheduleLocalNotification:或 presentLocalNotificationNow: 推送通知。

* 1、创建本地推送 *

// 创建一个本地推送  
UILocalNotification *notification = [[[UILocalNotification alloc] init] autorelease];  
//设置10秒之后  
NSDate *pushDate = [NSDate dateWithTimeIntervalSinceNow:10];  
if (notification != nil) {  
    // 设置推送时间  
    notification.fireDate = pushDate;   
    //推送时区设置:从网上搜到 
    //timeZone是UILocalNotification激发时间是否依据时区改变而改变,假设设置为nil的话。
    //那么UILocalNotification将在一段时候后被激发,而不是某一个确切时间被激发。

notification.timeZone = [NSTimeZone defaultTimeZone]; // 设置反复间隔,若不设置将仅仅会推送1次 notification.repeatInterval = kCFCalendarUnitDay; // 推送声音,(若不设置的话系统推送时会无声音) notification.soundName = UILocalNotificationDefaultSoundName; // 推送内容,(若不设置,推送中心中不显示文字,有声音提示前提是设置有声音) notification.alertBody = @"推送内容"; //推送时小图标的设置,PS:这个东西不知道还有啥用 notification.alertLaunchImage=[[NSBundle mainBundle]pathForResource:@"3" ofType:@"jpg"]; //显示在icon上的红色圈中的数子 notification.applicationIconBadgeNumber = 1; //设置userinfo 方便在之后须要撤销的时候使用 NSDictionary *info = [NSDictionary dictionaryWithObject:@"name"forKey:@"key"]; notification.userInfo = info; //讲推送设置以及信息增加 UIApplication* app=[UIApplication sharedApplication]; BOOL status=YES; for (UILocalNotification* notification in app.scheduledLocalNotifications) { if ([notification.userInfo objectForKey:@"key"]) { status=NO; } } if (status) { //增加推送(仅仅能增加一次) [app scheduleLocalNotification:notification]; } }

* 2、接收本地推送 *

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification*)notification{  
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"iWeibo" message:notification.alertBody delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];  
    [alert show];  
    // 图标上的数字减1  
    application.applicationIconBadgeNumber -= 1;  
} 

* 3、解除本地推送 *

// 获得 UIApplication  
UIApplication *app = [UIApplication sharedApplication];  
//获取本地推送数组  
NSArray *localArray = [app scheduledLocalNotifications];  
//声明本地通知对象  
UILocalNotification *localNotification;  
if (localArray) {  
    for (UILocalNotification *noti in localArray) {  
        NSDictionary *dict = noti.userInfo;  
        if (dict) {  
            NSString *inKey = [dict objectForKey:@"key"];  
            if ([inKey isEqualToString:@"相应的key值"]) {  
                if (localNotification){  
                    [localNotification release];  
                    localNotification = nil;  
                }  
                localNotification = [noti retain];  
                break;  
            }  
        }  
    }  

    //推断是否找到已经存在的同样key的推送  
    if (!localNotification) {  
        //不存在初始化  
        localNotification = [[UILocalNotification alloc] init];  
    }  

    if (localNotification) {  
        //不推送 取消推送  
        [app cancelLocalNotification:localNotification];  
        [localNotification release];  
        return;  
    }  
}

二、远程推送

阅读參考链接。

* 參考链接 *

本地推送

  1. http://my.oschina.net/CarlHuang/blog/139104

远程推送:

  1. http://blog.csdn.net/enuola/article/details/8627283
  2. http://www.cnblogs.com/yh-qfnu/p/3269768.html
  3. http://www.cocoachina.com/ios/20100401/900.html
  4. http://blog.csdn.net/dalehui/article/details/16807157
  5. http://www.raywenderlich.com/32960/apple-push-notification-services-in-ios-6-tutorial-part-1
  6. http://www.raywenderlich.com/32963/apple-push-notification-services-in-ios-6-tutorial-part-2
  7. http://mobiforge.com/design-development/programming-apple-push-notification-services
  8. http://segmentfault.com/a/1190000000520755
  9. 极光推送文档
原文地址:https://www.cnblogs.com/gcczhongduan/p/5215765.html