Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 本地通知中未显示的操作_Ios_Objective C_Iphone_Xcode_Uilocalnotification - Fatal编程技术网

Ios 本地通知中未显示的操作

Ios 本地通知中未显示的操作,ios,objective-c,iphone,xcode,uilocalnotification,Ios,Objective C,Iphone,Xcode,Uilocalnotification,我正在尝试使用2个操作生成本地通知。然而,即使通知正在弹出。我看不到这些动作 这是因为iOS 10中的新功能吗 我的viewcontroller,h具有以下代码: -(void)createNotifications: (int)seconds{ UILocalNotification *local = [[UILocalNotification alloc]init]; local.fireDate = [[NSDate date]dateByAddingTim

我正在尝试使用2个操作生成本地通知。然而,即使通知正在弹出。我看不到这些动作

这是因为iOS 10中的新功能吗

我的viewcontroller,h具有以下代码:

-(void)createNotifications: (int)seconds{
        UILocalNotification *local = [[UILocalNotification alloc]init];
        local.fireDate = [[NSDate date]dateByAddingTimeInterval:seconds];
        local.timeZone = nil;
        local.alertBody = @"Alert body";
        local.alertTitle = @"Alert Title";
        local.alertAction = @"Okay";
        local.soundName = UILocalNotificationDefaultSoundName;
        local.applicationIconBadgeNumber = 4127;
        local.category = @"MAIN_CATEGORY";


        [[UIApplication sharedApplication] scheduleLocalNotification:local];
    }

    -(void)requestPermissionToNotify{

        UIMutableUserNotificationAction *action  = [[UIMutableUserNotificationAction alloc]init];
        action.identifier = @"FLOAT_ACTION";
        action.title = @"float";
        action.activationMode = UIUserNotificationActivationModeBackground;
        action.destructive = YES;
        action.authenticationRequired = NO;

        UIMutableUserNotificationAction *stingAction  = [[UIMutableUserNotificationAction alloc]init];
        stingAction.identifier = @"STING_ACTION";
        stingAction.title = @"sting";
        stingAction.activationMode = UIUserNotificationActivationModeForeground;
        stingAction.destructive = NO;
        stingAction.authenticationRequired = NO;

        UIMutableUserNotificationCategory *category = [[UIMutableUserNotificationCategory alloc]init];
        category.identifier = @"MAIN_CATEGORY";

        NSSet *categories = [NSSet setWithObjects:category, nil];


        UIUserNotificationType types = UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert;
        UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:types categories:categories];
        [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
        [category setActions:@[action,stingAction] forContext:UIUserNotificationActionContextDefault];
    }
my appdelegate.m具有回调处理程序:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    application.applicationIconBadgeNumber = 0;
    UILocalNotification *localnotif = launchOptions[UIApplicationLaunchOptionsLocalNotificationKey];

    if(localnotif){
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Received while launch" message:localnotif.alertBody preferredStyle:UIAlertViewStyleDefault];
        UIAlertAction *aa = [UIAlertAction actionWithTitle:@"okay" style:UIAlertViewStyleDefault handler:nil];
        [alert addAction:aa];

        dispatch_async(dispatch_get_main_queue(), ^{
            [application.keyWindow.rootViewController presentViewController:alert animated:YES completion:nil];
        });


    }
    return YES;
}

-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{

    application.applicationIconBadgeNumber = 0;

    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Received while running" message:notification.alertBody preferredStyle:UIAlertViewStyleDefault];
    UIAlertAction *aa = [UIAlertAction actionWithTitle:@"okay" style:UIAlertViewStyleDefault handler:nil];
    [alert addAction:aa];

    dispatch_async(dispatch_get_main_queue(), ^{
        [application.keyWindow.rootViewController presentViewController:alert animated:YES completion:nil];
    });
}

-(void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification completionHandler:(void (^)())completionHandler{

        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Received while action" message:identifier preferredStyle:UIAlertViewStyleDefault];
        UIAlertAction *aa = [UIAlertAction actionWithTitle:@"okay" style:UIAlertViewStyleDefault handler:nil];
        [alert addAction:aa];

        dispatch_async(dispatch_get_main_queue(), ^{
            [application.keyWindow.rootViewController presentViewController:alert animated:YES completion:nil];
        });

    completionHandler();



}

在本地通知中添加操作的方向可能错误。在请求权限时,您需要添加类别和操作标识符。请检查以下代码,以在通知中添加操作和处理操作点击

// Ask for permission to allow Notification
if(SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"10.0")){
        UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];

        UNNotificationAction *action = [UNNotificationAction actionWithIdentifier:@"ActionIdentifier" title:@"NewAction" options:UNNotificationActionOptionNone];
        NSArray *notificationActions = @[action];

        UNNotificationCategory *category = [UNNotificationCategory categoryWithIdentifier:@"Notification" actions:notificationActions intentIdentifiers:@[] options:UNNotificationCategoryOptionCustomDismissAction];
        NSSet *categories = [NSSet setWithObject:category];

        [center setNotificationCategories:categories];
        [center requestAuthorizationWithOptions:(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge) completionHandler:^(BOOL granted, NSError * _Nullable error){
            if(!granted) {

            }
        }];
    }
    else {
        UIMutableUserNotificationAction *action = [[UIMutableUserNotificationAction alloc] init];
        action.identifier = @"ActionIdentifier";
        action.title = @"NewAction";
        action.activationMode = UIUserNotificationActivationModeBackground;
        action.destructive = NO;
        action.authenticationRequired = NO;

        UIMutableUserNotificationCategory *category = [[UIMutableUserNotificationCategory alloc] init];
        category.identifier = @"ActionIdentifier";
        [category setActions:@[action] forContext:UIUserNotificationActionContextDefault];
        [category setActions:@[action] forContext:UIUserNotificationActionContextMinimal];

        NSSet *categories = [NSSet setWithObjects:category, nil];
        [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:categories]];
    }

// Handle Notification for iOS 10
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler {
    // Handle Notification here
}

- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)())completionHandler {
    NSString *actionIdentifier = response.actionIdentifier;
    BOOL newActionTapped = [actionIdentifier isEqualToString:@"NewAction"];

    if(snooze) {
            // Handle NewAction action button here
    }
    else {
        // Handle Notification here
    }
    completionHandler();
}

// Handle Notification for iOS 9 or lower
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
    // Handle Notification here
}

- (void)application:(UIApplication *)application handleActionWithIdentifier:(nullable NSString *)identifier forLocalNotification:(UILocalNotification *)notification completionHandler:(void(^)())completionHandler {
    // This method will be called when user tap on Action
    // Handle NewAction action button here
    if (completionHandler) {
        completionHandler();
    }
}

可能重复的谢谢你,我正在调查:)检查此链接:为清楚的理解