smartnav / fcm-notification

Push notificaton for ios and android
25 stars 13 forks source link

Error sending message #7

Closed manzoor-mt closed 5 years ago

manzoor-mt commented 5 years ago

I am trying to send a simple FCM notification to Android devices using sendToMultipleToken(). It always returns response like: [{ "response": "Error sending message:", "token": "dc9x65gKLLo:APA91bFnku__06dRI7CHjaMLy1A9a4ixzu.........." }]

smartnav commented 5 years ago

I am trying to send a simple FCM notification to Android devices using sendToMultipleToken(). It always returns response like: [{ "response": "Error sending message:", "token": "dc9x65gKLLo:APA91bFnku__06dRI7CHjaMLy1A9a4ixzu.........." }]

It's working fine, I checked, Can you please share your method to send?

tjodalv commented 5 years ago

I'am having exactly the same issue. When using method sendToMultipleToken() I got error message saying: "Error sending message:". I've noticed that Android tokens works, but for iOS token it wont send notification.

manzoor-mt commented 5 years ago

I installed the library with npm command and downloaded privatekey.json file using your link.

After that I created a function to send FCM i.e.

let FcmNotification = require('fcm-notification');

module.exports = class MyFCM {

constructor() {
   this.fcm = new FcmNotification('privatekey.json');
}

sendFcm(message, fcmTokens) {
        this.fcm.sendToMultipleToken(message, fcmTokens, (error, response) => {
            if (error) {
                console.log('Error # FCM failed => ', error);
            }
            else {
                console.log('FCM response = ', response);
            }
        });     
}

}

I called it like

// ....required MyFCM class here
let myFcm = new MyFCM();
myFcm.sendFcm({notification: {title: "test title", body: "this is a test"}}, ["eDzRC65Bu0E:APA91bEyblFMzpukUeBac2SgypdEwymlV63OtMwgzOR6DNCihIm1bEIFJpfZJlC6d1fPLPugeLZU0gWTR8Juijt0lVy3vNi4WbrwGa058EDaEMz1rvaQeIPKul0bJBsnlPPzQkBXPAuL"]);
tjodalv commented 5 years ago

These are the notification data I am sending to sendToMultipleToken()

const collapseKey = `chat.${msg.room_id}.message`;

const options = {
    data: {
        chat_room_id: `${msg.room_id}`
    },
    android: {
        ttl: 24 * 60 * 60 * 1000,
        priority: 'high',
        collapseKey: collapseKey,
        notification: {
            title: msg.sender.name,
            body: msg.content,
            tag: collapseKey,
            color: '#dd949b',
            icon: 'notification_icon'
        }
    },
    apns: {
        headers: {
            'apns-priority': '10',
            'apns-collapse-id': collapseKey
        },
        payload: {
            aps: {
                alert: {
                    title: msg.sender.name,
                    body: msg.content
                },
                'thread-id': collapseKey
            }
        }
    },
};

FCM.sendToMultipleToken(options, arrTokens, (err, response) => {
    if (err) {
        console.log("push error found", err);
    } else {
        console.log("push response here", response);
    }
});

I've tested on Android device and notifications are working, but on iOS device wont send notifications.

smartnav commented 5 years ago

I installed the library with npm command and downloaded privatekey.json file using your link.

After that I created a function to send FCM i.e.

let FcmNotification = require('fcm-notification');

module.exports = class MyFCM {

constructor() {
   this.fcm = new FcmNotification('privatekey.json');
}

sendFcm(message, fcmTokens) {
        this.fcm.sendToMultipleToken(message, fcmTokens, (error, response) => {
            if (error) {
                console.log('Error # FCM failed => ', error);
            }
            else {
                console.log('FCM response = ', response);
            }
        });     
}

}

I called it like

// ....required MyFCM class here
let myFcm = new MyFCM();
myFcm.sendFcm({notification: {title: "test title", body: "this is a test"}}, ["eDzRC65Bu0E:APA91bEyblFMzpukUeBac2SgypdEwymlV63OtMwgzOR6DNCihIm1bEIFJpfZJlC6d1fPLPugeLZU0gWTR8Juijt0lVy3vNi4WbrwGa058EDaEMz1rvaQeIPKul0bJBsnlPPzQkBXPAuL"]);

Hi manzoor-mt , I have checked with this module with your given method, its working fine. You need to check your project setting under setting->General for android and ios.

I'm getting success response : response: 'Successfully sent message:projects/****' } ]

If any issue, please let me know.

Thanks, Navish

smartnav commented 5 years ago

These are the notification data I am sending to sendToMultipleToken()

const collapseKey = `chat.${msg.room_id}.message`;

const options = {
    data: {
        chat_room_id: `${msg.room_id}`
    },
    android: {
        ttl: 24 * 60 * 60 * 1000,
        priority: 'high',
        collapseKey: collapseKey,
        notification: {
            title: msg.sender.name,
            body: msg.content,
            tag: collapseKey,
            color: '#dd949b',
            icon: 'notification_icon'
        }
    },
    apns: {
        headers: {
            'apns-priority': '10',
            'apns-collapse-id': collapseKey
        },
        payload: {
            aps: {
                alert: {
                    title: msg.sender.name,
                    body: msg.content
                },
                'thread-id': collapseKey
            }
        }
    },
};

FCM.sendToMultipleToken(options, arrTokens, (err, response) => {
    if (err) {
        console.log("push error found", err);
    } else {
        console.log("push response here", response);
    }
});

I've tested on Android device and notifications are working, but on iOS device wont send notifications.

Hi tjodalv,

Please check your code from my end, it is working on ios from my end. var fcm = require('fcm-notification'); var FCM = new fcm('path.json'); const collapseKey = 'testcollapsKey';

const options = { data: { chat_room_id: 'roomId' }, android: { ttl: 24 60 60 * 1000, priority: 'high', collapseKey: collapseKey, notification: { title: 'Test title tjodalv', body: 'content for tjodalv', tag: collapseKey, color: '#dd949b', icon: 'notification_icon' } }, apns: { headers: { 'apns-priority': '10', 'apns-collapse-id': collapseKey }, payload: { aps: { alert: { title: 'Test title tjodalv', body: 'content for tjodalv' }, 'thread-id': collapseKey } } }, };

FCM.sendToMultipleToken(options, ['cOPEyz1QMPk:APA91bEe1uDq-qXczg-p0l9jvbCNlg1fLscpZQZnzYUoDmvKZlI-f630k**'],` (err, response) => { if (err) { console.log("push error found", err); } else { console.log("push response here", response); } });

Please check attached photo for reference.

Mail   Navish kumar   Outlook(3)

If any issue then let me know.

Thanks, Navish

smartnav commented 5 years ago

tjodalv

danilrafiqi commented 4 years ago

i get same error, the problem is : this package only can send 2 payload, that is title and body