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

[iOS] notification not received although it's successfully sent ("success":1) #8

Closed antoinerousseau closed 7 years ago

antoinerousseau commented 7 years ago

I have installed my new app on my iPhone 5s using Xcode, and the FCM registration token is successfully given to me when I start the app. When I use this token in the Firebase console (console.firebase.google.com), it works! But when I send it with fcm-node, it doesn't :/ Note that my config is working for Android. This is my test code:

const to = '<FCM registration token of my device goes here>'

const notification = {
  title: 'test 4',
  body: 'test 4',
}

const payload = {
  to,
  notification,
  priority: 'high',
  content_available: true, // tried without too
}

console.log('Sending to', to)

fcm.send(payload, (error, response) => {
  if (error) {
    console.error('Notification error:', error)
  } else {
    console.log('Notification sent:', response)
  }
})

And I get:

Sending to <token>
Notification sent: {"multicast_id":8677751413683945576,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1475855513649189%f587d512f587d512"}]}

But nothing arrives on the phone! (I have tried with the app in the background as well as killed) Again, I do receive the ones sent through the Firebase console, so my setup is correct imo...

antoinerousseau commented 7 years ago

My bad, I had a typo in the code so the priority wasn't set (in my case it must be set...)

abmnukmr commented 6 years ago

@antoinerousseau Hi i didnt get notification on my ios device. please help me out

const FCM = require('fcm-node');

const apiKey = "MY API KEY" /// it  replaced with my own values
const fcm = new FCM(apiKey);

var message = {
    to:" device Token",  /// it  replaced with my own value

   priority: 'high',

    data: {
        title: "msguser_sender",
        body: "msguser_sender",
        text: " msgmessage",
              }
};

fcm.send(message, function (err,response) {
    //console.log(tokenn + "fetchig right" + message +msg.message)
    if (err) {
        console.log(err);
       console.log("Something has gone wrong!");
    } else {

        console.log("Successfully sent with response: ", response);
    }
});

res.send('respond with a resource'); });

antoinerousseau commented 6 years ago

what do you get in the log? (response or error)

abmnukmr commented 6 years ago

@antoinerousseau Successfully sent with response: {"multicast_id":8837314142145939068,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1510925424333357%4d35cc53f9fd7ecd"}]} but not received any notification . :(

antoinerousseau commented 6 years ago

well first of all it's not data it's notification you must set in the payload.

abmnukmr commented 6 years ago

@antoinerousseau still not getting

abmnukmr commented 6 years ago

@antoinerousseau thanks alot its working 👍

antoinerousseau commented 6 years ago

I'm now using the official firebase-admin package anyway:

const admin = require('firebase-admin')
const account = require('./serviceAccountCredentials.json')
const app = admin.initializeApp({
  credential: admin.credential.cert(account),
})
const messaging = app.messaging()

messaging.sendToDevice(token, payload, options)
.then(response => console.log(response))
.catch(error => console.error(error))

See the doc: https://firebase.google.com/docs/cloud-messaging/admin/send-messages

enlivenaman commented 6 years ago

@antoinerousseau not getting notification in ios but in android it's working fine

const FCM = require('fcm-node');
// Replace these with your own values.
const apiKey = <API Key>';
const deviceID = '<Device id>';
const fcm = new FCM(apiKey);

const message = {
  to: deviceID,
  notification : {
    title: 'Sound Test',
    message: 'Loaded res/raw',
priority: 'high',
    soundname: 'default'
  }
};

fcm.send(message, (err, response) => {
  if (err) {
    console.log(err);
    console.log('Something has gone wrong!');
  } else {
    console.log('Successfully sent with response: ', response);
  }
});

Successfully sent with response: {"multicast_id":5920736412909434312,"success": 1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1528721367502716%4cb d4c214cbd4c21"}]}

antoinerousseau commented 6 years ago

@enlivenaman use firebase-admin as explained in my previous comment.

enlivenaman commented 6 years ago

thanks @antoinerousseau it's working :+1:

ghost commented 6 years ago

hello i got same issues

ghost commented 6 years ago

Check my code Here: define( 'API_ACCESS_KEY', '------' ); // API access key from Google API's Console //define( 'API_ACCESS_KEY', 'YOUR-API-ACCESS-KEY-GOES-HERE' ); $registrationIds = array('e8-l_jCfL6M:APA91bHiE1nVEYDSAdLnIPI-Z5-Wo4_Z6nKkG6YEX5gpW1P79vrClvkX5YZ3w6cw9WbQ5WzoO0qDCxZHWzr0SVnIL_IE0aeD1XVGA7dj2JHnyYhpGMaB-Rv1Db5sK3g3C-UDEFAvlHq4Sb3Vi7tz53nBAnZuQ9bX0w','fnSbi298T44:APA91bH_Sy-gX1yzxofSVwLqjPxiCMdIQ2TGoUuc0BjxZaJIzRXO-RamaGSd6V-qOOY1nKNK5JtYlcLnw-vry8KCuqpxCbrDLHLtb2skSa_G-6IPnazk_whNOsxl_QEv8L83cjuL2qwdclscvdO25Oanuv8DNxIbzw');

            //$registrationIds = array('');
            // prep the bundle
            $msg = array
            (
                'message'   => 'here is a message. message',
                'title'     => 'This is a title. title',
                'subtitle'  => 'This is a subtitle. subtitle',
                'tickerText'    => 'Ticker text here...Ticker text here...Ticker text here',
                'vibrate'   => 1,
                'sound'     => 1,
                'largeIcon' => 'large_icon',
                'smallIcon' => 'small_icon'
            );
            $fields = array
            (
                'registration_ids'  => $registrationIds,
                'data'          => $msg,
                'priority'=>'high',
            );

            $headers = array
            (
                'Authorization: key=' . API_ACCESS_KEY,
                'Content-Type: application/json'
            );

            $ch = curl_init();
            curl_setopt( $ch,CURLOPT_URL, 'https://android.googleapis.com/gcm/send' );
            curl_setopt( $ch,CURLOPT_POST, true );
            curl_setopt( $ch,CURLOPT_HTTPHEADER, $headers );
            curl_setopt( $ch,CURLOPT_RETURNTRANSFER, true );
            curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, false );
            curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode( $fields ) );
            $result = curl_exec($ch );
            curl_close( $ch );
            echo $result;