jlcvp / fcm-node

A Node.JS simple interface to Google's Firebase Cloud Messaging (FCM) for Android & iOS & Web Notification and data push
MIT License
125 stars 46 forks source link

Working iOS-Example? #7

Closed Suven closed 7 years ago

Suven commented 7 years ago

Hey,

I have the following behaviour: iOS-Users receive messages send through the firebase-console BUT not send via the script. Android-Users receive both.

I tried it with single-device-targeting, topic-targeting and with/without priorities.

Is there anything else to be aware of? Are pushes send from the HTTP-API using the dev-cert instead of the prod-cert?

I really would appreciate a working example.. Here is my test-script:

var process = require('process');
var winston = require('winston');

process.title = 'fanradio.streamstatus';

var firebase = require('firebase');

winston.info('Logging in...');

firebase.initializeApp({
  serviceAccount: './service.json',
  databaseURL: 'MYURL',
});

winston.info('Authenticated via Firebase.');

var FCM = require('fcm-node');
var fcm = new FCM('MYKEY');

var topic = 'MYSINGLEDEVICETARGET';
// topic = '/topics/test';
var push = { text: 'foo' };

fcm.send({
  to: topic,
  notification: { priority: 'high', body: push.text },
}, function (error, response) {
  if (error) {
    winston.warn('A pushmessage could not be send', { error });
    return;
  }

  winston.info('Push was send', response);
});
GitVitor commented 7 years ago

Do you try insert a prop title in your Notification object?

notification: {
    title: 'YOUR TITLE', 
    body: 'YOUR MESSAGE' 
},
Suven commented 7 years ago

Yup, also tried that :/ The strange thing is that the response would always be successfull, even if I directly send it to one device. It's like firebase is ignoring ios.

I just tested it via Postman and Curl and have the same problem.

Feel free to close this then, as it does not seem to be related to that module :/

Edit:/ I found the issue. The priority-flag does NOT belong into the notification-object, but into the root-object. 😅 Thanks for your help.

So just for anybody else who might find this through google, here is the working example:

var FCM = require('fcm-node');
var fcm = new FCM('YOURKEY');

var topic = 'YOURDEVICEKEY';
// topic = '/topics/test';
var push = { text: 'foo' };

fcm.send({
  to: topic,
  priority: 'high',
  notification: { body: push.text },
}, function (error, response) {
  if (error) {
    winston.warn('A pushmessage could not be send', { error });
    return;
  }

  winston.info('Push was send', response);
});
ghost commented 4 years ago

I am having the same issue. I also want to send the payload along with title and body, how can I achieve that?