zo0r / react-native-push-notification

React Native Local and Remote Notifications
MIT License
6.76k stars 2.05k forks source link

Cancel recurring/repeating notifications #1749

Open ayusch-apps-curefit opened 3 years ago

ayusch-apps-curefit commented 3 years ago

I've scheduled local notifications using this library. The notifications repeat every day at a specific time.

But now I want to delete these notifications but couldn't find any way! This is very surprising that library doesn't support this out of the box.

I get all notifications and identify which one I want to cancel based on some attribute. Then I call cancelLocalNotificationForId on that notification. I do this operation on app launch.

But the problem is that this only cancels it for that particular day and not all days. How do I cancel for all recurring days?

Dallas62 commented 3 years ago

Hi @ayusch-apps-curefit, Are you sure this is the same id as the notification triggered? The Android library re-schedule the next notification once the previous is triggered. If you cancel the first notification, the others can't trigger.

Can you also try cancelAllLocalNotifications ?

ayusch-apps-curefit commented 3 years ago

Hi @Dallas62 I'm sure it's the same id as the notifications I've scheduled have a custom sound. So I get all notifications, filter them by sound name (since I didn't provide ID when scheduling) and then pass the id to cancelLocalNotificationForId. So there's no question of wrong ID since it's coming from the scheduled notification itself.

It's happening on IOS devices. Complaints have been from iOS. Not sure about Android.

Dallas62 commented 3 years ago

If it's from the iOS side, you should look to the iOS library 😉

ayusch-apps-curefit commented 3 years ago

I'm using this library to schedule notifications locally like below:

    PushNotification.localNotificationSchedule({
      id,
      channelId,
      channelName,
      userInfo: userInfo || {},
      largeIcon: "ic_launcher",
      smallIcon: "notify_logo",
      date: date || new Date(Date.now() + 60 * 1000),
      title,
      message,
      ...rest,
    });

What's the other library to look at ?

Dallas62 commented 3 years ago

This library doesn't provide the iOS code for notification: https://github.com/zo0r/react-native-push-notification#installation

NOTE: If you target iOS you also need to follow the installation instructions for PushNotificationIOS since this package depends on it.

Also you mentioned a method that doesn't exist cancelLocalNotificationForId.

ayusch-apps-curefit commented 3 years ago

@Dallas62 My bad. The method I mentioned is from a Util class I've created. But it internally calls: PushNotification.cancelLocalNotifications({ id: notifId });

Also, dependency for PushNotificationIOS was already added.

Dallas62 commented 3 years ago

This could be related to: https://github.com/react-native-push-notification-ios/push-notification-ios/issues/190

ayusch-apps-curefit commented 3 years ago

@Dallas62 That's something else. I'm trying to delete a notification which is recurring. Basically clear out all the future recurring notifications.

Dallas62 commented 3 years ago

Hi @ayusch-apps-curefit ,

I tried to reproduce the issue on the exemple project, and this is not occurring. Without a reproductible exemple I will not able to help you more. I suggest you to re-check the type&value of the id. Also be sure that the notification is not re-schedule.

If the issue still happen, check this repository https://github.com/react-native-push-notification-ios/ This is the repository which is used for iOS notification, this library just wrap the iOS library.

Regards,

ayusch-apps-curefit commented 3 years ago

@Dallas62 The notification is not re-scheduled.

Can I use: PushNotification.cancelAllLocalNotifications() ?

I hope it will clear future recurring notifications as well.

Dallas62 commented 3 years ago

You can use it.

From test and documentation of Apple, when you scheduled a recurring notification, it's impossible to cancel one occurrence of the notification. It's all or nothing. There is only two cases this issue can occur: 1) notification is not canceled 2) notification is rescheduled by code

Without reproducible exemple, can't help more...

ayusch-apps-curefit commented 3 years ago

Sure @Dallas62 This was really helpful. Thanks for the prompt reply :)

syedumeraliSalsoft3552 commented 3 years ago

I've scheduled local notifications using this library. The notifications repeat every day at a specific time.

But now I want to delete these notifications but couldn't find any way! This is very surprising that library doesn't support this out of the box.

I get all notifications and identify which one I want to cancel based on some attribute. Then I call cancelLocalNotificationForId on that notification. I do this operation on app launch.

But the problem is that this only cancels it for that particular day and not all days. How do I cancel for all recurring days?

I've scheduled local notifications using this library. The notifications repeat every day at a specific time.

But now I want to delete these notifications but couldn't find any way! This is very surprising that library doesn't support this out of the box.

I get all notifications and identify which one I want to cancel based on some attribute. Then I call cancelLocalNotificationForId on that notification. I do this operation on app launch.

But the problem is that this only cancels it for that particular day and not all days. How do I cancel for all recurring days?

Can I know how you made it possible that the notifications repeat every day at a specific time? Because I am stuck there and can't make it possible.

mparramont commented 3 years ago

Can I know how you made it possible that the notifications repeat every day at a specific time? Because I am stuck there and can't make it possible.

@syedumerali I think they're using the repeatType option in the PushNotification.localNotification function: https://github.com/zo0r/react-native-push-notification#local-notifications

sangolariel commented 3 years ago

