简体   繁体   English

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

[英]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() .我试过的东西至今都设置popInitialNotificationtrue运行的时候PushNotification.configure()并设置ongoingtrue时调用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.我只是没有找到一个。

Edit编辑

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.

相关问题 使用 react-native-push-notification,App 在收到新的 FCM 通知时崩溃 - Using react-native-push-notification, App crashes when receiving new FCM notification 添加 React-Native-Camera 和 React-Native-Push-Notification 后无法构建 React Native - Can't build React Native after add React-Native-Camera and React-Native-Push-Notification 当应用程序在后台并单击通知时,Android react-native-push-notification onNotification 没有被调用? - Android react-native-push-notification onNotification not called when app is in the background and notification clicked? 将senderID添加到react-native-push-notification配置时,react本地应用程序立即崩溃 - react native app instantly crashes when adding senderID to react-native-push-notification configure “ react-native-push-notification”包在Android上导致多个dex调试错误 - “react-native-push-notification” package causing multiple dex debug error on android 如何在Android上用react-native-push-notification获取所有通知,相当于PushNotificationIOS getDeliveredNotifications? - How to get all notifications with react-native-push-notification on Android, equivalent to PushNotificationIOS getDeliveredNotifications? 当 android 应用程序被杀死时,react-native-push-notification 自定义声音不起作用 - react-native-push-notification custom sound not working when android app is killed React Native 中的推送通知 - Push Notification In React Native 针对Android的React Native Push通知 - React Native Push Notification for Android React Native 推送通知问题 - Trouble with React Native Push Notification
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM