如何解决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 举报,一经查实,本站将立刻删除。