本地通知视图显示在前台 [英] Local notification views to appear in foreground

查看:29
本文介绍了本地通知视图显示在前台的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在前台显示本地通知.我读到这在 iOS10 UserNotifications 框架中引入是可能的.我尝试实现它并仅在后台获取通知.在前台 -(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification 被调用但未显示通知视图.我希望它同时显示在前景和背景上,可以点击并在点击时触发方法.这可能吗,如果是的话,我的代码中缺少什么.这是代码:在 AppDelegate 中:

I need local notifications to display in foreground. I read that this is possible in the introduced in iOS10 UserNotifications framework. I try to implement it and get the notifications only in background. In foreground -(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification gets called but the view of the notification is not shown. I would like it to show on both foreground and background, to be tappable and to trigger a method when tapped. Is this possible and what am I missing in my code if yes. This is the code: In 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");
}

在视图控制器中:

- (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:

来自文档:

completionHandler 与通知的呈现选项一起执行的块.总是在某个时候执行这个块在您实施此方法的过程中.指定一个选项指示您希望系统如何提醒用户(如果有的话).这个块没有返回值并采用以下参数:

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 通知用户的选项.指定 UNNotificationPresentationOptionNone 使任何警报静音.经过其他值以指定要允许的警报类型.为了有关可用警报选项的信息,请参阅UNNotificationPresentationOptions.

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 编程指南

有什么计划:

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

Let's say that you receive 2 types of notifications, by you want to handle them differently, for one you want to show the alert even if the app is in foreground, and the other one just a sound. That's why you use this method.

分析UNNotification,根据你的选择,最后调用completionHandler(someValue),其中someValue是一个UNNotificationPresentationOptions(这实际上是一个标志,你应该能够将它们结合起来).您至少需要的是 UNNotificationPresentationOptionAlert.

Analyse the UNNotification, and according to your choices, at the end, call completionHandler(someValue), where someValue is a UNNotificationPresentationOptions (it's a flag in reality, you should be able to combine them). The one you want at least is 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);
}

这篇关于本地通知视图显示在前台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