From a cloud function, how can i get a list of all devices and send a specific push notifications to each one of them depending on their parameters?
1. i know how to send push notifications filtering by device id
>> http://docs.telerik.com/platform/backend-services/javascript/push-notifications/push-send-cloud-code
i am OK here
2. get a list of device and for each device create a push notification >>> i am not OK
http://docs.telerik.com/platform/backend-services/rest/push-notifications/push-initialization-and-registration#retrieving-a-device-list
>>> this is the only place where i can find information about listing devices (this is the rest api, not the cloud function documentation).
http://docs.telerik.com/platform/backend-services/rest/server-side-logic/cloud-code/cloud-code-make-http-requests
>>> will i have to make a rest call from cloud function (this is strange) ?
thank you
6 Answers, 1 is accepted
In a cloud function, the best way to read devices registered for push notifications is by using the JS SDK. You could make a REST API call, but the SDK has convenient methods to make it easy for you. Note, that you must initialize the SDK with master key, as the devices are only available for reading with master authentication.
Here is how it's done:
Everlive.CloudFunction.onRequest(
function
(request, response, done){
var
el = Everlive.Sdk.withMasterKey(); //Convenient way to get SDK initialized with Master Key
el.push.devices.get().then(
function
success(result) {
response.result = result;
done();
},
function
error(err) {
response.result = err;
done();
}
);
});
Note, that the code above sets the retrieved devices to the result object so that you can easily test your cloud function. Normally you will just use the result to create a push notification.
The el.push.devices object works the same way as if you work with data e.g. is similar to el.data('myType'). You can read more information on working with data from the JS SDK here.
There is just one more thing I would like to note. The mechanism for targeting push notifications is very flexible. You can put a complex filter in the push notification itself. Instead of doing a filter on devices and then sending a notification by specifying the IDs of those devices, you can simply set the filter on the push notification. When we start sending it, we will filter the devices using the filter you specified and send only to devices that match.
I hope this helps, let me know if you have other questions.
Regards,
Lyubomir Dokov
Telerik
Thanks for your answer.
When i went into the documentation, i did this : backend services > api reference > javascript sdk > push
I just saw currentDevices. I didn't find push.devices (screenshot attached).
Am i missing a point or is it missing in the documentation?
Thank you
You are right, this is missing from our documentation. Thank you for pointing it out, we will fix it.
Regards,
Lyubomir Dokov
Telerik
Thanks for reporting this, I updated your Telerik Points.
Regards,
Anton Dobrev
Telerik