如何使用 Objective C 将推送通知的设备令牌和其他用户设置发送到服务器上的 SQL 表

发布于 2024-08-15 23:01:02 字数 145 浏览 4 评论 0原文

理想情况下,我想使用 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

丘比特射中我 2024-08-22 23:01:02

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

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

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

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

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];
爱她像谁 2024-08-22 23:01:02

另一种方式:

NSString * tokenAsString = [[[deviceToken description] 
stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]] 
stringByReplacingOccurrencesOfString:@" " withString:@""];

another way:

NSString * tokenAsString = [[[deviceToken description] 
stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]] 
stringByReplacingOccurrencesOfString:@" " withString:@""];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文