1

I'm trying to use a Python library for APNS but can I don't know from where I should get the token, any help?

from apns import APNs, Payload

apns = APNs(use_sandbox=True, cert_file='cert.pem', key_file='key.pem')

# Send a notification
token_hex = 'b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b87' // ??? This token
payload = Payload(alert="Hello World!", sound="default", badge=1)
apns.gateway_server.send_notification(token_hex, payload)

# Get feedback messages
for (token_hex, fail_time) in apns.feedback_server.items():
    # do stuff with token_hex and fail_time
el.severo
  • 2,202
  • 6
  • 31
  • 62

2 Answers2

1

You get it from the device that you want to send the push notification to; it's the deviceToken parameter in application:didRegisterForRemoteNotificationsWithDeviceToken:.

The token is actually an NSData object (roughly equivalent to a Python byte string), but you can easily convert that to a hex string if that's what your library needs.

Community
  • 1
  • 1
omz
  • 53,243
  • 5
  • 129
  • 141
0

In Your iOS App, add this method to the AppDelegate.m

- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken
{
    NSLog(@"My token is: %@", deviceToken); // this log out the token

    // the following code store the token to app's profile, 
    // you dont need to do this if you dont want to

    NSString *tokenString = [NSString stringWithFormat:@"%@",deviceToken];
    [[NSUserDefaults standardUserDefaults] setObject:tokenString forKey:@"devicetoken"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    NSLog(@"Data saved");    
}

Then when you build and run your app on your device (iphone/ipad) you will see the token printed on console.. the line after

nilveryboring
  • 653
  • 1
  • 9
  • 18