How to use Objective C to send device token for push notifications and other user settings to sql table on server

試著忘記壹切 提交于 2019-11-28 19:23:28

问题


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?


回答1:


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



回答2:


another way:

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


来源:https://stackoverflow.com/questions/1959600/how-to-use-objective-c-to-send-device-token-for-push-notifications-and-other-use

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!