react-native-push-notification / ios

React Native Push Notification API for iOS.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

PushNotification Repeat🔁 doesn't work

mohammad-goldast opened this issue · comments

Bug

PushNotification Repeat🔁 doesn't work on both Android and IOS platforms. I don't have any ideas to fix it. I added my code to check and let me know if someone has an idea.

Environment info

react-native info output:

    OS: macOS 11.6
    CPU: (8) arm64 Apple M1
    Memory: 123.45 MB / 8.00 GB
    Shell: 3.3.1 - /opt/homebrew/bin/fish
  Binaries:
    Node: 16.10.0 - /opt/homebrew/bin/node
    Yarn: 1.22.15 - /opt/homebrew/bin/yarn
    npm: 7.24.0 - /opt/homebrew/bin/npm
    Watchman: Not Found
  Managers:
    CocoaPods: 1.11.2 - /usr/local/bin/pod
  SDKs:
    iOS SDK:
      Platforms: DriverKit 21.2, iOS 15.2, macOS 12.1, tvOS 15.2, watchOS 8.3
    Android SDK: Not Found
  IDEs:
    Android Studio: Not Found
    Xcode: 13.2.1/13C100 - /usr/bin/xcodebuild
  Languages:
    Java: 11.0.15 - /usr/bin/javac
  npmPackages:
    @react-native-community/cli: Not Found
    react: 17.0.1 => 17.0.1
    react-native: ^0.64.0 => 0.64.3
    react-native-macos: Not Found
  npmGlobalPackages:
    *react-native*: Not Found

"react-native-push-notification": "^8.1.1",
"@react-native-community/push-notification-ios": "^1.10.1",

Steps To Reproduce

  1. Set a repetitive PushNotification for every day at 6 p.m.
  2. PushNotification triggers once.
  3. Other days nothing happened.

Describe what you expected to happen:

I expected when I set a new repetitive PushNotification for every day, it occurs at firedate/date every day.

Reproducible sample code

PushNotif Helper file:

import ...
import PushNotification, {Importance} from 'react-native-push-notification';
import PushNotificationIOS from '@react-native-community/push-notification-ios';

class PushNotifHelper {
  constructor() {
    this.developmentMode = __DEV__;
    this.unReadNotifications = [];

    this.createNotifChannel();
    this.setBadgeCount(0);
  }

  setup() {
    this.checkPermission();
    this.getUnreadNotifications();
  }

  createNotifChannel() {
    PushNotification.createChannel({
      channelId: 'scheduled-notifications',
      channelName: 'IFSGuide Notifications',
      soundName: 'default',
      importance: Importance.HIGH,
      vibrate: true,
    });
  }

  scheduleNotif(
    fireDate,
    title,
    body,
    notifId,
    extraData = {},
    hasRepeat,
    repeatsOptions,
  ) {
    return new Promise(() => {
      let options = {
        id: String(notifId),
        title: title,
        userInfo: extraData,
      };

      if (Platform.OS === 'android') {
        options = {
          ...options,
          date: new Date(fireDate),
          message: body,
          soundName: 'default',
          number: this.unReadNotifications.length + 1,
          channelId: 'scheduled-notifications',
          bigText: body,
          largeIcon: 'ic_launcher_ifs',
          smallIcon: 'ic_launcher_ifs',
          vibration: 300,
        };
      } else {
        options = {
          ...options,
          fireDate: fireDate,
          body: body,
          badge: this.unReadNotifications.length + 1,
          category: JSON.stringify(extraData),
        };
      }

      if (hasRepeat) {
        if (Platform.OS === 'android') {
          options.repeatType = repeatsOptions.type;
          options.repeatTime = repeatsOptions.interval;
        } else {
          options.repeats = true;

          const interval =
            repeatsOptions.type === 'week' ? 'dayOfWeek' : repeatsOptions.type;

          options.repeatsComponent = {
            [interval]: true,
            hour: true,
            minute: true,
          };
        }
      }

      if (Platform.OS === 'android') {
        PushNotification.localNotificationSchedule(options);
      } else {
        PushNotificationIOS.addNotificationRequest(options);
      }
    }).catch((err) => {
      logger(err, 'scheduleNotif()');
    });
  }

}

export const pushNotifHelper = new PushNotifHelper();

Set PushNotification file:

pushNotifHelper.scheduleNotif(
  time,
  name,
  notifBody,
  notif_id,
  extraData,
  hasRepeat,
 repeatComponent,
);

/* 

👇

hasRepeat: true
name: "PushNotification Title"
notifBody: "Here’s your friendly reminder for \"PushNotification Title\", Tap here to continue."
notif_id: 319621152
extraData: {}
repeatComponent: {id: 2, name: 'Every day', type: 'day', interval: 1}
time: Sun Jun 12 2022 21:05:22 GMT+0430 (Iran Daylight Time) {}
*/

Description:

We are using react-native-push-notification package to handle push notifications on Android and using @react-native-community/push-notification-ios to handle push notifications on IOS devices.

We have a Reminder feature to remind people to do their tasks. we want to be able to set a PushNotification with these types: Every day, Every week, Every month for IOS and these types: Every day, Every two days, Every three days, Every week, Every two weeks, Every month for Android devices.

Unfortunately, the repetitive push notification doesn't work perfectly on both OS. When I change these configs to set a PushNotification for every minute, it works fine but when I set a PushNotification for every day, it occurs once on the first day. On other days nothing happened.

I don't know, what should i do. I think the ReactNative app bundle kills after a day and these notifications disappear forever. Someone suggested using react-native-fetch-background package to reschedule the ReactNative app bundle but I don't know is this the right way or not?.

Thanks in advance for your helps ✌️