【问题标题】:Trouble with React Native Push NotificationReact Native 推送通知的问题
【发布时间】:2020-10-29 20:39:17
【问题描述】:

我目前正在开发一款 Android 移动应用程序。

这是一款厨房食谱应用。应用会在白天向用户发送通知。

在应用程序的设置中,用户可以选择接收通知的数量和时间(例如上午 11 点到晚上 7 点)

这就是问题的开始;

我使用react-native-push-notification 库,代码如下:

static LocalNotif(string)
{
    PushNotification.localNotification({
        vibrate: true, // (optional) default: true
        vibration: 300, // vibration length in milliseconds, ignored if vibrate=false, default: 1000
        title: "Vérifier vos produit", // (optional)
        message: string, // (required)
        largeIcon: "ic_launcher",
        smallIcon: "ic_notification",
        });    
}

接下来,我使用react-native-background-fetch 发送通知,即使应用程序没有运行

static async backFetch(delay_to_next_notif)
{
    BackgroundFetch.configure({
        minimumFetchInterval: 3600
      }, async (taskId) => {
        // This is the fetch-event callback.
        console.log("[BackgroundFetch] taskId: ", taskId);
      
        // Use a switch statement to route task-handling.
        switch (taskId) {
          case 'com.foo.customtask':
            this.LocalNotif("test")
            break;
          default:
            console.log("Default fetch task");
        }
        // Finish, providing received taskId.
        BackgroundFetch.finish(taskId);
      });
      
      // Step 2:  Schedule a custom "oneshot" task "com.foo.customtask" to execute 5000ms from now.
      BackgroundFetch.scheduleTask({
        taskId: "com.foo.customtask",
        forceAlarmManager: true,
        delay: delay_to_next_notif// <-- milliseconds
      });
}

react-native-background-fetch 的使用很奇怪。有时我从未收到通知。

是否可以使用推送通知库并创建例程,以便用户在一天中的特定时间接收通知,即使应用程序没有运行?

【问题讨论】:

    标签: javascript android react-native


    【解决方案1】:

    您可以使用 Pushnptification.configure 方法并设置您的状态,如果您的应用处于前台或后台类似这样的状态

    async componentDidMount() {
            await this.requestUserPermission();
            PushNotification.configure({
              onNotification: (notification) => {
                console.log('NOTIFICATION', notification);
                if (notification.foreground === false) {
                  console.log('app is in background')
                }
        
                this.setState({
                  msg: notification.message.body
                    ? notification.message.body
                    : notification.message,
        
                  forground: notification.foreground,
                });
              },
            });
          }
    

    在你的回报你可以做这样的事情

    {this.state.forground === true
                ? showMessage({
                    message: this.state.msg,
    
                    backgroundColor: '#1191cf',
                    type: 'default',
                    duration: 10000,
                    icon: 'success',
    
                    onPress: () => {
                      console.log('app is in forground')
                    },
                  })
            : null}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-10
      • 1970-01-01
      • 1970-01-01
      • 2015-06-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多