安排交互式UILocalNotification - Obj-C

时间:2015-01-20 18:44:56

标签: ios objective-c uilocalnotification

我正在尝试安排互动UILocalNotifaction

我的尝试是使用以下代码,我从此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];
}

但是,此代码似乎并未在任何地方实际安排通知。我错过了什么?

enter image description here

1 个答案:

答案 0 :(得分:0)

阿波罗,

安排本地通知相对容易。 Piggy支持你引用的教程,我把它打开了,所以它可以更有意义,你可以根据需要阻止或改变代码,但这会给你一个比教程更好的起点。请注意,将其置于应用程序didFinishLaunchingWithOptions:中并非强制要求您可以在应用范围内的任何地方安排UILocalNotification。有关每种方法的完整属性,请参阅以下参考资料

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

您的结果将如预期的那样: pic1

不要忘记为所选的操作调用完成处理程序。 application:handleActionWithIdentifier:forLocalNotification:completionHandler:

请查看以下文档和参考资料,以便进一步定制。同样,我并不是说这是正确的方式,它绝不是唯一的,它只是你了解它们如何工作的一个很好的起点。操作和UILocalNotifications

有许多可自定义的属性

参考