存储令牌推送通知

我们的项目目前有一个Node jS后端,我们想为iOS实现推送通知。 我们做了一些调查,发现我们必须将APN提供给我们的令牌存储在我们的数据库中,以便将推送通知发送到特定设备。 有人可以证实这一点或有没有更好的方式发送通知?

其次,我还发现,当设备通过软件更新,这会改变他们的令牌,这意味着我们必须有能力更新我们的数据库中的令牌,因为它会经常改变。 这也是非常重要的。 还有其他的时候令牌可能会改变吗?

最后,Node中有没有好的库来发送推送通知?

提前致谢!

您必须将通知accessToken发送到服务器,其类似的通知地址。 您不必担心accesstoken中的变化,因为每次login时都必须发送它,以便新的更新的accesstoken将会附加到服务器中。您必须像这样在Appdelegate中注册远程通知,然后将保存的令牌在nsuserdefault中login到loginAPI中的服务器。

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { // Override point for customization after application launch. let settings = UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil) UIApplication.sharedApplication().registerUserNotificationSettings(settings) UIApplication.sharedApplication().registerForRemoteNotifications() return true } //Called if successfully registered for APNS. func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) { // let deviceTokenString = NSString(format: "%@", deviceToken) as String var tokenStr = deviceToken.description tokenStr = tokenStr.stringByReplacingOccurrencesOfString("<", withString: "", options: [], range: nil) tokenStr = tokenStr.stringByReplacingOccurrencesOfString(">", withString: "", options: [], range: nil) tokenStr = tokenStr.stringByReplacingOccurrencesOfString(" ", withString: "", options: [], range: nil) print(deviceToken.description) print(tokenStr) //save the token in NSUserDefaults NSUserDefaults.standardUserDefaults().setObject(deviceTokenString, forKey: "deviceToken") } //Called if unable to register for APNS. func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError) { print(error) } 

参考Apple的文档

设备令牌是将推送通知发送到特定设备上的应用程序的关键。 设备令牌可以改变,所以你的应用程序每次启动时都需要重新注册,并将收到的令牌传回给服务器。 如果您无法更新设备令牌,则远程通知可能无法前往用户的设备。 当用户将备份数据恢复到新设备或计算机或重新安装操作系统时,设备令牌始终会更改。 将数据迁移到新设备或计算机时,用户必须先启动应用程序,然后才能将远程通知传送到该设备。