[英]Delete previous notification(s) using react-native-push-notification
I'm writing an app that sends a local push notification every fifteen minutes while a background timer is running via react-native-push-notification
.我正在编写一个应用程序,它每十五分钟发送一次本地推送通知,同时后台计时器通过
react-native-push-notification
。 If notification n
isn't acted on by the user, when notification n+1
is pushed, I'd like to delete notification n
.如果
notification n
未对notification n
采取行动,则在推送notification n+1
,我想删除notification n
。
The things I've tried so far are to set popInitialNotification
to true
when running PushNotification.configure()
and setting ongoing
to true
when calling PushNotification.localNotification()
.我试过的东西至今都设置
popInitialNotification
到true
运行的时候PushNotification.configure()
并设置ongoing
到true
时调用PushNotification.localNotification()
I've also tried adding an id
when calling localNotification()
and then calling PushNotification.cancelLocalNotifications({id: myId})
, but the documentation explicitly says that cancelLocalNotifications()
only cancels scheduled notifications (and I'm pretty sure it means only future ones).我还尝试在调用
localNotification()
然后调用PushNotification.cancelLocalNotifications({id: myId})
时添加一个id
,但文档明确指出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,
});
}
}
Currently I'm only working on the Android version, but soon I'll work on the iOS version, so a general solution (or the solution for each) would be most helpful.目前我只在 Android 版本上工作,但很快我将在 iOS 版本上工作,因此通用解决方案(或每个解决方案)将是最有帮助的。
I'm not totally sold on react-native-push-notification
, either, if there's a better React Native library out there;如果有更好的 React Native 库,我也不完全赞成
react-native-push-notification
; I just haven't found one.我只是没有找到一个。
I figured it out for Android.我为Android找到了它。 Setting the
id
when calling PushNotification.localNotification()
to the same value as the previous notification will overwrite the notification.将调用
PushNotification.localNotification()
时的id
设置为与前一个通知相同的值将覆盖该通知。
I'm still installing XCode on my Mac, so I can't test the current behavior on iOS just yet.我还在我的 Mac 上安装 XCode,所以我还不能在 iOS 上测试当前的行为。 However, the
react-native-push-notification
readme says that id
is an Android-only option (as is ongoing
).然而,
react-native-push-notification
自述文件说id
是一个仅限 Android 的选项( ongoing
)。 I may still need help getting the iOS notifications to do what I want.我可能仍然需要帮助让 iOS 通知做我想做的事。
The docs for react-native-push-notification state that you can cancel a ios local notification using userInfo
like this: react-native-push-notification 的文档指出,您可以使用
userInfo
取消 ios 本地通知,如下所示:
PushNotification.localNotification({
...
userInfo: { id: '123' }
...
});
PushNotification.cancelLocalNotifications({id: '123'});
I have tested this, and it works.我已经测试过了,它有效。
For android, this somehow worked:对于android,这以某种方式有效:
Creating the notification :创建通知:
PushNotification.localNotificationSchedule({
message: message ,
date: new Date(date),
repeatType: "minute",
id: JSON.stringify(id),
userInfo: { id: JSON.stringify(id) }
});
PushNotification.cancelLocalNotifications({ id: id});
Having the id stringified worked with cancelling the android push notification.将 id 字符串化可用于取消 android 推送通知。
you can add this code inside handleAppStateChange(appState)您可以在handleAppStateChange(appState) 中添加此代码
if (appState === 'active') {
PushNotification.cancelAllLocalNotifications()
}
Below code works on both iOS and Android : We need to stringify id so that it works in android and the date must be created from Date class not from some library like moment.下面的代码适用于 iOS 和 Android :我们需要对 id 进行字符串化,以便它在 android 中工作,并且日期必须从 Date 类创建,而不是从诸如 moment.js 之类的库中创建。
const LocalNotificationSchedule = (id, afterSec, message) => {
PushNotification.localNotificationSchedule({
id: id+'',
message: message,
date: new Date(Date.now() + afterSec * 1000),
playSound: true,
soundName: 'default',
vibrate: true,
vibration: 300,
playSound: true,
soundName: 'default',
ignoreInForeground: false
})
}
const CancelLocalNotifications = (id) => {
PushNotification.cancelLocalNotifications({id: id+''})
}
如果您使用的是 react-native-push-notification 库。那么您可以在 componentDidMount 部分使用此代码
PushNotification.getDeliveredNotifications((all) => {
console.log(all, "notification liast");
PushNotification.removeAllDeliveredNotifications();
});
尝试使用cancelLocalNotifications
删除通知。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.