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
Solution
Q1 is solved by moving the setBackgroundHandler from inside the useEffect to outside the hook.
Q2 still pending
Q3 still pending
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.
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
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.