如何将设备令牌和应用程序版本发送到服务器 [英] How to send the device token and app version to server

查看:86
本文介绍了如何将设备令牌和应用程序版本发送到服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已实现将设备令牌和应用版本发送到serverm。它在模拟器(硬编码数据)中运行良好,但它不能在设备中工作。
任何形式的帮助将不胜感激。
提前谢谢

I have implemented to send the device token and app version to serverm. It was working fine in simulator(hard coded data) but it is not working in device. Any kind of help would be appreciated. Thank you in advance

这是代码

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)devToken {


    // Get Bundle Info for Remote Registration (handy if you have more than one app)
    NSString *appVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"];

    // Prepare the Device Token for Registration (remove spaces and < >)
    NSString *deviceToken = [[[[devToken description]
                               stringByReplacingOccurrencesOfString:@"<"withString:@""]
                              stringByReplacingOccurrencesOfString:@">" withString:@""]
                             stringByReplacingOccurrencesOfString: @" " withString: @""];

    NSMutableString *urlString = [[BASE_URL mutableCopy] autorelease];
    [urlString appendFormat:@"traceDeviceTokenId?tokenid=%@&version=%@",deviceToken, appVersion];
    NSURL *url = [NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

    NSString *postLength = [NSString stringWithFormat:@"%d", [urlString length]];       
    NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
    [request setURL:url];
    [request setHTTPMethod:@"POST"]; 

    [request setValue:postLength forHTTPHeaderField:@"Content-Length"]; 
    [request setValue:@"text/xml; charset=utf-16" forHTTPHeaderField:@"Content-Type"];
    [request setHTTPBody:[urlString dataUsingEncoding:NSUTF16StringEncoding]];
    NSLog(@"Request xml>>>>>> %@", urlString);

    NSError *error; 
    NSURLResponse *response;
    NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

    NSString *responseXml = [[NSString alloc] initWithData:urlData encoding:NSUTF16StringEncoding];
    NSLog(@"Response xml>>>>>> = %@", responseXml);

}


推荐答案

- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{ 

    NSString *strWithoutSpaces  = [NSString stringWithFormat:@"%@",deviceToken];
    strWithoutSpaces = [strWithoutSpaces stringByReplacingOccurrencesOfString:@" " withString:@""];    
    strWithoutSpaces = [strWithoutSpaces stringByReplacingOccurrencesOfString:@"<" withString:@""];
    strDeviceToken = [strWithoutSpaces stringByReplacingOccurrencesOfString:@">" withString:@""];

    [self createRequestWithDeviceToken:strDeviceToken];     
}

- (void) createRequestWithDeviceToken:(NSString*)deviceToken
{    
        [[UIApplication sharedApplication]setNetworkActivityIndicatorVisible:YES];
        NSString *strURL = [[NSString alloc] initWithFormat:@"%@%@",APNS_DEVICE_REGISTRATION_URL, deviceToken];
        [self prepareConnectionForWebService:strURL delegateHandler:self];
        [strURL release];

}

- (void) prepareConnectionForWebService:(NSString*)webServiceURL delegateHandler:(id)object
{
    //Create HTTP request
    objRequest = [[RequestCreation alloc] init];
    [self.objRequest initRequestWithURL:webServiceURL withMethodType:@"GET" withContentType:@"text/html" andBodyData:nil];

    //Placing URLConnection with request.
    objConnection = [[[ConnectionCreation alloc]init] autorelease];
    self.objConnection.delegate = object;
    [self.objConnection initWithRequest:self.objRequest.objURLRequest];
    [objRequest release];
}

- (void) initWithRequest:(NSMutableURLRequest *)requestedURL
{
    self.urlConnection = [NSURLConnection connectionWithRequest:requestedURL delegate:self];
    if(self.urlConnection)
    {
        receivedData=[[NSMutableData alloc] init];
    }
    else 
    {
        //infiorm the user that the connection is failed
        UIAlertView *alert =[[UIAlertView alloc]initWithTitle:@"Connection Failure" message:@"Unable to connect" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
        [alert show];
        [alert release];

    }
}

我justed粘贴了当前的代码运行我的应用程序。只需通过代码了解你出错的地方。用小方法打破didRegisterForRemoteNotificationsWithDeviceToken中编写的代码,以便正确理解。
希望得到这个帮助。

I justed pasted the code which currently running for my application. Just go through the code for understanding where u went wrong. Break the code written in didRegisterForRemoteNotificationsWithDeviceToken in small methods for proper understanding. Hope this help.

这篇关于如何将设备令牌和应用程序版本发送到服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