2015-01-20 87 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