1

我已经在 iOS 上设置了 Urban Airship 推送通知注册和处理,并使用 CURL 终端命令对其进行了测试。但是,我无法使用 ASIFormDataRequest 重现相同的内容。做过这件事的任何人都可以告诉我如何使用 JSON 序列化数据并使用 ASIHTTPRequest 或其他方式发送它吗?

顺便说一句,Urban Airship 无法识别我在他们的 Web 界面上的推送(推送的数量没有增加),但我可以使用这个来验证来自 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];
4

2 回答 2

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];
}
于 2013-03-17T23:32:16.383 回答
-3

Urban Airship 有关于将其 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);
        }
于 2013-03-17T00:48:29.533 回答