U rock
U rock

Reputation: 775

How to make async call for sending one signal push notifications

I want to make async call to send push notification to every device. I have array of objects contains userinfo and device id. I have to rotate every object until array finish. I do not understand how to make it. What i have tried to get it through closure function to wait manually for 2 seconds. Please help me to get out of this.

var userinfo = [{
    userdata: "some info for user 1",
    deviceid: "user device id 1"
  },
  {
    userdata: "some info for user 2",
    deviceid: "user device id 2"
  },
  {
    userdata: "some info for user 3",
    deviceid: "user device id 3"
  }
]

for (var i = 0; i < userinfo.length; i++)(function(t) {
  setTimeout(function() {
    var message = {
      app_id: "my app id",
      contents: {
        "en": userinfo[t].userdata
      },
      include_player_ids: [userinfo[t].deviceid],
    };
    sendNotification(message);
  }, t * 2000)
}(i));

Upvotes: 0

Views: 1047

Answers (1)

SRK
SRK

Reputation: 3496

You can use async await functionality.

for (var i = 0; i < userinfo.length; i++)( async function(t) {
    var message = await {
      app_id: "my app id",
      contents: {
        "en": userinfo[t].userdata
      },
      include_player_ids: [userinfo[t].deviceid],
    };
    await sendNotification(message);
}(i));

Here is a Documentation

Upvotes: 1

Related Questions