Search code examples
iosobjective-cuilocalnotification

Local Notification for iOS in objectiveC does not work


I am trying to trigger local notification for iOS with ObjectiveC. I can't use Swift as the react native's new architecture does not support it.

In my AppDelegate.m file I have added following code

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    
    UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
    UNAuthorizationOptions options = UNAuthorizationOptionAlert + UNAuthorizationOptionSound;
    [center requestAuthorizationWithOptions:options
     completionHandler:^(BOOL granted, NSError * _Nullable error) {
      if (!granted) {
        NSLog(@"Something went wrong");
      }
    }];
    [center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
      if (settings.authorizationStatus != UNAuthorizationStatusAuthorized) {
        // Notifications not allowed
      }
    }];

    return YES;
}

And in my button's action I am calling below code

- (IBAction)clickMe:(id)sender {
   

    UNMutableNotificationContent *content = [UNMutableNotificationContent new];
    content.title = @"Don't forget";
    content.body = @"Buy some milk";
    content.sound = [UNNotificationSound defaultSound];
    UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger
      triggerWithTimeInterval:1 repeats:NO];
    NSString *identifier = @"UYLLocalNotification";
    UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:identifier
                                                                          content:content trigger:trigger];
    UNUserNotificationCenter* center = [UNUserNotificationCenter currentNotificationCenter];
    [center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
      if (error != nil) {
        NSLog(@"Something went wrong: %@",error);
      }
    }];


}

I want to trigger notification as soon as the button is pressed


Solution

  • If you want to present your notification in Foreground you need to set UNUserNotificationCenterDelegate in your didFinishLaunchingWithOptions method.

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
        
        UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
        // Setting delegate
        center.delegate = self;
        UNAuthorizationOptions options = UNAuthorizationOptionAlert + UNAuthorizationOptionSound;
        [center requestAuthorizationWithOptions:options
         completionHandler:^(BOOL granted, NSError * _Nullable error) {
          if (!granted) {
            NSLog(@"Something went wrong");
          }
        }];
        [center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
          if (settings.authorizationStatus != UNAuthorizationStatusAuthorized) {
            // Notifications not allowed
          }
        }];
    
        return YES;
    }
    

    Then add this delegate method:

    - (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler {
        
        completionHandler(UNNotificationPresentationOptionBanner);
        
    }
    

    Also don't forget to adopt UNUserNotificationCenterDelegate protocol in your AppDelegate.h class interface:

    @interface AppDelegate : UIResponder <UIApplicationDelegate, UNUserNotificationCenterDelegate>