iOS8自定義推送顯示按鈕及推送優化

http://www.jianshu.com/p/803bfaae989ecss

iOS8自定義推送顯示按鈕及推送優化

字數1435 閱讀473 評論0
導語

在iOS8中,推送消息再也不只是簡單地點擊打開客戶端,對推送消息下拉時還能夠執行預先設定好的操做,接下來咱們來介紹如何自定義推送信息顯示按鈕和對推送的一些優化策略。git

註冊推送

在iOS8中,咱們使用新的函數來註冊通知,以下:github

- (void)registerForRemoteNotifications NS_AVAILABLE_IOS(8_0);

該函數的做用是向蘋果服務器註冊該設備,註冊成功事後會回調json

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken NS_AVAILABLE_IOS(3_0);

註冊失敗則回調數組

- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error NS_AVAILABLE_IOS(3_0);

執行registerForRemoteNotifications只是完成了與APNS的註冊交互,接下來還要設置推送的類型和策略。若是沒有設置則接收到的消息都是以靜默的方式接收。服務器

設置推送類型

- (void)registerUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings NS_AVAILABLE_IOS(8_0);

在這裏咱們用到了UIUserNotificationSettings這個新類,蘋果對其解釋是這樣的。app

A UIUserNotificationSettings object encapsulates the types of notifications that can be displayed to the user by your app. Apps that use visible or audible alerts in conjunction with a local or push notification must register the types of alerts they employ. UIKit correlates the information you provide with the user’s preferences to determine what types of alerts your app is allowed to employ.ide

簡單來講就是咱們能夠自行註冊推送的提醒類型。再來看看UIUserNotificationSettings爲咱們提供了那些函數。查看API,咱們只找到了函數

+ (instancetype)settingsForTypes:(UIUserNotificationType)types categories:(NSSet *)categories;

UIUserNotificationType就是以往咱們設定的推送聲音、推送數量Badge和是否Alter的參數組合。由於它們是NS_OPTIONS類型,因此是能夠多選的。工具

咱們重點放在categories上。能夠看到,categories的類型是一個集合(NSSet *),也就是說咱們能夠設置多個推送策略。繼續查找API,咱們找到了UIUserNotificationCategory。蘋果的說明是這樣的

A UIUserNotificationCategory object encapsulates information about custom actions that your app can perform in response to a local or push notification. Each instance of this class represents a group of actions to display in conjunction with a single notification. The title of each action is uses as the title of a button in the alert displayed to the user. When the user taps a button, the system reports the selected action to your app delegate.

就是說咱們對每一條推送信息能夠設置一組行爲,行爲以按鈕方式顯示。當咱們點擊按鈕時會調用app delegate的代理方法。
查看UIUserNotificationCategory相關屬性

@property (nonatomic, copy, readonly) NSString *identifier;
  • identifier:策略標識,在推送時用來決定客戶端顯示哪一種推送策略,稍後會介紹。

看到該屬性是隻讀的,咱們在自定義策略時使用的是UIMutableUserNotificationCategory來設置,設置方法以下:

- (void)setActions:(NSArray *)actions forContext:(UIUserNotificationActionContext)context;

對一個策略咱們能夠設置多個行爲,使用的是UIUserNotificationAction。

查看UIUserNotificationAction相關屬性

// The unique identifier for this action. @property (nonatomic, copy, readonly) NSString *identifier; // The localized title to display for this action. @property (nonatomic, copy, readonly) NSString *title; // How the application should be activated in response to the action. @property (nonatomic, assign, readonly) UIUserNotificationActivationMode activationMode; // Whether this action is secure and should require unlocking before being performed. If the activation mode is UIUserNotificationActivationModeForeground, then the action is considered secure and this property is ignored. @property (nonatomic, assign, readonly, getter=isAuthenticationRequired) BOOL authenticationRequired; // Whether this action should be indicated as destructive when displayed. @property (nonatomic, assign, readonly, getter=isDestructive) BOOL destructive;
  • identifier:行爲標識符,用於調用代理方法時識別是哪一種行爲。
  • title:行爲名稱。
  • UIUserNotificationActivationMode:即行爲是否打開APP。
  • authenticationRequired:是否須要解鎖。
  • destructive:這個決定按鈕顯示顏色,YES的話按鈕會是紅色。

