如何将设备令牌和应用程序版本发送到服务器

2024-05-21

我已经实现将设备令牌和应用程序版本发送到 serverm。它在模拟器(硬编码数据)中工作正常,但在设备中无法工作。 任何形式的帮助将不胜感激。 先感谢您

这是代码

- (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];

    }
}

我刚刚粘贴了当前为我的应用程序运行的代码。只需浏览一下代码即可了解哪里出了问题。以小方法打破 didRegisterForRemoteNotificationsWithDeviceToken 中编写的代码,以便正确理解。 希望这有帮助。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何将设备令牌和应用程序版本发送到服务器 的相关文章

随机推荐

  • 滚动时我的身体背景颜色消失

    我希望有人能帮帮忙 我已将 body height 属性设置为 100 当所有内容同时显示在屏幕上时 这很好 然而 当我需要滚动 最小化窗口时 主体颜色消失 只留下我为 HTML 背景设置的颜色 有人知道解决办法吗 html backgro
  • 删除向导请求响应日志记录

    我想将 dropwizard 中的每个请求和响应记录到不同的文件中 例如我希望所有请求都被登录 var log applicationname request log以及所有回复 var log applicationname respon
  • 有条件地显示按钮

    我知道如何在找到布局 XML 中定义的按钮后启用 禁用它 testBtn Button findViewById R id test 但是除了有条件地加载布局之外 还有没有办法在我的代码中告诉 使用该布局 XML 但不加载那里定义的按钮 要
  • 将扩展配置文件实体添加到 FOS UserBundle

    我正在尝试扩展 FOS UserBundle 以允许扩展配置文件实体除了基本 UserBundle 字段之外还保存其他信息 因为我的网站上有多种类型的用户 所以我创建了单独的实体来保存个人资料信息 我的实体设置如下 class UserPr
  • 使用文件的 AWS Elasticsearch 同义词

    我正在尝试使用文件将同义词添加到弹性搜索 如本链接所示 但它给了我以下错误https www elastic co guide en elasticsearch reference 5 2 analysis synonym tokenfil
  • 使 JScrollPane 在 JList 内部更改时显示滚动条

    我正在尝试使用动态更改 JScrollPane 内的 JList myList setListData someArray 之后 我希望 JScrollPane 根据需要显示滚动条 但这不会发生 谷歌搜索这个问题并没有多大帮助 我尝试了以下
  • 从后台返回时,Android 片段被放置在前一个片段之上

    在我的 MainActivity 中 我有三个片段 还有一个 BottomNavigationView 来处理要显示的片段 这是我的 MainActivity 的 OnCreate 中的内容 fragmentManager beginTra
  • 如何在 Wolfram alpha 中指定函数的域

    有没有办法在 Wolfram alpha 中指定函数的域 我有一个想要绘制的函数 我想将函数 f x y xy 3 x y 限制为 x gt 0 y gt 3 且 y 您可以在 Wolfram Alpha 中指定函数参数的边界 它们更多地是
  • 使用 Ruby 的“open-uri”打开 utf-8 URI 时遇到问题

    我正在尝试使用 ruby 和 open uri 从谷歌地图网络服务 API 获取丹麦位置地址 试图得到丹麦艾勒 http maps googleapis com maps api geocode json address r sensor
  • 如何将条件聚合 mysql 转换为 laravel 查询?

    我的sql查询是这样的 SELECT a number a description MAX CASE WHEN b attribute code brand then b attribute value END as brand MAX C
  • java 8可选替换返回null

    我正在将代码重构为 Java 8 并且想用Optional 替换空检查 public Employee findEmployeeById String id List
  • 在 GraphViz 中将多个短节点与单个高节点并行排列

    我想生成这样的东西 节点的对齐是重要的事情 而不是边缘的角度 V V lt alignment at top gt V gt lt alignment at
  • 猫鼬不创建索引

    我最近开始使用 Mongoose v 3 2 1 但我遇到了索引问题 我在我的模式 Schema path attr index true 上定义了几个索引 但它们不是在数据库中创建的 我在 shell 中运行 db collection
  • Linux 上的用户空间能否实现本机代码的抢占式多任务处理?

    我想知道是否可以在 Linux 用户空间的单个进程中实现本机代码的抢占式多任务处理 也就是说 从外部暂停一些正在运行的本机代码 保存上下文 交换到不同的上下文 然后恢复执行 所有这些都由用户空间精心安排 但使用可能进入内核的调用 我认为这可
  • 更改 Android JellyBean 上的操作栏高度

    我最近一直在开发一个 Android 应用程序 其中我需要为选项卡栏提供自定义布局和尺寸 到目前为止 我所做的方法是使用 Jake Wharton 的 ActionBarSherlock 库来支持 HoneyComb 之前的 Android
  • Rails 3.2:ArgumentError:创建时参数数量错误(2 为 1)

    尝试创建模型的实例时 出现以下错误 u User create or where first or create or where first or initialize ArgumentError wrong number of argu
  • UWP 组合 - 将不透明蒙版应用到 ListView 的顶部 30 像素

    如何将效果应用到 ListView 其中顶部 30 像素从完全透明渐变为完全不透明 这个想法是 当你向下滚动时 顶部的项目逐渐消失 我正在构建一个 UWP 应用程序 其中设计要求 ListView 的顶部 30px 从不透明度 0 开始并过
  • C++ 输出中的微笑

    我现在正在编写 Tic Tac Toe 控制台游戏 我有一个非常有趣的问题 该程序运行良好 但问题出在 Tic Tac Toe 矩阵中 最后一个元素 最后一行 最后一列 是一个微笑 它有时会发生变化 有时是笑意 有时是一颗心 怎么了 整个代
  • React Context 的范围是什么?

    我不清楚在哪里Context可以在 React 应用程序中创建 我可以在组件树中的任何位置创建上下文吗 如果可以 该上下文的范围是否仅限于创建它的子级 或者 Context 本质上是全局的 我可以在文档中的哪里找到这个 案例 我在页面上多次
  • 如何将设备令牌和应用程序版本发送到服务器

    我已经实现将设备令牌和应用程序版本发送到 serverm 它在模拟器 硬编码数据 中工作正常 但在设备中无法工作 任何形式的帮助将不胜感激 先感谢您 这是代码 void application UIApplication applicati