当应用程序在后台时如何更新推送通知(React native)

时间:2018-10-22 17:27:02

标签: react-native push-notification

componentDidMount() {

    PushNotificationIOS.cancelAllLocalNotifications();
    PushNotificationIOS.addEventListener('localNotification', this._onNotification);
    PushNotificationIOS.checkPermissions(async res => {
        var localPushChecker = await AsyncStorage.getItem("localPushChecker");
        if (localPushChecker === null) {
            await AsyncStorage.setItem("localPushChecker", "1");
            if (res.alert !== 1) {
                PushNotificationIOS.requestPermissions();
            }
        }
        else {
            this.scheduleNotification();
        }
    });
 }


async _onNotification(notification) {
    console.log(AppState.currentState);
    if (AppState.currentState === "background") {
        try {
            var query = "http://content_to_fetch";
            var contents = (await axios.get(query)).data;
            var section = Categories.contentSection[contents[0].Content_Mark.toString()];
            var message = section + ":\n" + contents[0].Content_Title;
            var uri = "http://content_to_fetch";
            Actions.web({
                title: section,
                uri: uri
            });
        } catch (err) {
            console.log(err);
        }
    }
}

async scheduleNotification() {
    try {
        var query = "http://content_to_fetch";
        var contents = (await axios.get(query)).data;
        var section = Categories.contentSection[contents[0].Content_Mark.toString()];
        var message = section + ":" + contents[0].Content_Title;
        var fireDate = new Date(Date.now() + (15 * 1000));
        PushNotificationIOS.scheduleLocalNotification({
            fireDate: fireDate,
            alertTitle: "Myapp",
            alertBody: message,
            alertAction: "view",
            repeatInterval: "minute"
        });
    } catch (err) {
        console.log(err);
    }
}

我正在编写一个移动应用程序。我尝试在后端更新链接时更新推送通知。问题是,当我们更改链接时,我们希望将其推入后端。该应用仍在尝试推送上一个链接。

我尝试通过如下所示的setinterval()函数对其进行修复,以每100秒对其进行更新:

componentDidMount() {

........

        else {
            setInterval(() => {
              console.log('testing');
              this.scheduleNotification();
            }, 100000);

        }
    });
 }

但是我注意到只有在应用程序处于活动状态时,推送通知才会被更新。当应用程序在后台运行时,由于未执行console.log,因此推送通知将不会更新。当应用程序在后台运行时,是否可以更新推送通知?预先感谢。

0 个答案:

没有答案