2013-07-11 13 views
1

我正在尝试为APNS使用Python库,但是我能不知道从哪里获取令牌,有什么帮助吗?如何为我的应用程序获取令牌十六进制

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 

回答

0

您从设备中获得您要发送推送通知;它是application:didRegisterForRemoteNotificationsWithDeviceToken:中的deviceToken参数。

令牌实际上是一个NSData对象(大致相当于一个Python字节字符串),但如果这是您的库所需的,则可以很容易地使用convert that to a hex string

0

在iOS应用中,当你建立你的设备(iPhone/iPad的),你会看到印在控制台中的令牌上运行你的应用程序这种方法添加到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");  
} 

然后..后行

相关问题