我需要在前台显示本地通知。我读到这可以在iOS10 UserNotifications
框架中引入。我尝试实现它并仅在后台获取通知。在前台-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification
被调用但未显示通知视图。我希望它在前景和背景上显示,可以点击并在点击时触发方法。这是可能的,如果是的话我的代码中缺少什么。这是代码:
在AppDelegate中:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
center.delegate = self;
UNAuthorizationOptions options = UNAuthorizationOptionAlert + UNAuthorizationOptionSound;
[center requestAuthorizationWithOptions:options
completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (!granted) {
NSLog(@"Something went wrong");
}
}];
return YES;
}
-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler {
NSLog(@"Will present notification");
}
-(void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler {
NSLog(@"Did Receive Notification Response");
}
在ViewController中:
- (void)locationManagerDidEnterRegionOfCamera:(NSString *)cameraName {
NSDictionary *info = @{ kRegionNotificationDictionaryCameraName : cameraName };
// Check if the switch was previously set to off and not fire notification
if ([[NSUserDefaults standardUserDefaults] boolForKey:@"switch"]) {
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"10.0.0")) {
UNMutableNotificationContent *content = [UNMutableNotificationContent new];
content.title = @"Don't forget";
content.body = @"Buy some milk";
content.sound = [UNNotificationSound defaultSound];
UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:1
repeats:NO];
NSString *identifier = @"UYLLocalNotification";
UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:identifier
content:content trigger:trigger];
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
[center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
if (error != nil) {
NSLog(@"Something went wrong: %@",error);
} else {
}
}];
} else {
[NotificationsHelper scheduleRegionNotificationWithInfo:info];
}
}
}
答案 0 :(得分:2)
重要的方法是:userNotificationCenter:willPresentNotification:withCompletionHandler:
上的其他信息completionHandler 使用演示文稿选项执行的块。始终在某个时刻执行此块 在您执行此方法期间。指定一个选项 指示您希望系统如何提醒用户(如果有的话)。这个 block没有返回值并采用以下参数:
选项用于通知用户的选项。指定
UNNotificationPresentationOptionNone
以使任何警报静音。通过 其他值,用于指定要允许的警报类型。对于 有关可用警报选项的信息,请参阅UNNotificationPresentationOptions
。
计划是什么:
假设您希望以不同的方式处理这两种类型的通知,即使应用程序位于前台,您也希望显示警报,另一种只是声音。 这就是你使用这种方法的原因。
分析UNNotification
,根据您的选择,最后致电completionHandler(someValue)
,其中someValue
是UNNotificationPresentationOptions
(这是现实中的一面旗帜,你应该能够将它们结合起来)。你想要的至少是UNNotificationPresentationOptionAlert
。
示例代码:
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler {
UNNotificationPresentationOptions options;
//Read the notification object, parse some info, decide on what do to by settings options
if (something)
{
//Show badge
options = UNNotificationPresentationOptionBadge;
}
else if (somethingElse)
{
//Show alert and make sound
options = UNNotificationPresentationOptionSound | UNNotificationPresentationOptionAlert;
}
else ...
completionHandler(options);
}