问题描述
我是新来的响应本机的人,正在尝试创建一个允许用户收听将通过数据库每周发布的播客的应用。每次发布新播客时,我希望用户收到背景通知。
我已经做过一些研究,并且能够响应打开应用程序时显示的本地通知(如下所示)。我一直在关注本教程
https://wix.github.io/react-native-notifications/docs/localNotifications
这是我的代码
import React from 'react';
import {View} from 'react-native';
import {Notifications} from 'react-native-notifications';
import HomeStack from './app/nav';
export default class App extends React.Component {
constructor(props) {
super(props);
Notifications.registerRemoteNotifications();
Notifications.events().registerNotificationReceivedForeground((notification: Notification,completion) => {
console.log(`Notification received in foreground: ${notification.title} : ${notification.body}`);
completion({alert: true,sound: false,badge: true});
});
Notifications.events().registerNotificationopened((notification: Notification,completion) => {
console.log(`Notification opened: ${notification.payload}`);
completion();
});
}
somelocalnotification = Notifications.postlocalnotification({
body: 'Local notification!',title: 'Local Notification!',//sound: "chime.aiff",category: 'SOME_CATEGORY',userInfo: {},});
render() {
return <View style={{ flex: 1 }}><HomeStack /></View>
}
}
进行一些研究后,我发现一些教程有些混乱或无法使用。我想知道创建后台通知的最佳方法是什么?
解决方法
您需要将Firebase集成到您的项目中,我也面临这个问题,即Firebase集成是解决方案,可以在打开或关闭应用程序时获取动态通知内容。npm i react-native-firebase