How to implement interactive notifications ios8

后端 未结 5 1909
名媛妹妹
名媛妹妹 2020-12-24 14:26

I\'m creating a timed todo list app in which users can start a timer from a lock screen notification by swiping to reveal a start button. This is a new feature shown in iOS

5条回答
  •  佛祖请我去吃肉
    2020-12-24 15:14

    STEP 1:

    NSString * const NotificationCategoryIdent  = @"ACTIONABLE";
    NSString * const NotificationActionOneIdent = @"ACTION_ONE";
    NSString * const NotificationActionTwoIdent = @"ACTION_TWO";
    
    - (void)registerForNotification {
    
        UIMutableUserNotificationAction *action1;
        action1 = [[UIMutableUserNotificationAction alloc] init];
        [action1 setActivationMode:UIUserNotificationActivationModeBackground];
        [action1 setTitle:@"Action 1"];
        [action1 setIdentifier:NotificationActionOneIdent];
        [action1 setDestructive:NO];
        [action1 setAuthenticationRequired:NO];
    
        UIMutableUserNotificationAction *action2;
        action2 = [[UIMutableUserNotificationAction alloc] init];
        [action2 setActivationMode:UIUserNotificationActivationModeBackground];
        [action2 setTitle:@"Action 2"];
        [action2 setIdentifier:NotificationActionTwoIdent];
        [action2 setDestructive:NO];
        [action2 setAuthenticationRequired:NO];
    
        UIMutableUserNotificationCategory *actionCategory;
        actionCategory = [[UIMutableUserNotificationCategory alloc] init];
        [actionCategory setIdentifier:NotificationCategoryIdent];
        [actionCategory setActions:@[action1, action2] 
                        forContext:UIUserNotificationActionContextDefault];
    
        NSSet *categories = [NSSet setWithObject:actionCategory];
        UIUserNotificationType types = (UIUserNotificationTypeAlert|
                                        UIUserNotificationTypeSound|
                                        UIUserNotificationTypeBadge);
    
        UIUserNotificationSettings *settings;
        settings = [UIUserNotificationSettings settingsForTypes:types
                                                     categories:categories];
    
        [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
    }
    

    STEP 2: To send this type of notification simply add the category to the payload.

    {  
        "aps" : { 
            "alert"    : "Pull down to interact.",
            "category" : "ACTIONABLE"
        }
    }
    

    STEP 3: Use this method

    application:handleActionWithIdentifier:forRemoteNotification:completionHand
    ler:
    
    [application:handleActionWithIdentifier:forLocalNotification:completionHandler:
     -> For LOCAL Notification]
    
    - (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo completionHandler:(void (^)())completionHandler {
    
        if ([identifier isEqualToString:NotificationActionOneIdent]) {
    
            NSLog(@"You chose action 1.");
        }
        else if ([identifier isEqualToString:NotificationActionTwoIdent]) {   
    
            NSLog(@"You chose action 2.");
        }    
        if (completionHandler) {
    
            completionHandler();
        }
    }
    

    1.Refer link: Obj C tutorial

    2.Swift Code Tutorial here

提交回复
热议问题