Sending POST requests manually using Postman and/or cURL works great and successfully triggers expo’s Notifications.addListener, but fails to trigger this listener when I programmatically send POST requests to https://exp.host/--/api/v2/push/send
My code is below, but pls note a few things first:
- i have already confirmed that my programmatic POST request (syntax below) works correctly with other non-Expo api endpoints, and you can even verify the network receipt/response at this POST request debugging service: http://ptsv2.com/t/x620r-1523655299
- the reason I’m using this POST debugger service, is because the POST request function below, is a serverless function that only supports synchronous logging ( I cannot console.log things like resolved promises, async callbacks, etc, hence the need for the POST debugging service mentioned above )
- i have intentionally omitted .then and .catch from my fetch call since there’s no logging I can do with them
- the version of fetch used, is isomorphic-fetch@2.2.0
- pls forgive the non-es6 usage
require('isomorphic-fetch')
module.exports = function (event) {
var uri = 'https://exp.host/--/api/v2/push/send'
// var uri = 'http://ptsv2.com/t/x620r-1523655299/post'
var res = event.data.Chat.node
var msg = res.messages[0]
var txt = msg.text
var senderId = msg.writerx.id
var senderName = `${msg.writerx.fbkFirstName || ''} ${msg.writerx.fbkLastName || ''}`
var tokens = []
function reshape(arr) {
arr.forEach(function(unit){
if (unit.userx.id !== senderId) tokens.push(unit.userx.pushToken)
})
}
reshape(res.distributorsx)
reshape(res.shoppersx)
function sendNotifications(data) {
return fetch(uri, {
body: JSON.stringify(data),
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
'Accept-Encoding': 'gzip, deflate'
}
})
}
var newTokens = []
if (tokens.length <= 100) {
tokens.forEach(function(token){
var notification = {
to: token,
body: `${senderName}: ${txt}`,
title: 'LC',
ttl: 3600000,
priority: 'high',
badge: 1
}
newTokens.push(notification)
})
sendNotifications(newTokens)
} else {
// handle chunking when the number of push notifications exceeds 100
}
}
This may be related to why Expo’s Push Notification Online Tester Tool does not seem to be working for me either. Please see my other expo forum post for more clarification on that issue: