1

我正在尝试构建一个水提醒应用程序。我有 3 个屏幕,我正在使用 react-navigation

  • 家(我允许用户增加他们当天的饮料量)
  • 通知(用户使用开关按钮定义是否要接收通知以及何时接收)
  • 设置(用户输入年龄、体重以确定他们每天应该喝多少)。这是用户下载应用程序时看到的第一个屏幕

我正在尝试使用 expo 推送通知及其 HTTP/2 API 向我的用户发送推送通知。但是我对此有点迷茫,并且在下面有这些问题。

  1. 下面在哪里编写推送通知代码并调用 HTTP/2 API?(App.js、通知或设置?)
  2. 如何根据用户选择确定何时发送此通知,例如每小时发送一次。

我的获取权限、存储密钥和调用 api 发送通知的代码。

    registerforPushNotifications = async () => {
    // check fox existing permission
    const { status } = await Permissions.getAsync(Permissions.NOTIFICATIONS);
    let finalStatus = status;

    // if no existing permission granted ask user
    if (status !== 'granted') {
      const { status } = await Permissions.askAsync(Permissions.NOTIFICATIONS);
      finalStatus = status;
    }

    //if no permission is granted exit the status
    if (finalStatus !== 'granted') {
      return;
    }

    // get the token
    let token = await Notifications.getExpoPushTokenAsync();

    const request = {
      to: token,
      title: "Don't forget to drink water",
    };

    _storeToken = async () => {
      try {
        await AsyncStorage.setItem('token', token.toString());
      } catch (error) {
        console.log(error.message);
      }
    };

    _storeToken();

    return fetch('https://exp.host/--/api/v2/push/send', {
      method: 'POST',
      headers: {
        'host':'exp.host',
        'accept': 'application/json',
        'content-Type': 'application/json',
        'accept-encoding': 'gzip, deflate'
      },
      body: JSON.stringify(request),
    }).then(data => {console.log(data)});
  };

我收到的回复

"_bodyInit": "{\"data\":{\"status\":\"ok\",\"id\":\"93d0f654-d8f6-4587-b4bb-ed4f5cd08b68\"}}",
4

1 回答 1

1

我决定不使用firebase,而是使用

Notifications.scheduleLocalNotificationAsync(localNotification, schedulingOptions)

这有助于我安排本地通知在未来某个特定时间或在给定时间间隔触发。

论据

localNotification (object) -- 具有 LocalNotification 中描述的属性的对象。

scheduleOptions (object) -- 描述通知何时触发的对象。

  • time (date or number) -- 一个 Date 对象,表示何时触发通知或 Unix 纪元时间中的数字。示例: (new Date()).getTime() + 1000 距离现在一秒。

  • 重复(可选)(字符串) ——“分钟”、“小时”、“日”、“周”、
    “月”或“年”。

  • (Android only) intervalMs (optional) (number)
    -- 以毫秒为单位的重复间隔

    componentDidMount() { this.willFocusSubscription = this.props.navigation.addListener('willFocus', payload => {

      // call the functions after component did mounted
      this._handleLocalNotification();
      this.listenForNotifications();
    });}
    
    // function to request permission to send notifications and schedule notifications
    _handleLocalNotification = async () => {
    // check fox existing permission
    const { status } = await Permissions.getAsync(Permissions.NOTIFICATIONS);
    let finalStatus = status;
    
    // if no existing permission granted ask user
    if (status !== 'granted') {
      const { status } = await Permissions.askAsync(Permissions.NOTIFICATIONS);
      finalStatus = status;
    }
    
    //if no permission is granted exit the status
    if (finalStatus !== 'granted') {
      return;
    }
    
    const localnotification = {
      title: 'Water Reminder',
      body: 'Dont forget to drink water!',
      android: {
        sound: true,
      },
      ios: {
        sound: true,
      },
    };
    let sendAfterFiveSeconds = Date.now();
    sendAfterFiveSeconds += 5000;
    
    const schedulingOptions = { time: sendAfterFiveSeconds };
    Notifications.scheduleLocalNotificationAsync(localnotification, schedulingOptions);
      };
    

    // 监听应用打开时是否收到通知的函数。当它收到时,它会创建并提醒

      listenForNotifications = () => {
        Notifications.addListener(notification => {
          if (notification.origin === 'received') {
            Alert.alert(localnotification.title, localnotification.body);
          }
        });
      };
于 2019-03-05T07:00:47.590 回答