首页 > 解决方案 > 使用 React Native 的 iOS 后台通知

问题描述

我是原生反应新手,我正在尝试创建一个应用程序,允许用户收听每周通过数据库发布的播客。每次发布新播客时,我都希望用户收到后台通知。

我已经做了一些研究,并且能够对打开应用程序时显示的本地通知做出反应(如下所示) 我一直在关注本教程

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>
}
}

在做了一些研究之后,我发现一些教程有点令人困惑或不起作用。我想知道创建后台通知的最佳方法是什么?

标签: iosreact-nativepush-notification

解决方案


你需要在你的项目中集成firebase,我也面临这个问题,firebase集成是解决方案,在应用程序打开或关闭时获取动态通知内容。npm i react-native-firebase


推荐阅读