我想在我的应用程序中使用 Apple 推送通知服务。我阅读了几个教程,但仍然不明白一些事情。据说我需要定期向服务器发送设备 token (在我的例子中是服务器用 PHP 编写)。但是我多久发送一次它以及如何生成设备 token ?
提前致谢。
最佳答案
您应该在每次应用启动时注册推送通知(使用 registerForRemoteNotificationTypes
)。
每次更改时,您都应该将设备 token 本地存储在您的应用程序中,并将其发送到您的服务器。
您应该将您从 didRegisterForRemoteNotificationsWithDeviceToken
获得的设备 token 与您存储的最后一个设备 token 进行比较。通过这种方式,您可以节省对服务器的一些调用(因为在实践中设备 token 很少更改)。
这是注册码(摘自 Apple Push Notifications Programming Guide):
- (void)applicationDidFinishLaunching:(UIApplication *)app {
// other setup tasks here....
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)];
}
// Delegation methods
- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)devToken {
const void *devTokenBytes = [devToken bytes];
self.registered = YES;
[self sendProviderDeviceToken:devTokenBytes]; // custom method
}
关于ios - 如果我想实现推送通知,我必须多久向服务器发送一次设备 token ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15966343/