存储推送通知的令牌

问题描述 投票:0回答:2

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

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

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

提前致谢!

ios node.js swift push-notification apple-push-notifications
2个回答
4
投票

您必须将通知accessToken发送到服务器,它类似于要传递通知的地址。您不必担心 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)
    
}

参考Apple的文档

设备令牌是向应用程序发送推送通知的关键 在特定设备上。设备令牌可能会更改,因此您的应用程序需要 每次启动时重新注册并将收到的令牌传回 到您的服务器。如果更新设备令牌失败,请远程 通知可能无法到达用户的设备。设备 当用户将备份数据恢复到新的时,令牌总是会改变 设备或计算机或重新安装操作系统。迁移时 将数据传输到新设备或计算机时,用户必须启动您的应用程序一次 在远程通知可以传送到该设备之前。

新参考苹果文档

向 APNs 注册您的应用程序并接收全球唯一的设备令牌,该令牌实际上是您的应用程序在当前的地址 设备。您的提供商服务器必须拥有此令牌才能 向设备发送通知。 您注册您的应用程序并每次都会收到您的设备令牌 您的应用程序使用 Apple 提供的 API 启动。报名 跨平台的过程是相似的


1
投票

我一直在寻找最佳解决方案,但似乎没有明确的处理方法,因为我们不知道令牌何时会改变。 这是否意味着我们应该将令牌存储在数据库中并在每次用户打开应用程序时获取它,然后尝试比较它们?我认为这不够有效。

在我看来,将令牌保留在服务器的数据库中,然后在本地存储中保留一份副本,以便您可以将其与每次打开应用程序时生成的令牌进行比较,除非两者不相同,否则您可以更新数据库中的一个。

© www.soinside.com 2019 - 2024. All rights reserved.