Support Topics Documentation Slack YouTube Blog

Saving deviceID for Push to Individual

In iOS, I need to associate BackendlessUsers with their device that gets saved to the Messages console. This is because I need to send push notifications to individual users.
However, I am unsure how to obtain and save the “Device id” that gets saved to the Messages console:
http://support.backendless.com/public/attachments/d81cc215e03182a18a2814388a9f5faa.png</img>
If I am able to save this device id value, I can map it to a class named UserDevice that stores the deviceID and also points to its corresponding user. I know you can also just save the deviceId as a custom property on the BackendlessUser. However, I just don’t know when you can obtain this value.
After reading the docs, I thought you get it the following way:

 func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
 let deviceTokenStr = backendless.messagingService.deviceTokenAsString(deviceToken)
 backendless.messagingService.registerDeviceToken(deviceTokenStr, response: { (deviceId) -> Void in
//save deviceId here ...
}


However, the deviceID returned to me within the closure is different from the deviceID that is saved to the Messages console.
So what method gives me the correct deviceID that gets saved to the Messages console and be used for sending pushes to that specific device?







Hi Vikzilla,

Your client app could use the following API to retrieve device registration information:
https://backendless.com/documentation/messaging/ios/messaging_retrieve_device_registration.htm

The object returned by the API call contains deviceId, which you could store as a user property or in the UserDevice table as you described.

Regards,
Mark

Is it reliable to get the device id the following way:

backendless.messagingService.currentDevice().deviceId

It seems to be returning a different string than it was a few days ago for the same device.

Have you reinstalled the app between a few days ago and now? The ID would change if the app is uninstalled and then re-installed again.

Ah yes I have. That would explain it. Thanks!

There are android examples show a way to assign you own device id. is there no way to do it is iOS(swift). Here is what works in android

    Messaging.DEVICE_ID = strDeviceID;       // This unique id this device to the backend server using userInfo
    Backendless.Messaging.registerDevice(getResources().getString(R.string.gcm_project_num), new AsyncCallback&lt;Void&gt;() {
        @Override
        public void handleResponse(Void response) {
            Log.i("App","Device ID has been registered for Push Notifications");
        }


        @Override
        public void handleFault(BackendlessFault fault) {
            Log.e("App","Device IDFALLED to registered for Push Notifications " + fault.getMessage());
        }
    });

Android getDeviceId

        Backendless.Messaging.getDeviceRegistration( new AsyncCallback<DeviceRegistration>() {            

        @Override
        public void handleResponse(DeviceRegistration response) {
            Log.i("LoginResult", "response: " + response.getDeviceId());
        }

        @Override
        public void handleFault(BackendlessFault fault) {
            Log.i("LoginResult", "fault: " + fault);


        }
    });

        Backendless.Messaging.getDeviceRegistration( new AsyncCallback<DeviceRegistration>() {
        @Override
        public void handleResponse(DeviceRegistration response) {
            Log.i("LoginResult", "response: " + response.getDeviceId());


        }

        @Override
        public void handleFault(BackendlessFault fault) {
            Log.i("LoginResult", "fault: " + fault);


        }
    });