how to list devices from a cloud function

7 posts, 1 answers
  1. Michael
    Michael avatar
    15 posts
    Member since:
    Jun 2015

    Posted 05 Nov 2015 Link to this post

     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

     

     

     

     

     

  2. Lyubomir Dokov
    Admin
    Lyubomir Dokov avatar
    43 posts

    Posted 09 Nov 2015 Link to this post

    Hi Michael,

    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
     
    Everlive is now Telerik Backend Services, and is part of the Telerik Platform.
     
  3. Michael
    Michael avatar
    15 posts
    Member since:
    Jun 2015

    Posted 12 Nov 2015 in reply to Lyubomir Dokov Link to this post

    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

  4. Lyubomir Dokov
    Admin
    Lyubomir Dokov avatar
    43 posts

    Posted 12 Nov 2015 Link to this post

    Hi Michael,

    You are right, this is missing from our documentation. Thank you for pointing it out, we will fix it.


    Regards,
    Lyubomir Dokov
    Telerik
     
    Everlive is now Telerik Backend Services, and is part of the Telerik Platform.
     
  5. Michael
    Michael avatar
    15 posts
    Member since:
    Jun 2015

    Posted 12 Nov 2015 in reply to Lyubomir Dokov Link to this post

    Cool. Do i get Telerik points for this? ;-)
  6. Answer
    Anton Dobrev
    Admin
    Anton Dobrev avatar
    539 posts

    Posted 16 Nov 2015 Link to this post

    Hi Mike,

    Thanks for reporting this, I updated your Telerik Points.

    Regards,
    Anton Dobrev
    Telerik
     
    Everlive is now Telerik Backend Services, and is part of the Telerik Platform.
     
  7. Michael
    Michael avatar
    15 posts
    Member since:
    Jun 2015

    Posted 16 Nov 2015 in reply to Anton Dobrev Link to this post

    thanks a lot
Back to Top