iOS訊息機制

躍然發表於2015-03-31

一、本地推送

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. 極光推送檔案

相關文章