2015-01-20 51 views
2

我正在尝试安排交互式UILocalNotifactionSchedule Interactive UILocalNotification - Obj-C

我的尝试是使用下面的代码,这是我从这个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

+0

你有没有implemen t应用程序:handleActionWithIdentifier:forLocalNotification:completionHandler:'你的应用程序委托中的方法? – soulshined 2015-01-20 19:01:47

+0

@soulshined我的意思是不,但不应该对通知的安排有任何影响... – Apollo 2015-01-21 03:13:23

+0

你仍然必须像任何其他通知一样安排本地通知。在这里,您只需注册该类型的通知即属于一个类别。 – soulshined 2015-01-21 03:15:30

回答

0

阿波罗,

调度的本地通知是比较容易的。小猪对你引用的教程做了一些补充,我有点改变了它,所以它可以变得更有意义,你可以根据需要阻止或改变代码,但是这会给你一个比教程更好的起点。请注意,这并不是强制要求在应用程序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


参考

+0

如果我想在for循环中触发10个通知,该怎么办?每个通知具有不同的类别和不同的操作按钮 – iOSdev 2015-04-23 12:40:35

+0

我遵循相同的步骤并在for循环中一次触发10到15个通知,并且我的第一个通知显示了操作按钮,第二个通知显示了操作按钮,第二个通知无法追上按钮 – iOSdev 2015-04-23 12:46:12

+0

@iOSdev这似乎适合一个不同的问题。在另一个问题中不回答另一个问题,造成混乱,寻找相同答案的人不会找到它,因为它与这个无关的主题是一样的。如果与这个问题无关,我会建议开始一个新的问题 – soulshined 2015-04-23 16:49:29