Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to recieve background notifications in notifee and FCM

I have been trying to implement notifications using FCM through rnfirebase in react native. And to handle the local notifications using notifee.

I have been able to recieve background notifications ie killed state and minimized state through firebase cloud messaging and able to get the foreground notifications using notifee.

Now I want to use notifee for background notifications for consistency betweeen the notifications.

Here the code

const displayNotification = async () => {
    const channelId = await notifee.createChannel({
      id: 'important',
      name: 'Important Notifications',
      importance: AndroidImportance.HIGH,
    });
    notifee.displayNotification({
      body: 'This message was sent via FCM!',
      android: {
        channelId: channelId,
        actions: [
          {
            title: 'Mark as Read',
            pressAction: {
              id: 'read',
            },
          },
        ],
      },
    });
  };

   messaging().setBackgroundMessageHandler(async remoteMessage => {
      console.log('Message handled in the background!', remoteMessage);
      displayNotification();
    });

    messaging().onMessage(async remoteMessage => {
      console.log('Message handled in the foregourp!', remoteMessage);
      displayNotification();
    });

With this code getting foreground notifications. And when the app is minimized getting two notifications one from notifee and other from FCM. And when the app is killed only getting FCM notification not notifee one.

Questions

  1. How to get notification from notifee in killed state ?
  2. How to disable FCM background notification. Do I need to send data only notification from firebase ?
  3. Also on One Plus device not able to get FCM notification in killed state because it is showing that the app is not running. Do I need to add a to the android manifest file ?

Solution

Q1 is solved by moving the setBackgroundHandler from inside the useEffect to outside the hook.

Q2 still pending

Q3 still pending

like image 376
ShIvam Rawat Avatar asked Sep 06 '25 03:09

ShIvam Rawat


2 Answers

The backgroundMessageHandler creates a Notification with the Info from FCM on its own and triggering your own would just cause two notifications. You should remove displayNotification(); in the backgroundMessageHandler. Further more, the backgroundMessageHandler should be located in index.js file and not your App.

like image 160
Maximilian Dietel Avatar answered Sep 07 '25 21:09

Maximilian Dietel


If you want to ignore the FCM background notification, just ignore the notification parameter. Just send the data parameter, but Data-only messages are sent as low priority on both Android and iOS and will not trigger the setBackgroundMessageHandler by default. To enable this functionality, you must set the "priority" to high on Android and enable the content-available flag for iOS in the message payload.

{
  "to": "User_Device_Token",
  "data": {
     "body": "Body of Your Notification",
     "title": "Title of Your Notification"
  },
  "priority": "high",
  "contentAvailable": true
}
like image 32
Vishal Sinha Avatar answered Sep 07 '25 23:09

Vishal Sinha