androidiosnotificationsreact-native-iosbackground-fetch

react-native - showing notification after a specific intervals of time - iOS


I have a scenario where I need to remind user after every specific period of time (usually 3 days) using local notification and not using internet based push-notifications.

I am using react native and have a background in Native Android specifically. I am looking for something similar to AlarmService or preferably SyncAdapter for iOS that can either be native or react-native based.


Solution

  • I ended up making my own simple module for handling scheduled local notifications... below is a snippet I am using for scheduling notification.

      UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
    
      //check notification permission
      [center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
        if (settings.authorizationStatus != UNAuthorizationStatusAuthorized) {
          // Notifications not allowed
        }else{
    
          // show notificaiton
          RCTLogInfo(@"Showing Notification with title: %@", title);
    
          //set notification content
          UNMutableNotificationContent *content = [UNMutableNotificationContent new];
          content.title = title;
          content.body = body;
          content.sound = [UNNotificationSound defaultSound];
    
          //add timer in ms
          NSTimeInterval timeInterval = interval;
          UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:timeInterval repeats:NO];
    
    
          //set identifier with any string
          NSString *identifier = id;
          UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:identifier content:content trigger:trigger];
    
          //request for scheduling notification
          [center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
            if(error != nil){
              RCTLogInfo(@"Something went wrong: %@", error);
            };
          }];
        }
      }];
    

    It's a stripped down version of my schedular, actual code also have support for adding image attachments but since this specific post was only about scheduling them, that's I had to drop some of the functionality.