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

时间:2017-04-28 19:29:26

标签: ios objective-c iphone xcode uilocalnotification

我正在尝试使用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];
    }

我的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();



}

1 个答案:

答案 0 :(得分:0)

您可能会在本地通知中添加操作的方向错误。在要求许可时,您需要添加类别以及操作标识符。请检查以下代码,以便在通知

中添加操作和处理操作
// 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();
    }
}