iOS 本地通知不会在锁定屏幕上播放声音

Mar*_*vie 2 objective-c ios unusernotificationcenter ios11

我的应用程序应该通过 iBeacon 测距显示位置感知广告。这没有问题:当“看到”某些信标时,应用程序会在后台唤醒,并安排及时发送的本地通知。

问题在于,发送到锁定屏幕的本地通知不会点亮屏幕,甚至不会播放任何声音或振动 - 这对营销团队来说是一场灾难。

当通知发送到前台时声音正常播放,但发送到锁定屏幕时声音丢失。

这是我为 UNUserNotificationCenter 安排本地通知的代码:

- (void)scheduleLocalNotificationImageWithTitle:(NSString*)title andBody:(NSString*)body andImagePath:(NSString*)imagePath{
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
UNNotificationAction *deleteAction = [UNNotificationAction actionWithIdentifier:@"Close"
                                                                          title:@"Close" options:UNNotificationActionOptionDestructive];

UNNotificationCategory *category = [UNNotificationCategory categoryWithIdentifier:@"NOT_CategoryImage"
                                                                          actions:@[deleteAction] intentIdentifiers:@[]
                                                                          options:UNNotificationCategoryOptionNone];
NSSet *categories = [NSSet setWithObject:category];

[center setNotificationCategories:categories];

UNNotificationAttachment *imageAttachment = [UNNotificationAttachment attachmentWithIdentifier:@"SaleNOTIF_Image" URL:[NSURL fileURLWithPath:imagePath] options:nil error:nil];

UNMutableNotificationContent *content = [UNMutableNotificationContent new];
content.title = title;
content.body = body;
content.sound = [UNNotificationSound defaultSound];
content.attachments = @[imageAttachment];

UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:0.1 repeats:NO];

NSString *identifier = @"NotificationImage";
UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:identifier content:content trigger:trigger];

[center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
    if (error != nil) {
        NSLog(@"Something went wrong: %@",error);
    }
}];
}
Run Code Online (Sandbox Code Playgroud)

以及 didFinishLaunching 中 UNUserNotificationCenter 的代码:

    UNUserNotificationCenter* center = [UNUserNotificationCenter currentNotificationCenter];
center.delegate = self;
[center requestAuthorizationWithOptions:(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge) completionHandler:^(BOOL granted, NSError * _Nullable error){
    if( !error ){

    }
}];
[[UIApplication sharedApplication] registerForRemoteNotifications];


UNNotificationCategory* generalCategory = [UNNotificationCategory
                                           categoryWithIdentifier:@"GENERAL"
                                           actions:@[]
                                           intentIdentifiers:@[]
                                           options:UNNotificationCategoryOptionAllowInCarPlay];



// Register the notification categories.
[center setNotificationCategories:[NSSet setWithObjects:generalCategory, nil]];
Run Code Online (Sandbox Code Playgroud)

我也有 center willPresentNotification 方法,但据我所知它只对前台有用。

-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler{    
CLS_LOG(@"Userinfo %@", notification.request.content.body);

completionHandler(UNNotificationPresentationOptionSound + UNNotificationPresentationOptionAlert);
}
Run Code Online (Sandbox Code Playgroud)

我有什么想法吗?

Mar*_*vie 7

好吧,我终于在苹果公开讨论中发现了它使用这个线程的目的。

这个故事本身很搞笑:我在办公室构建了我的项目,但应用程序却保持沉默。然后我回到家继续使用我的 MacBook Pro 工作——瞧!- 一切正常。我锁定了屏幕,通知弹出并按预期发出声音。有一天,我带着 MacBook 跑到办公室,构建了相同的代码,但没有任何改变:我的应用程序像两天前一样保持沉默。我已经挖出了包括代理和 GPS 在内的所有内容,但一无所获,因为……

事实证明代码没有问题,问题本身出在系统的某个地方。因此,解决方案的关键就是防止您(和您的用户)Apple Watch 重复您的应用程序的通知(仅来自 Watch 应用程序)。因为如果重复的话 iPhone 就会静音。

这可能是为了防止通知通过声音和振动触发所有内容,并且只让您知道使用最近的设备发生了令人兴奋的事情(现在,如果手表戴在手腕上,那么它比任何东西都更接近)。这就是为什么我在家时一切正常:我只是没有佩戴 Watch,因此它不必重复通知,因此也不会阻止它们。

我只是希望这可以帮助其他人在 iOS 11 上处理通知时花很多时间。请记住,始终有观看。