- (void)application:(NSApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
// description
NSLog(@"Device token: %@",deviceToken);
std::string token;
const unsigned char *dataBuffer = (const unsigned char *)[deviceToken bytes];
NSUInteger dataLength = [deviceToken length];
NSMutableString *hexString = [NSMutableString stringWithCapacity:(dataLength * 2)];
for (int i = 0; i < dataLength; ++i)
[hexString appendString:[NSString stringWithFormat:@"%02lx", (unsigned long)dataBuffer[i]]];
NSString* str = [NSString stringWithString:hexString];
if(str)
{
token = std::string([str UTF8String]);
}
//send a request to APNs. Done by server.
std::string path_string = "/3/device/" + token;
NSString *path = [NSString stringWithCString:path_string.c_str() encoding:[NSString defaultCStringEncoding]];
NSLog(@"path: %@",path);
NSData *body;
NSURL *url = [NSURL URLWithString:@"https://api.sandbox.push.apple.com"];
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];
[req setHTTPMethod:@"POST"];
[req addValue:path forHTTPHeaderField: @"path"];
// [req addValue:@".." forHTTPHeaderField: @"authorization"];
[req addValue:@"10" forHTTPHeaderField: @"apns-priority"];
[req setHTTPBody:body];
NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
[[session dataTaskWithRequest: req completionHandler:^(NSData *data, NSURLResponse *response, NSError *error)
{
if (response && ! error) {
NSLog(@"Data: %@", [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding]);
NSLog(@"response: %@", [response description]);
}else {
NSLog(@"ERR: %@", error);
}
}] resume];
}
I get a 404 response with reason as Badpath. However, I generated the path from the device token post registeration with APNS.
Hence, I am a bit confused on what is wrong here?
2023-01-11 16:28:06.109600+0530 pAgentUI[17352:13881932] response: <NSHTTPURLResponse: 0x6000006800c0> { URL: https://api.sandbox.push.apple.com/ } { Status Code: 404, Headers {
"apns-id" = (
"729DCAAB-EEC0-E721-BE2C-AE6A2E797CB2"
);
} }