天天看點

Apple Watch Kit(2)- Actionable NotificationsActionable Notifications

Actionable Notifications

​整理的内容有不夠準确的,望大家積極排雷交流指正。
      

Actionable Notifications是什麼

Actionable Notifications是watchKit專門為Local Notification和remote Notificatio提供的接口。
      

Actionable Notifications分為兩種運作方式:

靜态(static)和動态(dynamic)運作方式。

* 靜态運作方式:

    隻能展示靜态内容,在storyboard中可以自定義添加其他控件,可以響應通知按鈕回調。隻是靜态的加載static interface controller。Notification interface controller類中各個回調方法都不會走,真不愧是靜态呀。(冷加載)

* 動态運作方式:

    Notification interface controller 類中各個回調方法都會走,可以在接收到推送的回調方法中,設定storyboard中動态推送UI。(熱加載)


    接收到推送的回調方法如下代碼:

        /**
         *@description  接收Remote Notification回調
         *@param        remoteNotification 推送内容
         *@param        回調
         *@returns void
         */
        ///接收到推送,展示前調用
        - (void)didReceiveRemoteNotification:(NSDictionary *)remoteNotification withCompletion:(void (^)(WKUserNotificationInterfaceType))completionHandler {

            //獲得apns推送内容,可更新storyboard中 dynamic ntoficaiotn controller 的 UI


            //設定采用那個ntoficaiotn controller 去展示推送的資料。
            //static 預設的
            completionHandler(WKUserNotificationInterfaceTypeDefault);
            //dynamic 自定義的
            //completionHandler(WKUserNotificationInterfaceTypeCustom);

        }

    Local notification對應回調方法:

        - (void)didReceiveLocalNotification:(UILocalNotification *)localNotification withCompletion:(void (^)(WKUserNotificationInterfaceType))completionHandler {

        }
      

如何設定靜态和動态運作?

Apple Watch Kit(2)- Actionable NotificationsActionable Notifications

Apple Watch Kit(2)- Actionable NotificationsActionable Notifications

* 推送展示的UI上的按鈕是由推送内容(josn)定制的,如下定制了兩個按鈕。

        "WatchKit Simulator Actions": [
                               {
                               "title": "First Button",
                               "identifier": "firstButtonAction"
                               }

                               ,{
                               "title": "Second Button",
                               "identifier": "secondButtonAction"
                               }

                               ],
      

響應推送按鈕事件

無論靜态還是動态方式都可以自定義多個按鈕,dismiss 按鈕是系統定制,無法修改,點選後不會走如下回調。
      

在main app 主controller中的對應的回調方法:

  • 遠端推送(remote notificaiton):
    #pragma mark - Notification Action
          //remote (identifier:事件id;remoteNotification:推送内容)
          - (void)handleActionWithIdentifier:(NSString *)identifier
                       forRemoteNotification:(NSDictionary *)remoteNotification{
              NSArray* identifiersArr = @[@"firstButtonAction",@"secondButtonAction"];
              NSInteger tag = NSNotFound;
              if ([identifiersArr containsObject:identifier]) {
                  tag = [identifiersArr indexOfObject:identifier];
              }
              switch (tag) {
                  case 0:{
                      NSLog(@"firstButtonAction is taped");
                      break;
                  }
                  case 1:{
                      NSLog(@"secondButtonAction is taped");
                      break;
                  }
                  default:{
                      NSLog(@"未找到action 對應的 identifier:%@",identifier);
                      break;
                  }
              }
          }
          
  • 本地推送(remote notificaiton):
    //local (identifier:事件id;remoteNotification:推送内容)
          - (void)handleActionWithIdentifier:(NSString *)identifier
                        forLocalNotification:(NSDictionary *)remoteNotification
                        {
          }
          

notificaiton controller 上不可添加的UI控件(靜态動态都不可以)

UIButton、