使用 react-native-push-notification 删除以前的通知

dx_*_*_dt 7 android push-notification ios react-native

我正在编写一个应用程序,当后台计时器通过react-native-push-notification. 如果notification n用户没有采取行动,当notification n+1被推送时,我想删除notification n.

到目前为止,我尝试过的事情是在运行时设置popInitialNotificationtruePushNotification.configure()并在调用时设置ongoing为. 我也试过在调用然后调用时添加一个,但文档明确指出只取消预定的通知(我很确定这意味着只有未来的通知)。truePushNotification.localNotification()idlocalNotification()PushNotification.cancelLocalNotifications({id: myId})cancelLocalNotifications()

componentWillMount() {
    PushNotification.configure({
        permissions: {
            alert: true,
            badge: true,
            sound: true
        },
        popInitialNotification: true,
    });
}

doPushNotification() {
    if (AppState.currentState !== 'active') {
        PushNotification.localNotification({
            vibration: 500,
            message: 'my message',
            ongoing: true,
        });
    }
}
Run Code Online (Sandbox Code Playgroud)

目前我只在 Android 版本上工作,但很快我将在 iOS 版本上工作,因此通用解决方案(或每个解决方案)将是最有帮助的。

react-native-push-notification如果有更好的 React Native 库,我也不完全赞成;我只是没有找到一个。

编辑

我为Android找到了它。将id调用时设置PushNotification.localNotification()为与上一个通知相同的值将覆盖该通知。

我仍在我的 Mac 上安装 XCode,所以我还不能在 iOS 上测试当前的行为。但是,react-native-push-notification自述文件说这id是一个仅限 Android 的选项(原样ongoing)。我可能仍然需要帮助让 iOS 通知做我想做的事。