Sampath Duddu
Sampath Duddu

Reputation: 277

Storing tokens for Push Notifications

our project currently has a Node jS backend and we want to implement push notifications for 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. 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?

Thanks in advance!

Upvotes: 4

Views: 6042

Answers (2)

Bishow Gurung
Bishow Gurung

Reputation: 2010

You must send the notification accessToken to the server, Its like address for notification to be delivered. 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.

 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

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.

New Reference Apple's Documentation

Register your app with APNs and receive a globally unique device token, which is effectively the address of your app on the current device. Your provider server must have this token before it can deliver notifications to the device. You register your app and receive your device token each time your app launches using Apple-provided APIs. The registration process is similar across platforms

Upvotes: 4

Steven Kakooza
Steven Kakooza

Reputation: 15

I have been looking for an optimal solution for this but seems there's no clear way of handling it since we are not aware when the token will change. Does this this mean we should store the token in our database and fetch it every time a user opens the app and then try to compare them ? That is not efficient enough I guess.

In my opinion, keep the token in db for the server then keep a keep a copy in local storage so you can compare that with one that is generated every time the app is opened, unless the two are not the same, then you can update the one in the db.

Upvotes: 1

Related Questions