I
I
Ilya2019-04-10 22:10:22
React Native
Ilya, 2019-04-10 22:10:22

How to properly create a function to receive notifications?

The question probably has no solution in principle with this approach, but I'll try.
I have an array with signals:

[
  {
"idSite": 123456,
"type": "1",
"description": "SIGNAL 1",
"dateTime": "2019-04-20 00:00:00"
},
  {
"idSite": 123456,
"type": "2",
"description": "SIGNAL 2",
"dateTime": "2019-04-20 00:00:00"
},
  {
"idSite": 123455,
"type": "4",
"description": "SIGNAL 3",
"dateTime": "2019-04-20 00:00:00"
}
]

There is a function that checks for signals. And sends notifications if any.
if (dataObject !== null) {
        let token = await Notifications.getExpoPushTokenAsync();
        console.log(token)
        let response = await fetch('https://exp.host/--/api/v2/push/send', {
            method: 'POST',
            headers: {
                Accept: 'application/json',
                'Content-Type': 'application/json'
            },
            body: JSON.stringify({
                to: token,
                title: dataObject[0].description,
                sound: 'default',
                body: dataObject[0].dateTime
            })
        })

    }

As you can see from the function, my brains figured out how to send only the first object to the notification.
Well and a question:
How to send all objects. How single notifications?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Panteleev, 2019-04-11
@ynizhenie

I'm not sure, because "How to correctly create a function for receiving notifications", but I'll try) The Push Notifications
documentation has

example of batch sending notifications (up to 100 pcs at a time)
// пакетная
[{
  "to": "ExponentPushToken[xxxxxxxxxxxxxxxxxxxxxx]",
  "sound": "default",
  "body": "Hello world!"
}, {
  "to": "ExponentPushToken[yyyyyyyyyyyyyyyyyyyyyy]",
  "badge": 1,
  "body": "You've got mail"
}]

// обычная
{
  "to": "ExponentPushToken[xxxxxxxxxxxxxxxxxxxxxx]",
  "title":"hello",
  "body": "world"
}

accordingly, pass an array of objects to the function, there you bring them to the desired form and send them :)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question