显示在前台的本地通知视图

Local notification views to appear in foreground

我需要在前台显示本地通知。我读到这在 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];
    }
}
}

重要的方法是这个:userNotificationCenter:willPresentNotification:withCompletionHandler:

来自documentation

completionHandler The block to execute with the presentation option for the notification. Always execute this block at some point during your implementation of this method. Specify an option indicating how you want the system to alert the user, if at all. This block has no return value and takes the following parameter:

options The option for notifying the user. Specify UNNotificationPresentationOptionNone to silence any alerts. Pass other values to specify which types of alerts you want to allow. For information about the available alert options, see UNNotificationPresentationOptions.

有关 Apple Programming Guide

的其他信息

计划是什么:

假设您收到两种类型的通知,您希望以不同的方式处理它们,一种是您希望即使应用程序在前台也能显示提醒,另一种只是声音。 这就是您使用此方法的原因。

分析UNNotification,根据你的选择,最后调用completionHandler(someValue),其中someValue是一个UNNotificationPresentationOptions(实际上是一个flag,你应该能够将它们结合起来)。你想要的至少是 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);
}