0

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

BraveEvidence
  • 53
  • 11
  • 45
  • 119
  • Are permissions pop up comes. For. Notifications ? – Arpit B Parekh Mar 04 '23 at 15:50
  • What do you mean by "notification doesn't work"? Does it throw any error? Does the application crash? Does the code run, but nothing happens? If there is an error, what does it say? – The Dreams Wind Mar 04 '23 at 17:47
  • Does this answer your question? [Getting local notifications to show while app is in foreground Swift 3](https://stackoverflow.com/questions/39713605/getting-local-notifications-to-show-while-app-is-in-foreground-swift-3) – The Dreams Wind Mar 04 '23 at 17:48
  • It does not throw any error. Also I need it for objectivec, the link which you have provided is for swift – BraveEvidence Mar 05 '23 at 01:38
  • @BraveEvidence you can implement the delegate with both Swift and [Objective-C](https://developer.apple.com/documentation/usernotifications/unusernotificationcenterdelegate?language=objc). What exactly looks confusing to you? It's the same API, only syntax is different. – The Dreams Wind Mar 05 '23 at 05:53
  • I am not much familiar with objective c so I am struggling. – BraveEvidence Mar 05 '23 at 08:48

1 Answers1

1

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>
Hamid Yusifli
  • 9,688
  • 2
  • 24
  • 48