2014-10-08 50 views
2

我正在做一個使用ios 8交互式通知功能的示例應用程序。就顯示通知而言,一切正常。但是,當我在通知中選擇某個操作時,將調用該操作的相應方法,但該方法內的代碼未在應用程序中反映出來。的代碼交互式通知中的操作未反映在應用程序中

我的應用程序委託部分其中的通知實現

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 
[self setupNotification]; 
// Override point for customization after application launch. 
return YES; 
} 

-(void)setupNotification 
{ 
UIMutableUserNotificationAction *accept =[[UIMutableUserNotificationAction alloc]init]; 
accept.identifier [email protected]"Accepted"; 
[email protected]"Accept"; 
accept.destructive=NO; 
accept.authenticationRequired=YES; 
accept.activationMode=UIUserNotificationActivationModeBackground; 

UIMutableUserNotificationAction *decline =[[UIMutableUserNotificationAction alloc]init]; 
decline.identifier [email protected]"Rejected"; 
[email protected]"Decline"; 
decline.destructive=YES; 
decline.authenticationRequired=YES; 
decline.activationMode=UIUserNotificationActivationModeBackground; 

UIMutableUserNotificationCategory *actions =[[UIMutableUserNotificationCategory alloc]init]; 
[actions setIdentifier:@"customActions"]; 
[actions setActions:@[decline,accept] forContext:UIUserNotificationActionContextDefault]; 

NSSet* categories = [NSSet setWithArray:@[actions]]; 
UIUserNotificationSettings* settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert) categories:categories]; 
[[UIApplication sharedApplication] registerUserNotificationSettings:settings]; 
} 

- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification completionHandler:(void(^)())completionHandler 
{ 
    if([notification.category isEqualToString:@"customActions"]) 
{ 
    ViewController *screen =[[ViewController alloc]init]; 
    if([identifier isEqualToString:@"Rejected"]) 
    { 

     [screen deleteAction]; 
    } 
    else if([identifier isEqualToString:@"Accepted"]) 
    { 
     [screen acceptAction]; 
    } 
} 

// Important to call this when finished 
completionHandler(); 
} 

我的視圖控制器的方法的內部

- (void)viewDidLoad 
    { 

    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 
    } 

    -(void)deleteAction 
    { 
    NSLog(@"delete"); 
    [self.view setBackgroundColor:[UIColor redColor]]; 

    } 
    -(void)acceptAction 
    { 
    NSLog(@"accept"); 
    [self.view setBackgroundColor:[UIColor greenColor]]; 
    } 

    - (IBAction)sendNotification:(id)sender { 

    UILocalNotification* notification = [[UILocalNotification alloc] init]; 
    [notification fireDate ]; 
    [notification setAlertBody:@"Notification triggered"]; 
    [notification setCategory:@"customActions"]; 
    [[UIApplication sharedApplication] scheduleLocalNotification:notification]; 
    } 

日誌打印,但背景顏色不改變。幫助我,我需要在我現有的應用程序中實現此功能。

+1

您是否找到任何解決方法? – 2015-04-09 09:45:01

+1

我有同樣的問題,不知道爲什麼它不顯示動作按鈕 – 2015-06-18 09:45:10

+0

我收到了操作按鈕,但沒有發生點擊事件 – 2015-07-15 09:09:25

回答

0

在迅速增加:

func application(application: UIApplication, handleActionWithIdentifier identifier: String?, forLocalNotification notification: UILocalNotification, completionHandler:() -> Void) { 
    Swift.print(identifier) 
} 

在AppDelegate中。一旦您按下通知中的操作按鈕,就會呼叫

相關問題