FCM Node.js topic notification doesn't reach Android app

148 views
Skip to first unread message

Whai Nooa

unread,
Jun 24, 2019, 6:51:55 PM6/24/19
to Firebase Google Group
I'm using a Node.js server to send topic notifications to Android devices using FCM. I tested the topic notifications from Firebase console and it did work, so the problem seems to be on the server side.

Here is my implementation:

**firebase/fb.js**

    const admin = require("firebase-admin");
    //Firestore Connect
    const DATABASE_NAME = "....";
    var serviceAccount = require('../config/.....json');
    
    module.exports = admin.initializeApp({
        credential: admin.credential.cert(serviceAccount),
        databaseURL: `https://${DATABASE_NAME}.firebaseio.com`
    });

**notifications.js**

    const router = express.Router();
    const fb = require('../firebase/fb');
    
    const db = fb.firestore();
    const fcm = fb.messaging();

    router.post('/send/topic/:topic', (req, res) => {
    
        var topic = `/topics/${req.params.topic.toString()}`;
    
        var currentTime = new Date().getTime();
    
        var payload = {
            notification: {
                title: req.body.title,
                body: req.body.body
            }
        };
    
        var options = {
            priority: "high",
            timeToLive: 60 * 60 * 24
        };
        
        fcm.sendToTopic(topic, payload, options)
            .then((response) => {
                
                res.send(200, req.body);
                // Response is a message ID string.
                console.log('Successfully sent message:', response);
            })
            .catch((error) => {
                
                res.send(400, error);
                console.log('Error sending message:', error);
            });
    });

When I execute this code I get the following response:

    Successfully sent message: { messageId: 7218165350026662000 }

The notification is not displayed on the Android phone. The device is registered to the specific topic.

I have also tried to use the `.send(message)` method but the result was the same. Moreover, I'm using quite the same algorithm to send to a specific device and that works.

What's wrong with my code?

Kato Richardson

unread,
Jun 26, 2019, 2:36:35 AM6/26/19
to Firebase Google Group
Hi Whai,

Nothing springs to mind looking at that code. As a general rule, if the FCM service send a 200 response, then it was sent to the device and left Google's servers successfully. What happens from there is a tricky business and there are a host of reasons a specific device may fail--mostly related to device state and carrier issues.


☼, Kato

--
You received this message because you are subscribed to the Google Groups "Firebase Google Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to firebase-tal...@googlegroups.com.
To post to this group, send email to fireba...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/firebase-talk/99703451-4177-407a-8651-8e0c8ca8a74f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


--

Kato Richardson | Developer Programs Eng | kato...@google.com | 775-235-8398

Reply all
Reply to author
Forward
0 new messages