iOS 锁定屏幕时本地通知不播放声音

iOS local notification doesn't play sound on a locked screen

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

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

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

这是我为 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);
    }
}];
}

以及 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]];

我也有中心 willPresentNotification 方法,但据我所知它只对前景有用。

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

completionHandler(UNNotificationPresentationOptionSound + UNNotificationPresentationOptionAlert);
}

知道我错过了什么吗?

好吧,我终于在 Apple public 讨论中找到了它使用 this 线程的内容。

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

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

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

我只是希望这可以让其他人在 iOS11 上处理通知时花费大量时间。请记住,WATCH 总是存在的。