一樣因爲這些屬性都是隻讀的,咱們使用UIMutableUserNotificationAction來生成自定義行爲。

編碼

咱們設置兩種推送策略,每種策略分別設置兩種行爲。代碼以下:

if(8.0 <= [UIDevice currentDevice].systemVersion.doubleValue) { [[UIApplication sharedApplication] registerForRemoteNotifications]; UIMutableUserNotificationAction * action1 = [[UIMutableUserNotificationAction alloc] init]; action1.identifier = @"action1"; action1.title=@"策略1行爲1"; action1.activationMode = UIUserNotificationActivationModeForeground; action1.destructive = YES; UIMutableUserNotificationAction * action2 = [[UIMutableUserNotificationAction alloc] init]; action2.identifier = @"action2"; action2.title=@"策略1行爲2"; action2.activationMode = UIUserNotificationActivationModeBackground; action2.authenticationRequired = NO; action2.destructive = NO; UIMutableUserNotificationCategory * category1 = [[UIMutableUserNotificationCategory alloc] init]; category1.identifier = @"Category1"; [category1 setActions:@[action2,action1] forContext:(UIUserNotificationActionContextDefault)]; UIMutableUserNotificationAction * action3 = [[UIMutableUserNotificationAction alloc] init]; action3.identifier = @"action3"; action3.title=@"策略2行爲1"; action3.activationMode = UIUserNotificationActivationModeForeground; action3.destructive = YES; UIMutableUserNotificationAction * action4 = [[UIMutableUserNotificationAction alloc] init]; action4.identifier = @"action4"; action4.title=@"策略2行爲2"; action4.activationMode = UIUserNotificationActivationModeBackground; action4.authenticationRequired = NO; action4.destructive = NO; UIMutableUserNotificationCategory * category2 = [[UIMutableUserNotificationCategory alloc] init]; category2.identifier = @"Category2"; [category2 setActions:@[action4,action3] forContext:(UIUserNotificationActionContextDefault)]; UIUserNotificationSettings *uns = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound) categories:[NSSet setWithObjects: category1,category2, nil]]; [[UIApplication sharedApplication] registerUserNotificationSettings: uns]; }

關於推送證書製做請百度,這裏推薦一個推送測試工具

  • 推送策略一
    {
          "aps":{ "alert":"推送內容", "sound":"default", "badge":0, "category":"Category1" } }
    顯示結果:

    策略一
  • 推送策略二
    {
          "aps":{ "alert":"推送內容", "sound":"default", "badge":0, "category":"Category2" } }
    顯示結果:

    策略二

點擊相應按鈕會激活代理方法

- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo completionHandler:(void(^)())completionHandler NS_AVAILABLE_IOS(8_0);

這裏咱們根據傳過來的identifier肯定點擊了哪一個按鈕,並執行相應操做。

推送優化

蘋果APNS對推送內容大小限制不能超過256個字節(如今這個限制好像放寬了)。若是推送消息內容過多,不只會形成推送延遲,還會消耗流量。對於推送信息中重複的文本內容,咱們能夠在本地字符串strings中自定義鍵值動態設定參數來完成推送。

Localizable.strings中添加:

"pushkey" = "%@ 的iOS8自定義推送顯示按鈕及推送優化教程 %@是一名iOS開發者,正在前行。";

設置推送信息

{
     "aps":{ "alert":{"loc-args":["Arms","Arms"],"loc-key":"pushkey"}, "sound":"default", "badge":0, "category":"Category2" } }

顯示結果:


本地字符串推送
相關文章
相關標籤/搜索