如何实现交互式通知ios8

dev*_*123 17 xcode objective-c ios8

我正在创建一个定时待办事项列表应用程序,用户可以通过滑动显示启动按钮从锁定屏幕通知启动计时器.这是iOS 8中显示的一项新功能,但几乎没有文档显示如何实现此功能.任何人都可以展示我将如何设置'开始'操作以及按下此操作时运行的代码块?如果该应用程序已关闭,此功能仍然有效吗?

Mar*_*ers 41

@Shubhendu感谢您的链接.对于那些不想坐视频的人,可以快速回顾一下您需要做什么才能在您的应用程序中包含交互式通知.

  • 定义用户可以从应用程序的通知中执行的所有操作.这些操作是使用UIMutableUserNotificationAction类创建的.

    UIMutableUserNotificationAction *action = [[UIMutableUserNotificationAction alloc] init];
    action.identifier = @"ACTION_ID"; // The id passed when the user selects the action
    action.title = NSLocalizedString(@"Title",nil); // The title displayed for the action
    action.activationMode = UIUserNotificationActivationModeBackground; // Choose whether the application is launched in foreground when the action is clicked
    action.destructive = NO; // If YES, then the action is red
    action.authenticationRequired = NO; // Whether the user must authenticate to execute the action
    
    Run Code Online (Sandbox Code Playgroud)
  • 将这些操作分类.每个类别定义用户可以从通知执行的一组动作.这些类别是使用UIMutableUserNotificationCategory创建的.

    UIMutableUserNotificationCategory *category = [[UIMutableUserNotificationCategory alloc] init];
    category.identifier = @"CATEGORY_ID"; // Identifier passed in the payload
    [category setActions:@[action] forContext:UIUserNotificationActionContextDefault]; // The context determines the number of actions presented (see documentation) 
    
    Run Code Online (Sandbox Code Playgroud)
  • 在设置中注册类别.请注意,使用注册类别不会取代要求用户发送远程通知的权限[[UIApplication sharedApplication] registerForRemoteNotifications]

    NSSet *categories = [NSSet setWithObjects:category, nil];
    NSUInteger types = UIUserNotificationTypeNone; // Add badge, sound, or alerts here
    UIUserNotificationSettings *settings = [UIUSerNotificationSettings settingsForTypes:types categories:categories];
    [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
    
    Run Code Online (Sandbox Code Playgroud)
  • 在通知有效负载中发送类别的标识符.

    {
        "aps":{
            "alert":"Here's a notification",
            ...
            "category":"CATEGORY_ID"
        }
    }
    
    Run Code Online (Sandbox Code Playgroud)
  • 通过实施UIApplicationDelegate协议方法处理应用程序委托中的用户操作: 用于本地通知的application:handleActionWithIdentifier:forRemoteNotification:completionHandler:远程 application:handleActionWithIdentifier:forLocalNotification:completionHandler:通知


Ram*_*has 8

步骤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];
}
Run Code Online (Sandbox Code Playgroud)

步骤2: 要发送此类通知,只需将类别添加到有效负载即可.

{  
    "aps" : { 
        "alert"    : "Pull down to interact.",
        "category" : "ACTIONABLE"
    }
}
Run Code Online (Sandbox Code Playgroud)

第3步:使用此方法

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();
    }
}
Run Code Online (Sandbox Code Playgroud)

1.参考链接:Obj C教程

2. Swift Code Tutorial在这里


Shu*_*ndu 3

要了解有关交互式通知的更多信息 - iOS 8 中的一项新功能,请访问以下链接

https://developer.apple.com/videos/wwdc/2014/

然后转到“iOS 通知中的新增功能”部分