Push Notification Endpoint Issue with My Post Requests

#1

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:

Issue with Expo's Online Push Notification Tester
Issue with Expo's Online Push Notification Tester
#2

Your code looks OK but without debugging further it’s hard to say what’s going on. The biggest thing that catches my eye is that the server response and network errors from fetch are ignored. The POST can succeed at the HTTP layer but the API call can still at the API layer.

You might also want to take a look at: https://github.com/expo/exponent-server-sdk-node

1 Like
#3

thank you @ide … the serverless function is a Graph.cool function… I’ve reached out to them already to try and diagnose further (i just need better access to their serverless function logs… without logs, i’m troubleshooting blind and in the dark)… if things don’t work out on their end, then it’s comforting to know that i still have the backup solution you are suggesting (exponent-server-sdk-node)… again, many many thanks

#4

resolved… Graph.cool helped me with a workaround to start returning the logs, and it turns out the issue was that my TTL property exceeded the maximum TTL allowed… the power of logs haha, without them, i’m blind testing in the dark. again, thanks so much for the patience and for helping me narrow things down!

closed #5

This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.