使用 TaskManager 推送通知,react-native 博览会

问题描述 投票:0回答:1

我尝试在应用程序处于后台并且用户收到通知但不与其交互时运行一个函数,为此我使用任务管理器来执行此操作,因为 sdk 42 可以在后台运行一个函数,具有以下代码:

  const BACKGROUND_NOTIFICATION_TASK = 'remote-notification';

  TaskManager.defineTask(BACKGROUND_NOTIFICATION_TASK, () => {
    BackgroundTimer.runBackgroundTimer(() => { 
      console.log("holalahola");
      // schedulePushNotification();
    }, 
    2000);
  });

  Notifications.registerTaskAsync(BACKGROUND_NOTIFICATION_TASK);

这适用于 Android,因为当应用程序在后台并且我发送通知时,任务中的代码将执行,但在 ios 中不会执行,已添加到 app.json 中

remote-notification

 "ios": {
      "supportsTablet": false,
      "infoPlist": {
        "UIBackgroundModes": [
          "remote-notification"
        ]
      },
      "googleServicesFile": "./GoogleService-Info.plist",
      "bundleIdentifier": "com.xxxx"
    }

有没有办法可以在后台运行函数或检测后台事件?使用托管工作流程和博览会,正在考虑如果不可能,那么如果发生事情,后台将每 15 分钟获取一次

react-native push-notification notifications apple-push-notifications
1个回答
0
投票

我在this github thread中找到了解决方案,基本上你需要在通知负载中包含

_contentAvailable: true
来提供expo推送通知服务:

ios 的有效负载示例
{ to: <push_token>, "_contentAvailable": true}
© www.soinside.com 2019 - 2024. All rights reserved.