安排交互式UILocalNotification - Obj-C [英] Schedule Interactive UILocalNotification - Obj-C

查看:125
本文介绍了安排交互式UILocalNotification - Obj-C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试安排交互式 UILocalNotifaction



我的尝试是使用以下代码,我从这里获取了

  • iOS8通知操作&属性:文档


  • I'm trying to schedule an interactive UILocalNotifaction.

    My attempt has been to use the following code, which I grabbed from this tutorial:

    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];
    }
    

    However, this code doesn't seem to actually schedule the notification anywhere. What am I missing?

    解决方案

    Apollo,

    Scheduling a local notification is relatively easy. Piggy backing off the tutorial you referenced I kind of switched it up so it can make more sense, you can deter or alter the code as need be, but this will give you a better starting point than the tutorial did. Please know it's not mandatory to put this in application didFinishLaunchingWithOptions: you can schedule a UILocalNotification anywhere app-wide. See the references below for complete properties of each method

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    //Here we are just going to create the actions for the category. 
    UIMutableUserNotificationAction *acceptAction = [self createAction];
    UIMutableUserNotificationAction *laterAction = [self createLaterAction];
    laterAction.title = @"Not Now";
    
    //Creating the category and assigning the actions:
    UIMutableUserNotificationCategory *acceptCategory = [self createCategory:@[acceptAction, laterAction]];
    
    //Register the categories 
    [self registerCategorySettings:acceptCategory];
    
    //For testing purposes we will just fire a local notification on load. This is just for reference, but you don't have to call it in applicationDidFinishLaunching
    [self fireLocalNotification];
    
    return YES;
    }
    
    //Create a category
    - (UIMutableUserNotificationCategory *)createCategory:(NSArray *)actions {
    UIMutableUserNotificationCategory *acceptCategory = [[UIMutableUserNotificationCategory alloc] init];
    acceptCategory.identifier = @"ACCEPT_CATEGORY";
    
    [acceptCategory setActions:actions forContext:UIUserNotificationActionContextDefault];
    
    return acceptCategory;
    }
    
    //Register your settings for that category 
    - (void)registerCategorySettings:(UIMutableUserNotificationCategory *)category {
    UIUserNotificationType types = (UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound);
    
    NSSet *categories = [NSSet setWithObjects:category, nil];
    UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:types categories:categories];
    
    [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
    }
    
    //Create Actions Methods
    - (UIMutableUserNotificationAction *)createAction {
    
    UIMutableUserNotificationAction *acceptAction = [[UIMutableUserNotificationAction alloc] init];
    acceptAction.identifier = @"ACCEPT_IDENTIFIER";
    acceptAction.title = @"Accept";
    
    acceptAction.activationMode = UIUserNotificationActivationModeBackground;
    acceptAction.destructive = NO;
    
    // If YES requires passcode
    acceptAction.authenticationRequired = NO;
    
    return acceptAction;
    }
    
    - (UIMutableUserNotificationAction *)createLaterAction {
    
    UIMutableUserNotificationAction *laterAction = [[UIMutableUserNotificationAction alloc] init];
    laterAction.identifier = @"LATER_IDENTIFIER";
    laterAction.title = @"Not Now";
    
    laterAction.activationMode = UIUserNotificationActivationModeBackground;
    laterAction.destructive = NO;
    laterAction.authenticationRequired = NO;
    
    return laterAction;
    }
    
    - (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings {
    
    UIUserNotificationType allowedTypes = [notificationSettings types];
    NSLog(@"Registered for notification types: %u", allowedTypes);
    }
    
    //Fire a local Notification: For testing purposes we are just going to fire this immediately after launch. But this will give you a general idea of how to create a UILocalNotification app-wide:
    - (void)fireLocalNotification {
    UILocalNotification *notification = [[UILocalNotification alloc] init];
    notification.alertBody = @"We are just testing -";
    notification.category = @"ACCEPT_CATEGORY";
    
    notification.fireDate = [NSDate dateWithTimeInterval:15 sinceDate:[NSDate date]];
    
    [[UIApplication sharedApplication] scheduleLocalNotification:notification];
    }
    

    And your outcome will be as anticipated:

    Don't forget to call the completion handlers for the actions that were selected. application:handleActionWithIdentifier:forLocalNotification:completionHandler:

    Please review the documentation and references below for further customization. Again, i'm not saying this is the right way and it's definetly not the only, it's just a great starting point for you to understand how they work. There are lots of customizable properties for actions and UILocalNotifications


    REFERENCES

    这篇关于安排交互式UILocalNotification - Obj-C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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