【问题标题】:POST to Urban Airship Push Notification iOS ASIFormDataRequestPOST 到 Urban Airship 推送通知 iOS ASIFormDataRequest
【发布时间】:2013-03-01 09:40:20
【问题描述】:

我已经在 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];

【问题讨论】:

  • 我认为这不起作用,因为我没有向 UA 发送电子邮件以允许从设备推送。我已经这样做了,如果解决了我会回答我的问题。
  • 很遗憾,我仍然无法让飞艇识别我的 POST 请求。

标签: http ios6 asihttprequest urbanairship.com


【解决方案1】:

我终于得到了这个工作。我想这是我的 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];
}

【讨论】:

    【解决方案2】:

    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);
            }
    

    【讨论】:

    • 感谢您的回答,但是如果您重新阅读我的问题,“我已经在 iOS 上设置了 Urban Airship 推送通知注册和处理,并使用 CURL 终端命令对其进行了测试。” (即,我已正确设置通知处理,但我现在想通过 iOS 发送推送通知)。
    • 我给的那些函数是针对iOS的AppDelegate文件的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-29
    • 2012-09-19
    相关资源
    最近更新 更多