push notification with firebase cloud functions

I am using Firebase Cloud Functions for push notification when onCreate. Please help.

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();

exports.sendNotification = functions.database.ref('/messages/diyetisyen/{uname}/{msgid}/message')
.onCreate((snapshot, context) => {

let message = snapshot.val();
let uname = context.params.uname;
//let uname_slice = uname.slice(0,(uname.length-4)) + uname.slice((uname.length-3));
let root = snapshot.ref.root;
let tokenRef = root.child('/users/' + uname + '/token').ref;

let payload = {
  data: {
    custom_notification: JSON.stringify({
      body: message + 'a',
      title: 'title'
    })
  }
};

let options = { priority: "high" };

return tokenRef.once('value')
  .then(dataSnapshot => {
    let token = dataSnapshot.val();
    console.log('2 \n'+'message \n' + message +'\n tokenref \n' + tokenRef +'\n token \n' + token +'\n uname \n' + uname+'\n gönderiliyor');
    return admin.messaging().sendToDevice(token, payload, options);
  });
});

I am getting message, token ,uname correctly but
admin.messaging().sendToDevice(token, payload, options); not working.

Upvotes: 0

Views: 543

Answers (1)

I changed payload like

 const payload = {
    notification: {
    title: 'title',
    body: message + ''
    }
};

and it's working.

Upvotes: 1

Related Questions