如何使用 Objective C 将推送通知和其他用户设置的设备令牌发送到服务器上的 sql 表 [英] How to use Objective C to send device token for push notifications and other user settings to sql table on server

查看:19
本文介绍了如何使用 Objective C 将推送通知和其他用户设置的设备令牌发送到服务器上的 sql 表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

理想情况下,我想使用 POST 将 HTTP 请求发送到包含设备令牌和一些用户定义设置的推送通知服务器.从那里我可以在服务器上设置一个 php 脚本来处理传入的数据并将其输入到 sql 表中.如果这是唯一的方法,我将如何从 Objective C 发起 HTTP 请求?

Ideally, I would like to send an HTTP Request using POST to the Push Notification Server that contains the device token as well as some user-defined settings. From there I can set up a php script on the server to deal with the incoming data and input it into an sql table. If this is the only way to do it, how would I go about initiating and HTTP Request from Objective C?

推荐答案

您首先需要使用如下函数将设备令牌转换为十六进制字符串:

You'll first need to convert the device token to a hex string with a function like this:

- (NSString*)stringWithDeviceToken:(NSData*)deviceToken {
  const char* data = [deviceToken bytes];
  NSMutableString* token = [NSMutableString string];

  for (int i = 0; i < [deviceToken length]; i++) {
    [token appendFormat:@"%02.2hhX", data[i]];
  }

  return [[token copy] autorelease];
}

然后您需要向您的服务器发出请求:

Then you'll need to make a request to your server:

NSURL* url = [NSURL URLWithString:[NSString stringWithFormat:@"http://example.com/script.php?token=%@", DEVICE_TOKEN]];
NSMutableURLRequest* request = [[[NSMutableRequest alloc] initWithURL:url] autorelease];
NSURLConnection* connection = [NSURLConnection connectionWithRequest:request delegate: self];

这篇关于如何使用 Objective C 将推送通知和其他用户设置的设备令牌发送到服务器上的 sql 表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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