I have react native application that targets to both IOS and android. I integrated firebase notifications in my react native mobile application. I face a problem for android when the app is in background then no callback is hit but notification is received in tray from firebase.
I want to add logic for notifications count when app is in background so that when user opens the app he can see that there are 2 or something like new notifications. also want to update the badge count.
I know how to update badge counter or notifications counter as i did when the app is in background then there is call back onNotification that is fired. but i want to hit some code when new notification is received while application is in background.
The code for notifcations
async componentWillUnmount() {
await this.notificationListener();
await this.notificationOpenedListener();
await this.messageListener();
}
messageListener = async () => {
// When Notification is Recived and app in fore ground
this.notificationListener = firebase
.notifications()
.onNotification(async notification => {
debugger
console.log("on notification");
const { title, body } = notification;
const badgeCount = await firebase.notifications().getBadge();
debugger
firebase.notifications().setBadge(badgeCount+1);
await this.props.clearNotificationCount(badgeCount+1).then(res => {
debugger
BadgeAndroid.setBadge(this.props.notifications.Count)
console.log("BadgeCount is" + badgeCount);
});
});
this.notificationOpenedListener = firebase
.notifications()
.onNotificationOpened(async notificationOpen => {
debugger
console.log("on open listner");
const { title, body } = notificationOpen.notification;
const badgeCount = await firebase.notifications().getBadge();
debugger
this.props.setNotificationCount(badgeCount).then(res => {
// BadgeAndroid.setBadge(badgeCount)
});
firebase.notifications().setBadge(0);
});
const notificationOpen = await firebase
.notifications()
.getInitialNotification();
if (notificationOpen) {
console.log("Notification open");
console.log(notificationOpen.notification);
const { title, body } = notificationOpen.notification;
}
this.messageListener = firebase.messaging().onMessage(message => {
console.log("on message listner");
// firebase.notifications().setBadge(2);
console.log(JSON.stringify(message));
});
};
checkPermission = async () => {
const enabled = await firebase.messaging().hasPermission();
if (enabled) {
await this.getFcmToken();
} else {
await this.requestPermission();
}
};
getFcmToken = async () => {
const fcmToken = await firebase.messaging().getToken();
if (fcmToken) {
let SaveFirebaseTokenEndpoint = ApiEndPoint.SavefireBasetoken;
let myResponse = await DataAccess.PostSecured(SaveFirebaseTokenEndpoint, {
DeviceToken: fcmToken
});
console.log(fcmToken);
console.log(myResponse);
} else {
// this.showAlert('Failed', 'No token received');
}
};
requestPermission = async () => {
try {
await firebase.messaging().requestPermission();
// User has authorised
} catch (error) {
// User has rejected permissions
}
};
onNotificationOpened() never hits in background