简体   繁体   English

存储用于推送通知的令牌

[英]Storing tokens for Push Notifications

our project currently has a Node jS backend and we want to implement push notifications for iOS. 我们的项目目前有一个Node jS后端,我们想为iOS实现推送通知。 We did some research and figured out that that we will have to store the tokens that APN gives us in our DB in order to send push notifications to specific devices. 我们进行了一些研究,发现为了将推送通知发送到特定设备,我们必须将APN提供给我们的令牌存储在数据库中。 Can someone confirm this or is there a better way of sending notifications? 有人可以确认吗?还是有更好的发送通知的方式?

Secondly, I also found that when devices go through software updates that this changes their token so does that mean we must have capability to update the token in our DB because it will change often. 其次,我还发现,当设备进行软件更新时,这会更改其令牌,这是否意味着我们必须具有更新数据库中令牌的能力,因为它会经常更改。 This is also pretty important. 这也很重要。 Is there also any other times that the token might change? 还有其他时间令牌可能会更改吗?

Lastly, are there any good libraries in Node for sending push notifications? 最后,Node中是否有用于发送推送通知的良好库?

Thanks in advance! 提前致谢!

You must send the notification accessToken to the server, Its like address for notification to be delivered. 您必须将通知accessToken发送到服务器,其类似地址才能发送通知。 You dont have to worry about the variation in the accesstoken because you have to send it when you login everytime so the new updated accesstoken will append in server too.You have to register for remote notifiation in your Appdelegate like this and later send the saved token in nsuserdefault to the server in login API. 您不必担心accesstoken的变化,因为每次登录时都必须发送它,这样新的更新的accesstoken也将附加在服务器中。您必须像这样在Appdelegate中注册远程通知,然后再发送已保存的令牌nsuserdefault中的服务器登录API中。

 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)

}

Reference Apple's Documentation 参考Apple的文档

The device token is your key to sending push notifications to your app on a specific device. 设备令牌是将推送通知发送到特定设备上的应用程序的关键。 Device tokens can change, so your app needs to reregister every time it is launched and pass the received token back to your server. 设备令牌可能会更改,因此您的应用在每次启动时都需要重新注册,并将接收到的令牌传递回服务器。 If you fail to update the device token, remote notifications might not make their way to the user's device. 如果您无法更新设备令牌,则远程通知可能不会进入用户的设备。 Device tokens always change when the user restores backup data to a new device or computer or reinstalls the operating system. 当用户将备份数据还原到新设备或计算机或重新安装操作系统时,设备令牌始终会更改。 When migrating data to a new device or computer, the user must launch your app once before remote notifications can be delivered to that device. 将数据迁移到新设备或计算机时,用户必须先启动您的应用程序,然后才能将远程通知传递到该设备。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM