Mishen Thakshana
Mishen Thakshana

Reputation: 1554

Push notifications not working on app killed state Notifee

I'm using FCM for notification purpose.

This is my index.js,

import {AppRegistry} from 'react-native';
import messaging from '@react-native-firebase/messaging';
import notifee, {AndroidImportance, EventType} from '@notifee/react-native';

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

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

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

notifee.onBackgroundEvent(async ({type, detail}) => {
  const {notification, pressAction} = detail;

  // Check if the user pressed the "Mark as read" action
  console.log(pressAction);

  if (type === EventType.PRESS) {
    // Update external API

    eventEmitter.emit('notificationReceived', notification);

    // Remove the notification
    await notifee.cancelNotification(notification.id);
  }
});

notifee.onForegroundEvent(async ({type, detail}) => {
  const {notification, pressAction} = detail;
  console.log(pressAction);

  if (type === EventType.PRESS) {
    eventEmitter.emit('notificationReceived', notification);

    await notifee.cancelNotification(notification.id);
  }
});

AppRegistry.registerComponent(appName, () => App);

This way it works in background state and foreground state. But when the app is in kill/quite mode doesn't receive notifications. Am I doing something wrong here?

Upvotes: 1

Views: 959

Answers (1)

Qasim Rizvi
Qasim Rizvi

Reputation: 131

For those who're still getting this issue on ios

You should register a category in order to work on ios

async function registerNotificationCategories() {
  console.log('HITTING');
  await notifee.setNotificationCategories([
    {
      id: 'MY_ACTION',
      actions: [
        {id: 'done', title: 'Mark as Done'},
        {id: 'pass', title: 'Mark as Pass'},
      ],
    },
  ]);
}

registerNotificationCategories();

Upvotes: 0

Related Questions