userNotification 没有显示在使用 Xcode 和 Objective-c 的 iOS 应用程序的后台

问题描述

我正在尝试使用 userNotifications 在应用程序处于后台时通知位置事件。 到目前为止,我已经在我的 App Delegate 中请求了许可,并且收到了请求许可的警报。

AppDelegate.h

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

@interface AppDelegate : UIResponder <UIApplicationDelegate,UNUserNotificationCenterDelegate >
{
    UNUserNotificationCenter *center;
}
@property(readonly,copy) NSString *localizedDescription;
@end

然后在 AppDelegate.m

@synthesize localizedDescription;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
   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");
           [self->center requestAuthorizationWithOptions:(UNAuthorizationOptionBadge|UNAuthorizationOptionAlert|UNAuthorizationOptionSound)
                     completionHandler:^(BOOL granted,NSError * _Nullable error) {
                         if (! error) {
                            
                             NSLog(@"success");
                         }
                         else {
                             NSLog(@"desc%@",self.localizedDescription);
                         }
                     }];
           }}
        else {
           NSLog(@"earlier");
       }
       
    }];
return YES;
}

就像我在安装后第一次启动时所说的那样,我被要求获得许可,并在发出通知后将其包含在应用程序的设置中。

接下来在单独的 viewController 中设置实际通知。

在notificationViewController.h中

#import <UserNotifications/UserNotifications.h>
#import <UserNotifications/UNUserNotificationCenter.h>
#import <UserNotifications/UNNotificationTrigger.h>
@interface notificationViewController :  UIViewController<UNUserNotificationCenterDelegate>

在notificationViewController.m中

    -(void)locationManager:(CLLocationManager*)manager didDetermineState:(CLRegionState)state forRegion:(CLRegion*)region{
       
             if(state == CLRegionStateInside){
        
            if ([region.identifier isEqualToString:@"com.checkers.bluetooth"]) {
                

UNUserNotificationCenter* center = [UNUserNotificationCenter currentNotificationCenter];
UNMutableNotificationContent *objNotificationContent = [[UNMutableNotificationContent alloc] init];                
center.delegate = self;
                
objNotificationContent.title = [NSString localizedUserNotificationStringForKey:NSLocalizedString(@"PLAYGAME",nil) arguments:nil];
                
objNotificationContent.body = [NSString localizedUserNotificationStringForKey: NSLocalizedString(@"PLAYCHECKERS",nil)arguments:nil];
                
objNotificationContent.sound = [UNNotificationSound defaultSound];
                
objNotificationContent.badge = @([[UIApplication sharedApplication] applicationIconBadgeNumber] + 1);

                CLRegion *regiona = region;
                UNLocationNotificationTrigger* trigger = [UNLocationNotificationTrigger
                                 triggerWithRegion:regiona repeats:NO];
                NSLog(@"trigger%@",trigger);
                
NSString *identifier = @"Someone wants to play";
                UNNotificationRequest* request = [UNNotificationRequest
                       requestWithIdentifier:identifier content:objNotificationContent trigger:trigger];
                
               
                [center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
                   if (error != nil) {
                       NSLog(@"%@",error.localizedDescription);
                   }
                   else {
                       NSLog(@"fired ok");
                   }
                }];
                
                }

我在这里所做的是,当设备进入标识符为“com.checkers.bluetooth”的区域时,我想在应用程序处于后台时发出通知。

所以我首先定义了 UNNotificationCenter,然后我分配并初始化了通知的内容。

 UNUserNotificationCenter* center = [UNUserNotificationCenter currentNotificationCenter];
    UNMutableNotificationContent *objNotificationContent = [[UNMutableNotificationContent alloc] init];

objNotificationContent.title = [NSString localizedUserNotificationStringForKey:NSLocalizedString(@"PLAYGAME",nil) arguments:nil];
                    
    objNotificationContent.body = [NSString localizedUserNotificationStringForKey: NSLocalizedString(@"PLAYCHECKERS",nil)arguments:nil];
                    
    objNotificationContent.sound = [UNNotificationSound defaultSound];
                    
    objNotificationContent.badge = @([[UIApplication sharedApplication] applicationIconBadgeNumber] + 1);

然后我定义区域并使其成为位置触发器。

CLRegion *regiona = region;
                    UNLocationNotificationTrigger* trigger = [UNLocationNotificationTrigger
                                     triggerWithRegion:regiona repeats:NO];
                    NSLog(@"trigger%@",trigger);

然后我定义通知的标识符并创建一个请求。

NSString *identifier = @"Someone wants to play";
                    UNNotificationRequest* request = [UNNotificationRequest
                           requestWithIdentifier:identifier content:objNotificationContent trigger:trigger];

最后,我添加了带有完成处理程序的通知请求。

[center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
                       if (error != nil) {
                           NSLog(@"%@",error.localizedDescription);
                       }
                       else {
                           NSLog(@"fired ok");
                       }
                    }];

当我运行应用程序并让设备进入信标区域时,我得到以下日志:

2021-03-30 10:03:16.451795-0700 my little world app[604:139241] trigger<UNLocationNotificationTrigger: 0x281f8ae20; repeats: NO,region: CLBeaconRegion (identifier:'com.checkers.bluetooth',uuid:C74E2565-516C-40AF-9ABF-A9ABEBDE3188,major:(null),minor:(null))>

我认为这表明触发器正在被识别。 然后我接下来得到这个日志:

2021-03-30 10:03:16.486303-0700 my little world app[604:139430] fired ok

这表明通知中心确实添加了通知请求,并且没有任何错误地完成。

问题是应用程序从不显示通知。

我的假设是否错误,是我的代码不正确,还是顺序错误。我查看了多个示例,它们都大致相同,而且似乎是我所拥有的,所以我不知道该怎么做才能显示通知。

问题可能是因为它是一个 CLBeaconRegion 而不是触发器的 CLRegion?

有人可以帮忙吗。

我已经使用 UIAlert 来通知用户这种情况发生在应用程序在前台时发生并且效果很好,所以我知道正在调用一些东西。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...