Local and Remote Notification

1 關於推送(簡介)服務器

推送的目的是爲了讓不在前臺運行的app使用戶可以知道咱們有信息給他們。好比一條信息,即將到來的約會等。app

推送給用戶的類型有三種ide

Users can get notified in the following ways:fetch

  • An onscreen alert or bannerspa

  • A badge on the app’s icon代理

  • A sound that accompanies an alert, banner, or badgecode

推送分爲兩種,本地通知和遠程推送,二者從用戶角度來講是相同的,但從技術實現角度來說,二者有很大的區別。對象

(1)本地通知:經過安裝在設備上的app設定和負責推送。token

(2)遠程推送:經過自身的服務器,發送通知到Apple Push Notification Center(APNs),而後在經過APNs發送到用戶的設備上。開發


2 本地通知和遠程推送的實現(基本)

2.1本地通知

(1)在iOS8以前的版本中,本地通知是不須要註冊的,可是在iOS8以後的版本,本地通知和遠程通知統一使用UIApplication類中的

registerUserNotificationSettings:方法註冊

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    if([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) {
        //配置通知類型
        UIUserNotificationType types =  UIUserNotificationTypeBadge  | UIUserNotificationTypeSound| UIUserNotificationTypeAlert;
        UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:types categories:nil];
        //調用這個方法後,進入app時系統會彈出對話框來詢問用戶是否接收通知
        //當調用這個方法後,系統會回調AppDelegate中的application:didRegisterUserNotificationSettings:
        [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
    }
    return YES;
}

本地通知使用步驟:

(1)使用上面的方法註冊通知的類型

(2)初始化UILocalNotification對象

(3)設置系統發送該通知的日期和時間,也就是fireDate屬性。若是設置了timeZone屬性爲當地時區,那麼當設備所在的時區改變時(或者從新設置了),系統會自動調整fireData。通常本地通知是基於時間來發送的,iOS8新出了一個region屬性,下面再說。

(4)設置提醒,圖標(右上角小紅點),聲音來提醒用戶。

  1. alert

    再說,iOS8有新特性

  2. applicationIconBadgeNumber 屬性用於顯示圖標右上角小紅點中的數字

  3. soundName 發送通知時的聲音,與上面兩個結合使用 

(5)可選擇的(到下面再說)

(6)iOS8以後,能夠自定義通知的操做。

(7)使用scheduleLocalNotification:方法設定本地通知,該方法會根據fireDate中設置的時間來發送通知,也可使用

presentLocalNotificationNow:當即發送通知。

使用cancelLocalNotification:或者cancelAllLocalNotifications:來用代碼隱藏當前顯示的通知。


2.2遠程推送(使用遠程通知須要申請推送證書,證書稍後再說,能夠選擇第三方極光推送或者百度雲推送,下降開發成本)

設備app向APNs請求device token,獲取後會返回給AppDelegate的代理方法,app須要接收device token,並將其發送給

推送服務器(provider);


使用步驟:(iOS8以後)

(1)使用2.1 (1)中的方法註冊通知類型

(2)調用registerForRemoteNotifications向APNs註冊接收推送

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    if([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) {
        //配置通知類型
        UIUserNotificationType types =  UIUserNotificationTypeBadge  | UIUserNotificationTypeSound| UIUserNotificationTypeAlert;
        UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:types categories:nil];
        //調用這個方法後,進入app時系統會彈出對話框來詢問用戶是否接收通知
        //當調用這個方法後,系統會回調AppDelegate中的application:didRegisterUserNotificationSettings:
        [application registerUserNotificationSettings:settings];
        //註冊遠程通知
        [application registerForRemoteNotifications];
    } else {
        //在iOS8以前,使用registerForRemoteNotificationTypes:方法來代替前兩個步驟
        //iOS8以前的方法
        UIRemoteNotificationType types = UIUserNotificationTypeBadge  | UIUserNotificationTypeSound| UIUserNotificationTypeAlert;
        [application registerForRemoteNotificationTypes:types];
    }
    return YES;
}

(3)成功註冊後,APNsf返回device token,系統回調AppDelegate中的代理方法

(4)將device token提交給推送的服務器(與APNs相連的);

//向APNs註冊,成功後返回deviceToken
-(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken{
    //處理接收到到了deviceToken,須要上傳給服務器(provider);
    ...
    NSLog(@"%ld",[deviceToken length]);
}

//向APNs註冊,失敗返回的結果
-(void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error{
    NSLog(@"%@",error);
}

注意點:

(1)設備離線狀況下,系統不會調用兩個代理方法。

(2)若是設備經過wifi沒法鏈接到APNs的5223端口,也會發生這種狀況。(更換wifi或者使用蜂窩來解決)

服務器推送包括1.device token 2.payload


3通知的處理

用戶收到通知後,會對其進行處理,處理通知分爲如下幾種狀況。

1 發送通知的時候app沒有在前臺運行(此狀況下,系統根據註冊的通知類型來提醒用戶)

(1)在iOS8以後的通知中,用戶點擊了自定義的Action button

系統調用application:handleActionWithIdentifier:forRemoteNotification:completionHandler:或者

application:handleActionWithIdentifier:forLocalNotification:completionHandler:。在兩個方法中,能夠

(2)用戶點擊了默認按鈕或者點擊了app圖標

若是是Local Notification,系統會將Notification傳遞給application:didFinishLaunchingWithOptions:方法

若是是Remote Notification,調用會講payload傳遞給application:didFinishLaunchingWithOptions:同時也會調用

application: didReceiveRemoteNotification:fetchCompletionHandler:方法


2通知發送的時候app運行在前臺


對於以上全部的狀況,實現

application:didReceiveRemoteNotification:fetchCompletionHandler:
  
application:didReceiveLocalNotification:

To handle notification actions.

application:handleActionWithIdentifier:forLocalNotification:completionHandler:

application:handleActionWithIdentifier:forRemoteNotification:completionHandler:
 methods.

四個方法便可


4通知(iOS8以後新增的內容)

(1)自定義Notification Action,iOS8以前,默認只有一個動做。如今咱們能夠爲通知增長自定義的內容。

爲了在app中使用Notification actions,你必須定義這個actions,將他們按照categories分類,而後用UIApplication註冊他們

爲了定義一個notification Action,

一,你必須建立並實例化一個notification class實例,好比UIMutableUserNotfiicationAction。

二。你定義一個identifier,當它處理Action的時候會傳給你的app,同時也定義一個本土化的Action button。

三。設置Action的激活模式,foreground(須要打擾用戶)或者background

四。聲明Action是否具備破壞性,他的按鈕顯示紅色。是否須要用戶輸入密碼




5證書申請


6APNs

5,6有空再寫。

相關文章
相關標籤/搜索