【发布时间】:2020-01-16 10:27:07
【问题描述】:
我已经对面向 IOS 和 android 的本机应用程序做出反应。我在我的 react 原生移动应用程序中集成了 firebase 通知。当应用程序在后台时,我遇到了 android 的问题,然后没有回调被击中,但从 firebase 的托盘中收到通知。
我想在应用程序处于后台时添加通知计数逻辑,以便当用户打开应用程序时,他可以看到有 2 个或类似的新通知。还想更新徽章计数。
我知道如何更新徽章计数器或通知计数器,就像我在应用程序处于后台时所做的那样,然后会触发 onNotification 回调。但是当应用程序在后台时收到新通知时,我想点击一些代码。
通知代码
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() 永远不会在后台点击
【问题讨论】:
标签: android firebase react-native push-notification