0
votes

Upon sending a successful notification via Cloud Functions for Firebase, the notification is not shown as a push notification on ios device. I have found a couple of similar issues, but none present a clear solution.

cloud function:

exports.sendInitialNotification = functions.database.ref('branches/{branchId}/notifications/{notifId}').onWrite(event => {
  const data = event.data.val()
  if (data.finished) return
  const tokens = []
  const notifId = event.params.notifId
  const getPayload = admin.database().ref(`notifications/${notifId}`).once('value').then(snapshot => {
    const notif = snapshot.val()
    const payload = {
      notification: {
        title: notif.title,
        body: notif.message,
      },
      data: {
        'title': notif.title,
        'message': notif.message,
        'id': String(notif.id),
      }
    }
    if (notif.actions) {
      payload.data['actions'] = JSON.stringify(notif.actions)
    }
    console.log('payload:', payload)
    return payload
  }, (error) => {
    console.log('error at sendInitialNotification getPayload():', error)
  })
  const getTokens = admin.database().ref(`notifications/${notifId}/users`).once('value').then(snapshot => {
    const users = snapshot.forEach((data) => {
      let promise = admin.database().ref(`users/${data.key}/profile/deviceToken`).once('value').then(snap => {
        if (tokens.indexOf(snap.val()) !== -1 || !snap.val()) return
        return snap.val()
      }, (error) => {
        console.log('error retrieving tokens:', error)
      })
      tokens.push(promise)
    })

    return Promise.all(tokens)
  }, (error) => {
      console.log('error at sendInitialNotification getTokens()', error)
    }).then((values) => {
      console.log('tokens:', values)
      return values
  })

  return Promise.all([getTokens, getPayload]).then(results => {
    const tokens = results[0]
    const payload = results[1]
    if (payload.actions) {
      payload.actions = JSON.stringify(payload.actions)
    }
    const options = {
      priority: "high",
    }
    admin.messaging().sendToDevice(tokens, payload, options)
      .then(response => {
        data.finished = true
        admin.database().ref(`notifications/${notifId}`).update({success: true, successCount: response.successCount})
        console.log('successfully sent message', response)
      }).catch(error => {
        data.finished = true
        admin.database().ref(`notifications/${notifId}`).update({success: false, error: error})
        console.log('error sending message:', error)
      })
  })
})

...and the logs in firebase console:

successfully sent message { results: [ { error: [Object] } ],
canonicalRegistrationTokenCount: 0, failureCount: 1, successCount: 0, multicastId: 7961281827678412000 }

tokens: [ 'eS_Gv0FrMC4:APA91bEBk7P1lz...' ]

payload: { notification: { title: 'test07', body: 'test07' }, data: { title: 'test07', message: 'test07', id: '1502383526361' } }

...but alas no notification shown on iphone. Im sure I am missing something along the OOO (order of operations) here, but not sure where the snag is. If anyone can point out my flaw please feel free to publicly chastise.

As always thank you in advance and any and all direction is appreciated!

1
Thanks for the housekeeping... On further investigation this appears to be associated with the ever-growing-in-popularity: "Invalid APNs certificate. Check the certificate in Settings". I have created and uploaded a new Key via: APNs Authentication Key, as directed in the docs, but still seeing the error on every sent notification... At a loss!studiobrain
Hi studiobrain. Have you tried sending a simole test notification via Postman and see if it works from there? It might give you a better error message.AL.
I have. The issue I was seeing was due to the reverse domain id in xcode not matching what was in firebase. It is working now.studiobrain
Cool. I would suggest adding in an answer as well with as much details as you can. It might help someone with a similar issue one day. Cheers! :)AL.
Most of the time, this problem seems to be caused by improper configuration, such a typo in the bundle name, bad entitlements in App ID, etc. However, I discovered that one solution if all else fails is to use your Team ID instead of the App Prefix in the Firebase Cloud Settings. Not the answer here, but that might help someone else.JohnnyC

1 Answers

0
votes

The issue I was seeing was due to the reverse domain id in xcode not matching what was in firebase.

Typo