【问题标题】:Delete previous notification(s) using react-native-push-notification使用 react-native-push-notification 删除以前的通知
【发布时间】:2017-03-09 19:26:18
【问题描述】:

我正在编写一个应用程序,当后台计时器通过react-native-push-notification 运行时,它每十五分钟发送一次本地推送通知。如果notification n没有被用户操作,当notification n+1被推送时,我想删除notification n

到目前为止我尝试过的事情是在运行PushNotification.configure() 时将popInitialNotification 设置为true,并在调用PushNotification.localNotification() 时将ongoing 设置为true。我还尝试在调用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,
        });
    }
}

目前我只在开发 Android 版本,但很快我将开发 iOS 版本,所以一个通用的解决方案(或每个解决方案)将是最有帮助的。

如果有更好的 React Native 库,我也不会完全支持 react-native-push-notification;我只是还没找到。

编辑

我发现它适用于 Android。将调用PushNotification.localNotification() 时的id 设置为与上一个通知相同的值将覆盖通知。

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

【问题讨论】:

标签: android ios react-native push-notification


【解决方案1】:

docs 用于 react-native-push-notification 状态,您可以使用 userInfo 取消 ios 本地通知,如下所示:

PushNotification.localNotification({
  ...
  userInfo: { id: '123' }
  ...
});
PushNotification.cancelLocalNotifications({id: '123'});

我已经测试过了,它可以工作。

对于 android,这以某种方式起作用:

创建通知:

 PushNotification.localNotificationSchedule({
      message: message ,
      date: new Date(date),
      repeatType: "minute",
      id: JSON.stringify(id),
      userInfo: { id: JSON.stringify(id) }
    });

 PushNotification.cancelLocalNotifications({ id: id});

将 id 字符串化有助于取消 android 推送通知。

【讨论】:

  • 不仅安卓它适用于 ios 和安卓 - 谢谢
  • 那不正确。它之所以起作用,仅仅是因为iduserIndo 中的 id 值相同。在 Android 上,如果您更改 localNotificationSchedule 对象中的 id 值,它将不起作用。 userInfo 不会被过滤。即 PushNotification.cancelLocalNotifications({ id: id}) 正在寻找 id 字段,而不是 userInfo.id github.com/zo0r/…
【解决方案2】:

您可以在 handleAppStateChange(appState)

中添加此代码
   if (appState === 'active') {
     PushNotification.cancelAllLocalNotifications()
   }

【讨论】:

  • 如果您打算删除所有这些,则此方法有效。如果不是,您需要跟踪每个通知的 id
【解决方案3】:

以下代码适用于 iOS 和 Android: 我们需要对 id 进行字符串化,以便它在 android 中工作,并且日期必须从 Date 类而不是像 moment 这样的库中创建。

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+''})
}

【讨论】:

  • 点评来源: Stack Overflow 上不鼓励仅使用代码的答案,因为它们没有解释它是如何解决问题的。请编辑您的答案以解释此代码的作用以及它如何回答问题,以便它对 OP 以及其他也有类似问题的用户有用。请参阅:How do I write a good answer?。谢谢
【解决方案4】:

如果您使用的是 react-native-push-notification 库。 那么您可以在 componentDidMount 部分中使用此代码

PushNotification.getDeliveredNotifications((all) => {
   console.log(all, "notification liast");
   PushNotification.removeAllDeliveredNotifications();
});

您可以在此处获取所有通知列表并在打开应用后清除所有列表。 这些代码在 ANDROID 设备中为我工作(我没有在 IOS 设备中测试它)

【讨论】:

    【解决方案5】:

    尝试使用cancelLocalNotifications删除通知。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-07-03
      • 2021-07-04
      • 1970-01-01
      • 2020-08-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多