2013-03-01 61 views
1

我已經設置了Urban Airship推送通知註冊並在iOS上處理,並使用CURL終端命令對其進行了測試。然而,我不能用ASIFormDataRequest重現同樣的事情。誰可以做到這一點向我展示瞭如何JSON序列化數據並使用ASIHTTPRequest發送它?發佈到城市飛艇推送通知iOS ASIFormDataRequest

順便說一句,城市飛艇不承認他們的網絡接口(推的數量沒有增加),對我的推動,但是我能夠使用這個認證從UA得到響應碼200回:

[request setUsername:URBAN_AIRSHIP_APP_KEY]; 
[request setPassword:URBAN_AIRSHIP_APP_SECRET]; 

但我似乎無法讓它發送通知到設備。

編輯:這裏是我的代碼是什麼樣子

NSDictionary *paramDict = 
@{ 
device_tokens: @[@"mytoken",@"mytoken2"], 
aps: @{alert: @"alertText"}, 
extras: @"myExtraParam", 
moreExtras: @"moreExtras" 
}; 

NSData *data = [NSKeyedArchiver archivedDataWithRootObject:paramDict]; 

    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL 
URLWithString:@"https://go.urbanairship.com/api/push/"]]; 
    [request setRequestMethod:@"POST"]; 

    [request appendPostData:data]; 

    [request setUsername:URBAN_AIRSHIP_APP_KEY]; 
    [request setPassword:URBAN_AIRSHIP_APP_SECRET]; 

    [request addRequestHeader:@"Content-Type" value:@"application/json"]; 

    [request setDelegate:self]; 
    [request setDidFinishSelector:@selector(pushSucceeded:)]; 
    [request setDidFailSelector:@selector(pushFailed:)]; 
    [request startAsynchronous]; 
+0

我認爲這不起作用,因爲我沒有通過電子郵件發送UA以允許從設備推送。我已經這樣做了,如果能解決這個問題,我會回答我的問題。 – 2013-03-04 01:18:29

+0

不幸的是,我仍然無法讓飛艇識別我的POST請求。 – 2013-03-12 18:46:04

回答

0

我終於得到了這個工作。我想這是我的JSON序列化的問題。使用NSJSONSerialization工作。

這是我的代碼,其他人試圖讓這個工作。

NSError *error; 
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:paramDict 
                options:0 
                error:&error]; 
if (!jsonData) { 
    NSLog(@"JSON error: %@", error); 
} else { 
    //Do something with jsonData 


    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:@"https://go.urbanairship.com/api/push/"]]; 


    [request appendPostData:jsonData]; 

    [request setUsername:URBAN_AIRSHIP_APP_KEY]; 
    [request setPassword:URBAN_AIRSHIP_APP_SECRET]; 

    [request addRequestHeader:@"Content-Type" value:@"application/json"]; 

    [request setDelegate:self]; 
    [request setDidFinishSelector:@selector(pushSucceeded:)]; 
    [request setDidFailSelector:@selector(pushFailed:)]; 
    [request startAsynchronous]; 
} 
-3

城市飛艇在它的SDK集成到您的代碼文檔。但這裏有一些提示讓你走。

您應該重寫應用程序的AppDelegate文件中執行類似操作的以下功能。 UAirship是Urban Airship SDK的經理級別。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
     {  
       // push notification 
       //Init Airship launch options 
       NSMutableDictionary *takeOffOptions = [[[NSMutableDictionary alloc] init] autorelease]; 
       [takeOffOptions setValue:launchOptions forKey:UAirshipTakeOffOptionsLaunchOptionsKey]; 

       // Create Airship singleton that's used to talk to Urban Airship servers. 
       // Please populate AirshipConfig.plist with your info from http://go.urbanairship.com 
       [UAirship takeOff:takeOffOptions]; 
       ... 
       } 

     - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { 
      // Updates the device token and registers the token with UA 
      NSLog(@"Registering for Devicetoken"); 
      [[UAirship shared] registerDeviceToken:deviceToken]; 

      }   

     - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo { 
      NSLog(@"Received remote notification: %@", userInfo); 

      // possibly throw alert box to show the payload data in the userInfo object. 
      }    

     - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *) error { 
      NSLog(@"Failed To Register For Remote Notifications With Error: %@", error); 
     } 
+0

感謝您的回答,但是如果您重讀我的問題,「我已經設置了Urban Airship推送通知註冊並在iOS上處理,並使用CURL終端命令進行了測試。」 (即我的通知處理設置正確,但我想現在發送推送通知通過iOS)。 – 2013-03-17 18:33:51

+0

我給出的這些函數是iOS的AppDelegate文件。 – 2013-03-19 22:27:33