I have an issue. The push notification schedule doesn't work on ios. I pass repeatType = "day' but it just notif for the first time. 2nd day it not work. Have any idea about it? Thanks.

mparramont commented 3 years ago

@sangolariel I think I had the same problem you're having. The issue lies in the companion push-notifications-ios library where notifications scheduled for the next day will be scheduled with date = null. Here is a bug report on this repo, the author of the library says it's a problem with push-notifications-ios. In that repo here is the related bug report. Somebody mentioned to use a deprecated method as a workaround, and it worked great for me.

This is how my notification code looks now:

    if (Platform.OS === 'ios') {
      // using a deprecated method since the current one seems to have a bug:
      // https://github.com/react-native-push-notification-ios/push-notification-ios/issues/247#issuecomment-751282455
      PushNotificationIOS.scheduleLocalNotification({
        fireDate: todayAt9PM.toISOString(),
        alertTitle: title,
        alertBody: message,
        userInfo,
        repeatInterval: 'day'
      })
      storeScheduledNotificationId()
    } else {
      PushNotification.localNotificationSchedule({...})
    }

Let me know if that works for you!

sangolariel commented 3 years ago

Hi, @mparramont, Thanks for your support. I have to wait for some days to make sure that it works. But it seems not to repeat by day. Here is the code of "@react-native-community/push-notification-ios": "^1.8.0".

> if (Platform.OS === 'ios') {
      PushNotificationIOS.addNotificationRequest({
          id: id ? id.toString() : '',
          fireDate: _date,
          title: `⏰ ${title}`,
          body: message,
          userInfo,
          repeats: true,
    });
  } else {
      PushNotification.localNotificationSchedule({
          id,
          date: _date,
          title: `⏰ ${title}`,
          message,
          channelId: CHANNEL_ID,
          repeatType: 'day',
          userInfo,
      });
  }

I set the schedule notification by 8hAM every day but it just works for the first time.

mparramont commented 3 years ago

I think there might be a couple issues with your PushNotificationIOS.addNotificationRequest() call:

  1. You're passing repeats: true, which I didn't know it was an available option. In my code I'm passing repeatInterval: 'day'
  2. fireDate should be an ISO String, as you can see in my example above I'm using .toISOString(), make sure you're doing that too.

I suggest you to use PushNotification.getScheduledLocalNotifications(callback) (doc is in the readme) to debug it. What I did to debug it on my side was

Doing that, I could see that with PushNotification.localNotificationSchedule() in iOS the date of the second notification was null, while with PushNotificationIOS.addNotificationRequest() the date was correctly set to the next day.

Hope that helps!

sangolariel commented 3 years ago

@mparramont my code use ""@react-native-community/push-notification-ios": "^1.8.0"." so the PushNotification.getScheduledLocalNotifications(callback) was "Deprecated" Pls check the lastest version.

"fireDate should be an ISO String, as you can see in my example above I'm using .toISOString(), make sure you're doing that too."=> I saw in this lib "fireDate" has Type is "Date" it not necessary an "ISOString".

mparramont commented 3 years ago

PushNotification.getScheduledLocalNotifications(callback) is from react-native-push-notification, not @react-native-community/push-notification-ios :)

sangolariel commented 3 years ago

Sorry I copy wrong :)) PushNotificationIOS.scheduleLocalNotification(details); here @mparramont

mparramont commented 3 years ago

Yes, I'm using that function even though it's deprecated, since it works for the purpose of have working repeating notifications.

sangolariel commented 3 years ago

I'll try it :((( Hope it runs i ll feedback on my result when I make sure that it works thanks for your time.

sangolariel commented 3 years ago

Haha, it works for me even though it's deprecated. I don't believe this. some react emoji for you kk :))

cassidypignatello commented 3 years ago

Does anyone happen to know the correct way to set an id using scheduleLocalNotification? This workaround works great for repeating notifications, but when I try to add an id to the object I'm passing to the function, it just ignores it and sets an id with a random string of letters and numbers like "id": "E15F3150-3803-4C53-A73B-CD42255EE760". That makes it pretty much impossible to display the notification time to the user since I'm using the id to look that up.

Dallas62 commented 3 years ago

Does anyone happen to know the correct way to set an id using scheduleLocalNotification? This workaround works great for repeating notifications, but when I try to add an id to the object I'm passing to the function, it just ignores it and sets an id with a random string of letters and numbers like "id": "E15F3150-3803-4C53-A73B-CD42255EE760". That makes it pretty much impossible to display the notification time to the user since I'm using the id to look that up.

Read the readme please! A valid int32. Regards

cassidypignatello commented 3 years ago

@Dallas62 I'm referring to the iOS library and workaround mentioned above where @mparramont is using PushNotificationIOS.scheduleLocalNotification. For your PushNotification.localNotificationSchedule function I'm passing id a valid int32 and it works as expected. I don't believe the deprecated PushNotificationIOS.scheduleLocalNotification accepts an id at all, or at least that's not clear in their docs if they do.

mparramont commented 3 years ago

For the workaround I'm using PushNotification.getScheduledLocalNotifications(callback) after the call to get the ID of the notification and store it in Async Storage for later use.

cassidypignatello commented 3 years ago

@mparramont great idea, thanks for letting me know!