React native 在后台响应本机推送本地通知(已终止)

React native 在后台响应本机推送本地通知(已终止),react-native,React Native,我正在编写一个需要后台推送通知的应用程序(应用程序已被杀死) 它的twitch通知应用程序 我正在使用redux persist(异步存储)存储通道 我该怎么办?你有关于这方面的直接文档吗?你可以使用推送通知库和后台任务库。然后,您可以在后台任务中调用推送通知 请记住,正在运行的后台任务之间的最小间隔为15分钟。因此,间隔时间只能是15分钟或更长 为了使这个答案更有用,下面是我以前在后台处理推送通知时使用的一个实现,让您了解如何处理推送通知: import PushNotification f

我正在编写一个需要后台推送通知的应用程序(应用程序已被杀死)

它的twitch通知应用程序

我正在使用redux persist(异步存储)存储通道


我该怎么办?你有关于这方面的直接文档吗?

你可以使用推送通知库和后台任务库。然后,您可以在后台任务中调用推送通知

请记住,正在运行的后台任务之间的最小间隔为15分钟。因此,间隔时间只能是15分钟或更长


为了使这个答案更有用,下面是我以前在后台处理推送通知时使用的一个实现,让您了解如何处理推送通知:

import PushNotification from 'react-native-push-notification';

export default class NotificationService {
  constructor(onRegister, onNotification) {
    this.configure(onRegister, onNotification);
    this.lastId = 0;
  }
  // Handles a user push notification registration
  configure(onRegister, onNotification, gcm = '') {
    PushNotification.configure({
      // (optional) Called when Token is generated (iOS and Android)
      onRegister: onRegister,

      // (required) Called when a remote or local notification is opened or received
      onNotification: onNotification,

      // ANDROID ONLY: GCM Sender ID (optional - not required for local notifications, but is need to receive remote push notifications)
      senderID: gcm,

      /**
       * (optional) default: true
       * - Specified if permissions (ios) and token (android and ios) will requested or not,
       * - if not, you must call PushNotificationsHandler.requestPermissions() later
       */
      requestPermissions: true,
    });
  }
  // Send a direct push notification to the user
  localNotif() {
    this.lastId++;
    PushNotification.localNotification({
      /* Android Only Properties */
      id: '' + this.lastId,
      bigText: 'My big text that will be shown when notification is expanded',
      subText: 'This is a subText',

      /* iOS and Android properties */
      title: 'Local Notification',
      message: 'My Notification Message',
      actions: '["Yes", "No"]', // (Android only) See the doc for notification actions to know more
    });
  }

  // Schedules a push notification by a given javascript Date object
  scheduleNotif(date, title, message) {
    this.lastId++;
    PushNotification.localNotificationSchedule({
      date: date,

      /* Android Only Properties */
      id: '' + this.lastId,
      bigText: '',
      subText: '',

      /* iOS and Android properties */
      title: title,
      message: message,
    });
  }

  checkPermission(cbk) {
    return PushNotification.checkPermissions(cbk);
  }

  cancelNotif() {
    PushNotification.cancelLocalNotifications({ id: '' + this.lastId });
  }

  cancelAll() {
    PushNotification.cancelAllLocalNotifications();
  }
}
/。。。
从“react native background fetch”导入BackgroundFetch;
const initBackGroundFetch=()=>{
BackgroundFetch.configure(
{
minimumFetchInterval:15,//{//…在寄存器上执行什么操作},
()=>{//…通知时做什么}
)
const date=new date(date.now()+60*1000)//根据您的用例进行调整
notificationService.scheduleNotif(日期,“标题”,“消息”);
BackgroundFetch.finish(taskId);
},
错误=>{
log(“[js]RNBackgroundFetch未能启动”);
},
);
};
常量应用=()=>{
useffect(()=>{
initBackGroundFetch();
}, []);
返回(
// ...
);
};

NotificationService
是react本机推送通知库提供的示例的调整版本,可在此处找到:。

您可以使用推送通知库和后台任务库。然后,您可以在background tasks.thx内调用推送通知以获取答案。当应用程序被杀死时它能工作吗?是的。但是需要注意的是,后台任务运行的最小间隔是15分钟。所以间隔时间只能是每15分钟或更长。我理解thx的答案。我想试试:)顺便说一句,你可以回答这个问题。我应该用setinterval吗?或者它已经被安排了15分钟?它已经被安排为每15分钟使用
minimumFetchInterval:15
。哦,它在应用程序位于前台时工作,但在终止时工作。它不起作用:(我应该做其他事情吗?我应该设置为true吗=>你也可以尝试在应用程序运行时安排通知。然后终止应用程序,看看通知是否仍然弹出。
// ...
import BackgroundFetch from 'react-native-background-fetch';


const initBackGroundFetch = () => {
  BackgroundFetch.configure(
    {
      minimumFetchInterval: 15, // <-- minutes (15 is minimum allowed)
      // Android options
      forceAlarmManager: false, // <-- Set true to bypass JobScheduler.
      stopOnTerminate: false,
      startOnBoot: true,
      requiredNetworkType: BackgroundFetch.NETWORK_TYPE_ANY, // Network connection needed
    },
    async taskId => {
      // Do stuff with notifications, for example:
      const notificationService = new NotificationService(
         () => {//... what to do on register},
         () => {//... what to do on notification }
      )
      const date = new Date(Date.now() + 60 * 1000) // adjust according to your use case
      notificationService.scheduleNotif(date, "title", "message");
      BackgroundFetch.finish(taskId);
    },
    error => {
      console.log('[js] RNBackgroundFetch failed to start');
    },
  );
};

const App = () => {
  useEffect(() => {
     initBackGroundFetch();
  }, []);

  return (
    // ...
  );
};