Firebase cloud function send notification to iPhone

Viewed 161

I'm trying to trigger push notification to iOS via the code below. I have already implemented this and it works fine when I trigger them via the Firebase console. But I am unable to trigger them via cloud functions.

When I run this function I get {"error":{"message":"Bad Request","status":"INVALID_ARGUMENT"}}

Am I missing something here ?

exports.testMessage = functions.https.onCall(() => { 

  console.log('test message started');

  const registrationToken = 'fPxlvSiOYEX6gyGhSb5zZp:APA91bH3Q10Yjmo9Oa0Glx78_EZJQ3LO0B1PPWhfOZkTthOI7aP9uUsvzAq3EmYkfaswWIjsTR57NEKkr8BF2q4UbWH4-C5AqGv_XGvJp5C2EPbQ3cjL4zo0eGcSr3IP0PmSpYLjh1Sm';
  
  var message = {
      notification: {
          title: 'Test title',
          body:'Test body'
      },
      token: registrationToken // Token is inserted here. 
  };
  
  return admin.messaging().send(message)
      .then((response) => {
          // Response is a message ID string.
          console.log('Successfully sent message:', response);
          return { 'message': 'sent' } 
      }).catch((error) => {
          console.log('Error sending message:', error);
          return { 'message': 'failed' }     
      });
  })
1 Answers

According to the official documentation you have to pass the device's registration token as shown

Send messages to specific devices

// This registration token comes from the client FCM SDKs.
var registrationToken = 'YOUR_REGISTRATION_TOKEN';

var message = {
  data: {
    score: '850',
    time: '2:45'
  },
  token: registrationToken
};

// Send a message to the device corresponding to the provided
// registration token.
admin.messaging().send(message)
  .then((response) => {
    // Response is a message ID string.
    console.log('Successfully sent message:', response);
  })
  .catch((error) => {
    console.log('Error sending message:', error);
  });


Related