如何在ios 10中生成本地通知,当您的应用程序处于默认状态时?当您的应用程序处于forground状态时,它将';那不行

如何在ios 10中生成本地通知,当您的应用程序处于默认状态时?当您的应用程序处于forground状态时,它将';那不行,ios,objective-c,Ios,Objective C,下面是我在应用程序中执行的步骤 在myappdelegate中 #import <UserNotifications/UserNotifications.h> - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after appli

下面是我在应用程序中执行的步骤

在myappdelegate中

#import <UserNotifications/UserNotifications.h>


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.


    UNUserNotificationCenter* center = [UNUserNotificationCenter currentNotificationCenter];
      center.delegate = (id)self;

    [center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert + UNAuthorizationOptionSound)
                          completionHandler:^(BOOL granted, NSError * _Nullable error) {


                              // Enable or disable features based on authorization.
                          }];







    return YES;
}
现在,当我的应用程序在前台中时,不会生成通知

在ios 9中,我使用

 UILocalNotification* localNotification = [[UILocalNotification alloc] init];
    localNotification.soundName = @"Default";
    localNotification.alertBody = @"my notification;)";
    localNotification.fireDate = [NSDate date];
    localNotification.category = @"Email";
    localNotification.repeatInterval = kCFCalendarUnitDay;
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
此代码在ios 9中可用,但在ios 10中不推荐使用UILocalNotification


那么,当您的应用程序在前台时,如何在ios 10中生成本地通知呢?

请尝试将其与ios 10中的
UNUserNotificationCenterDelegate
一起使用

iOS 10添加了
UNUserNotificationCenterDelegate
协议,用于在应用程序处于前台时处理通知


若要在应用程序位于前台时显示横幅消息,请使用以下方法

iOS 10,Swift 3:

//当应用程序在前台收到推送通知时,将调用此方法

func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) 
{
    completionHandler(
       [UNNotificationPresentationOptions.alert,
        UNNotificationPresentationOptions.sound,
        UNNotificationPresentationOptions.badge])
}

我已经在使用用户通知框架。我的问题是,当你的应用程序在前台时,如何在ios 10中生成本地通知?我可以在swift it中向你发布解决方案,该解决方案是oke?可能与@Mingebag重复,这没问题。willPresent通知:在本地通知中工作?
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) 
{
    completionHandler(
       [UNNotificationPresentationOptions.alert,
        UNNotificationPresentationOptions.sound,
        UNNotificationPresentationOptions.badge])
}