【发布时间】:2018-01-15 15:29:48
【问题描述】:
我的应用应该通过 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);
}
}];
}
以及在 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);
}
任何想法我错过了什么?
【问题讨论】:
标签: ios objective-c ios11 unusernotificationcenter