2017-01-13 2 views
6

OneSignal 설명서를 확인했지만 사용자가 알림과 상호 작용할 때 Swift를 사용하여 iOS Native SDK에서 게시물 알림의 추가 데이터 (예 : postID, userID, type)를 사전 설정하는 방법을 결정하고 리디렉션하는 방법을 명확하게 이해하지 못했습니다. 나는 단지 그렇게하고 있어요 게시신호 알림의 추가 데이터를 게시하고 수신하는 방법은 무엇입니까?

:

OneSignal.sendTag("username", value: "\(user)") 
OneSignal.postNotification(["contents": ["en": "@\(user) added an additive to your '\(title)' experience: \"\(strLast)\""], 
                   "include_player_ids": [postOwnerPlayerID], 

수신 :

OneSignal.initWithLaunchOptions(launchOptions, appId: "______", handleNotificationReceived: nil, handleNotificationAction: { 
     (result) in 

     // This block gets called when the user reacts to a notification received 
     let payload = result?.notification.payload 

     //Try to fetch the action selected 
     if let additionalData = payload?.additionalData { 

      print("payload") 
      print(additionalData) 
     } 

     // After deciding which action then I can redirect user.. 

     let username: String? = UserDefaults.standard.string(forKey: KEY_UID) 

     if username != nil { 

      if let tabbarController = self.window!.rootViewController as? UITabBarController { 
       tabbarController.selectedViewController = tabbarController.viewControllers?[2] 
       // NotificationCenter.default.post(name: Foundation.Notification.Name(rawValue: "notificationsUp"), object: nil) 
      } 
     } 

    }, settings: [kOSSettingsKeyInFocusDisplayOption : OSNotificationDisplayType.none.rawValue]) 

답변

6

당신은 다음과 같은 OneSignal.postNotification에 전달 된 사전에서 키로 data 필드를 설정합니다.

OneSignal.postNotification(["contents": ["en": "Test Message"], 
          "include_player_ids": ["3009e210-3166-11e5-bc1b-db44eb02b120"], 
          "data": ["postID": "id"]]) 

그런 다음 당신은 handleNotificationAction 함수에서 payload에서 additionalData에서 키를 준비해야합니다. objC에 추가 데이터를 전송하는 아이폰 OS에서

if let additionalData = payload?.additionalData { 
    let postID: String? = additionalData["postID"] 
} 
+0

감사합니다! – sputn1k

4

예 ...

[OneSignal postNotification:@{@"contents":@{@"en":text}, 
           @"include_player_ids":oneSignalIds, 
           @"data":@{@"key": @"value"}, 
           }]; 

그리고 데이터를 수신 할 수 ...

[OneSignal initWithLaunchOptions:launchOptions 
          appId:ONESIGNAL_APPID 
     handleNotificationReceived:^(OSNotification *notification) { 

      if (notification.payload.additionalData) { 

       NSDictionary* additionalData = notification.payload.additionalData; 

       if (additionalData[@"key"]){ 
        NSLog(@"Received Data - %@", additionalData[@"key"]); 
       } 
      } 
     } 

     handleNotificationAction:nil 
         settings:@{kOSSettingsKeyInAppAlerts:@YES}]; 

은 누군가가

0
을 :) 도움이되기를 바랍니다

@jkasten 덕분에 올바른 방향으로 나를 도왔습니다! 나는 AnyHashable 경고를 제거하는 것을 도왔다.

스위프트 3 코드합니다 (additionalData 매개 변수 변경 PATH는 출력하고자) : 나에게 도움이

let PATH = notification!.payload.additionalData["PATH"] 
print("PATH: ",PATH as Any)