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

前端 未结 2 442
不知归路
不知归路 2020-12-24 04:14

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 c

相关标签:
2条回答
  • 2020-12-24 04:27

    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];
    
    0 讨论(0)
  • 2020-12-24 04:41

    another way:

    NSString * tokenAsString = [[[deviceToken description] 
    stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]] 
    stringByReplacingOccurrencesOfString:@" " withString:@""];
    
    0 讨论(0)
提交回复
热议问题