Search code examples
objective-cnsusernotificationcenter

Trying to get NSUserNotifications to work in my app


I am trying to update to NSUserNotifications.

I have changed how my code is written and changed where it is place but the same thing keeps happening.

In my AnotherViewController.h

#import <UserNotifications/UserNotifications.h>
#import <UserNotifications/UNUserNotificationCenter.h>

@interface AnotherViewController : UIViewController <UNUserNotificationCenterDelegate>

{
UNAuthorizationOptions UNAuthorizationOptionBadge;;
    UNAuthorizationOptions UNAuthorizationOptionAlert;
    UNAuthorizationOptions UNAuthorizationOptionSound;
}

@property(readonly, copy) NSString *localizedDescription;

In my AnotherViewController.m

@synthesize localizedDescription;

-(void)viewDidAppear:(BOOL)animated;{
[super viewDidAppear:YES];



NSLog(@"viewDidAppear");


UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
center.delegate = self;



[center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
        if (@available(iOS 14.0, *)) {
            if (settings.authorizationStatus == UNAuthorizationStatusEphemeral) {
                NSLog(@"Ephermeral");
            }
            else if (settings.authorizationStatus == UNAuthorizationStatusProvisional) {
                NSLog(@"provisional");
            }
    
  else if (settings.authorizationStatus == UNAuthorizationStatusAuthorized) {
      NSLog(@"authorized");
  }
   else if (settings.authorizationStatus == UNAuthorizationStatusDenied) {
        NSLog(@"denied");
    }
   
   else if (settings.authorizationStatus == UNAuthorizationStatusNotDetermined) {
           NSLog(@"Not determined");
       [center requestAuthorizationWithOptions:(self->UNAuthorizationOptionBadge|self->UNAuthorizationOptionAlert|self->UNAuthorizationOptionSound)
                 completionHandler:^(BOOL granted, NSError * _Nullable error) {
                     if (! error) {
                        
                         NSLog(@"success");
                     }
                     else {
                         NSLog(@"desc%@",self.localizedDescription);
                     }
                 }];
       }
  
   }
    else {
       NSLog(@"earlier");
   }
   
}];

In my log I get Not determined and then success.

 if (! error){NSLog(@"success");}

So even though the status is Not determined the app calls the method but does not display the Alert asking for permission.
Still not sure why this is happening.


Solution

  • I finally figured this out. I had to enable the modules. I went to the Target > Build Settings and set the Enable Modules (C and Objective-C modules) to YES. It was set to NO so the modules included with UNUsersNotification weren't being used. Now I get the call for permissions. Now just have to finish figuring how to get the notification to come up when the app is in the background or not running. But that is a different post and question. Thanks