嗨,我正在使用 react native firebase对于通知,我成功地集成了它,并且两个平台都收到了通知,但是当应用程序处于前台或后台时,android 提示不会出现。我阅读了与此相关的所有问题,但没有任何线索。
app environment:
- “ react ”:“16.3.1”,
- "react-native": "0.55.3",
- "react-native-firebase": "4.2.0",
- firebase cloud messaging tab in console用于发送消息——也尝试使用高级选项
所以当应用程序在前台时,应用程序需要处理通知,这就是我正在做的:
componentDidMount() {
this.checkFirebase();
}
registerFbCloudMessagingListener = () => {
firebase.notifications().onNotification(notification => {
if (Platform.OS === "android") {
notification.android.setChannelId("forgroundnotification");
}
firebase.notifications().displayNotification(notification);
});
};
async checkFirebase() {
const enabled = await firebase.messaging().hasPermission();
if (enabled) {
// user has permissions
this.registerFbCloudMessagingListener();
} else {
// user doesn't have permission
this.requestFbPermission();
}
}
async requestFbPermission() {
try {
let permission = await firebase.messaging().requestPermission();
if (permission) {
this.checkFirebase();
}
// User has authorised
} catch (error) {
// User has rejected permissions
}
}
一开始我使用的是 mi device 因为它只在应用程序托盘中显示通知然后我检查了 settings > my_app > notifications > show floating notification
turned在
然后抬头开始进入该设备,但后来我尝试使用 one plus 设备,因为它没有显示。
我检查了所有这些问题
- https://github.com/invertase/react-native-firebase/issues/500
- https://github.com/invertase/react-native-firebase/issues/357
- https://github.com/invertase/react-native-firebase/issues/595
In oreo its not showing i think. because mi is having android N. Please help !!! Advance thanks.
最佳答案
下面是我是如何破解它的。
首先,来自 firebase 控制台的推送通知不会在 android 上显示通知。这个东西我是从discord channel 得到的。在那里我问过这个问题,有人建议设置自己的服务器,比如使用 firebase API 从后端服务器触发通知,然后它开始工作。
此外,您还必须在 Android 上设置 channel 并订阅该 channel 才能使其正常工作。
这是我更新后的代码。
Note this code is based on
"react-native-firebase": "4.2.0"
"react": "16.3.1"
"react-native": "0.55.3"
在最新版本中有很多方法可能会发生变化,我提供的代码仅供引用。
当时我确实遵循了以下步骤:
async checkFirebase() {
const enabled = await firebase.messaging().hasPermission();
if (enabled) {
// user has permissions
this.registerFbCloudMessagingListener();
} else {
// user doesn't have permission
this.requestFbPermission();
}
}
async requestFbPermission() {
try {
let permission = await firebase.messaging().requestPermission();
if (permission) {
this.checkFirebase();
}
// User has authorised
} catch (error) {
// User has rejected permissions
}
}
const channelConfig = {
channelId: "channelId",
channelName: "Channel Name"
};
- 订阅主题
- 创建一个 channel 并订阅它。
- 检查权限,如果没有则请求权限。
componentDidMount() {
firebase.messaging().subscribeToTopic("test");
const channel = new firebase.notifications.Android.Channel(
channelConfig.channelId,
channelConfig.channelName,
firebase.notifications.Android.Importance.Max
).setDescription("A natural description of the channel");
firebase.notifications().android.createChannel(channel);
this.checkFirebase();
}
关于android - React Native firebase 通知抬头未在某些设备中显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50875069/