【发布时间】:2012-07-20 12:31:52
【问题描述】:
我正在使用推送通知来通知用户。我已经在真机上测试成功。现在我尝试将它集成到我的应用程序中。我想知道如何获取用户的设备令牌。
我想我可以通过 http 或 ftp 将设备令牌发送到我的服务器并将它们存储在数据库中。服务器负责向所有提供令牌的设备推送通知。这是完成这项工作的正确方法还是有更好的方法?
【问题讨论】:
标签: iphone ios ipad push-notification
我正在使用推送通知来通知用户。我已经在真机上测试成功。现在我尝试将它集成到我的应用程序中。我想知道如何获取用户的设备令牌。
我想我可以通过 http 或 ftp 将设备令牌发送到我的服务器并将它们存储在数据库中。服务器负责向所有提供令牌的设备推送通知。这是完成这项工作的正确方法还是有更好的方法?
【问题讨论】:
标签: iphone ios ipad push-notification
您可以使用此代码打印设备令牌:
- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
NSString *str = [NSString stringWithFormat:@"Device Token=%@",deviceToken];
NSLog(str);
}
欲了解更多信息:CLICK HERE: Tutorial_Programming Apple Push Notification Services
【讨论】:
是的,您需要在服务器端处理它以及您在应用程序图标旁边看到的badge 数字。您可以使用PHP 文件(或任何您熟悉的文件)在用户每次启动应用程序时接收令牌,然后在数据库中将徽章图标设置为 0。对于您发送的每个新通知,您将该字段增加 +1。希望这对你有用。
【讨论】:
如果您仍然没有获得设备令牌,请尝试输入以下代码,以便为您的设备注册推送通知。
它也适用于 ios8 或更高版本。
如果 __IPHONE_OS_VERSION_MAX_ALLOWED >= 80000
if ([UIApplication respondsToSelector:@selector(registerUserNotificationSettings:)]) { UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge|UIUserNotificationTypeAlert|UIUserNotificationTypeSound 类别:无]; [[UIApplication sharedApplication] registerUserNotificationSettings:settings]; [[UIApplication sharedApplication] registerForRemoteNotifications]; } 别的 { [[UIApplication sharedApplication] registerForRemoteNotificationTypes: UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound];
} 否则
[[UIApplication sharedApplication] registerForRemoteNotificationTypes: UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound]; 结束
【讨论】: