react-native-push-notification / ios

React Native Push Notification API for iOS.
MIT License
740 stars 284 forks source link

Notifications with fireDate after next day do not trigger #247

Open devinjameson opened 3 years ago

devinjameson commented 3 years ago

When using addNotificationRequest, if the fireDate is not within the current day, the notification never triggers.

const [reminderTime, setReminderTime] = useState<Date>(new Date())

const request: NotificationRequest = {
  id: "id",
  title: "Friendly reminder:",
  body: "body",
  fireDate: reminderTime,
  repeats: true,
}
Notifications.scheduleNativeNotification(request)

const scheduleNativeNotification = async (
  request: NotificationRequest,
): Promise<void> => {
  const pushNotificationPermissions = await PushNotificationIOS.requestPermissions()
  if (pushNotificationPermissions.authorizationStatus === 2) {
    PushNotificationIOS.addNotificationRequest(request)
  }
}
devinjameson commented 3 years ago

@johnschoeman see above for notification delivery issue.

devinjameson commented 3 years ago

Any ideas on this? Additionally, if the fireDate is for the same day, the notification triggers once but does not repeat even if repeats is set to true.

solo29 commented 3 years ago

same issue :/

solo29 commented 3 years ago

try using PushNotificationIOS.scheduleLocalNotification until its resolved

devinjameson commented 3 years ago

I wrote my own native module to get around this @solo29. You can find it here: https://github.com/johnschoeman/building-habits-rn/tree/master/src/notifications.

And the Swift implementation: https://github.com/johnschoeman/building-habits-rn/blob/master/ios/NotificationsManager.swift

i1990jain commented 3 years ago

facing a similar issue where the notification is only triggered when addNotificationRequest is called without fireDate field in it. any Date mentioned the notification is not triggered.

jamesxabregas commented 3 years ago

So I have just encountered this same issue. The other issue is that the notification never repeats on susbsequent days either. I am pretty sure I have found the cause. In RCTConvert+Notification.m there is logic that transforms the JSON config into Objective-C types. https://github.com/react-native-push-notification-ios/push-notification-ios/blob/9fa4ff7763c099ce04bd76f3cd2d7fdbd5e8ea13/ios/RCTConvert%2BNotification.m#L120-L125 To create the trigger for the notification it uses the UNCalendarNotificationTrigger class. If you read the documentation for this it's clear that the conversion used won't work. It sets the notification to run on a specific year + month + day + hour + minute + second. As such it will never run again. To make this run daily, the year, month, and day values need to be removed from the calendar definition. This is simmilar to cron logic in a way, except with this class you need to ommit a metric to make it a wild card. For instance if you wanted a notification to be sent at 8:30am on the 2nd of each month you would specify day = 2, hour = 8, minute = 30, and second = 0, but not the month or year.

I'm going to have a go at fixing this.

YaoHuiJi commented 3 years ago

any update?