0

Unfortunately, no answer yet. I am checking out postman, I hope I can use that to test quicker.

I manage to send a notification through my app, however, the notification always ends up in the silent notification of my phone, no sound, no vibration and no notification icon in the top left of the phone, only a notification in the drawer when I swipe down :(

In an attempt to fix / improve the situation I tried the following:

  1. Create an android notification channel with id: high_importance_channel by using flutter_local_notifications package. The channel was created successful, because requesting an overview of the existing channels, showed the newly created channel (among the other channels). The new channel has importance: 5, enableVibration: true and playSound: true, so that should do the job.

  2. Send a FCM through cloud functions with the following code:

    const functions = require("firebase-functions");
    const admin = require("firebase-admin");
    
    admin.initializeApp();
    exports.chatNotification = functions.firestore
        .document('chats/{groupId}/chat/{chatId}')
        .onCreate( async (snapshot, context) => {
        const message = {
           "notification": {
               "title": "Message Received",
               "body": "Text Message from " + fromUserName,
             },
           "tokens": registrationTokens,
           "android": {
                 "notification": {
                     "channel_id": "high_importance_channel",
                   },
               },
         };
    
         admin.messaging().sendMulticast(message)
           .then((response) => {
             console.log(response.successCount + ' messages were sent successfully');
           });
           }
    

But so far not luck, the notification still ends up in the silent notifications. What am I doing wrong?

BJW
  • 925
  • 5
  • 15
  • Does this answer your question? [No notification sound when sending notification from firebase in android](https://stackoverflow.com/questions/37959588/no-notification-sound-when-sending-notification-from-firebase-in-android) – Alex Oct 15 '21 at 19:50

1 Answers1

0

There are 2 ways of doing it. Might work in your case.

Way 1:

var payload = {
      notification: {
        android_channel_id: 'AppChannel',
     /* 
       https://stackoverflow.com/questions/62663537/how-do-i-add-a-channelid-to-my-notification
       https://firebase.google.com/docs/cloud-messaging/http-server-ref#notification-payload-support
     */ 

        title: 'Push Notification Arrived!',
        body: 'Using Cloud Functions',
        sound: 'default',
      },
      data: {
        route: '/someRoute',
      },
    };

    try {
      const response = await admin.messaging().
        sendToDevice(deviceTokens, payload);

      console.log('Notification sent succesfully.');
    } catch (err) {
      console.log('Error sending Notifications...');
      console.log(err);
    }

Way 2:

const message = {
      /*
        https://firebase.google.com/docs/reference/admin/node/firebase-admin.messaging.basemessage.md#basemessagenotification
      */
      notification: {
        title: 'Push Notification Arrived!',
        body: 'Using Cloud Functions',
      },
      data: {
        route: '/someRoute',
      },
      android: {
        /*
          https://firebase.google.com/docs/reference/admin/node/firebase-admin.messaging.androidnotification.md#androidnotificationbodylockey
        */
        notification: {
          channelId: "AppChannel",
          priority: 'max',
          defaultSound: 'true',
        },
      },
      token: deviceTokens,
    };

    // https://firebase.google.com/docs/reference/admin/node/firebase-admin.messaging.messaging.md#messagingsend
    admin.messaging().send(message)
      .then((response) => {
        // Response is a message ID string.
        console.log('Successfully sent message:', response);
      })
      .catch((error) => {
        console.log('Error sending message, for LOVEs sake:', error);
      });
saqib shafin
  • 98
  • 1
  • 8
  • It turned out that 'importance: 5' was not supported by Android. https://stackoverflow.com/a/70835014/9424323 – BJW Aug 02 '22 at 12:36
  • I see...I checked that post of yours before making my answer, thought that it might help you in declaring channel names correctly. – saqib shafin Aug 02 '22 at 20:17