2014-10-02 64 views
4

我已創建在此模式下本地通知:在午餐UILocalNotification iOS8上handleActionWithIdentifier從來沒有所謂

AppDelegate.m

if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) { 
     // Setup each action thar will appear in the notification 
     UIMutableUserNotificationAction *acceptAction = [[UIMutableUserNotificationAction alloc] init]; 
     acceptAction.identifier = @"ACCEPT_ACTION"; // Identifier is returned in handleActionWithIdentifier: after the user taps on an action 
     acceptAction.title = @"ACCEPT_TITLE"; 
     acceptAction.activationMode = UIUserNotificationActivationModeForeground; //Brings the app into the foreground when action tapped 

     UIMutableUserNotificationCategory *not_cat = [[UIMutableUserNotificationCategory alloc] init]; 
     not_cat.identifier = @"testCategory"; 

     [not_cat setActions:@[acceptAction] forContext:UIUserNotificationActionContextDefault]; 

     NSSet *categories = [NSSet setWithObjects:not_cat, nil]; 

     UIUserNotificationSettings* notificationSettings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:categories]; 
     [[UIApplication sharedApplication] registerUserNotificationSettings:notificationSettings]; 
     [[UIApplication sharedApplication] registerForRemoteNotifications]; 
    } else { 
     [[UIApplication sharedApplication] registerForRemoteNotificationTypes: (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)]; 
    } 

然後創建一個本地通知:

+ (void)sendLocalNotification:(NotificaObject*)n 
{ 
    UILocalNotification *localNotif = [[UILocalNotification alloc] init]; 

    // Set the category to the category that contains our action 
    localNotif.category = @"testCategory"; 
    localNotif.alertBody = @"First Test mex, no action";//n.titolo; 
    localNotif.soundName = UILocalNotificationDefaultSoundName; 
    localNotif.applicationIconBadgeNumber = 1; 
    localNotif.userInfo = [NSDictionary dictionaryWithObject:@"0" forKey:@"id"]; 
    localNotif.fireDate = [NSDate dateWithTimeIntervalSinceNow:5]; //5 seconds 
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotif]; 
    NSLog(@"Sending Local notification"); 
} 

通知時FIRE如果應用程序(iOS8)在AppDelegate中是FOREGROUND 則稱爲

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification 

而且從不叫

- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification completionHandler:(void (^)())completionHandler 

如果應用程序(iOS 8)是在後臺只調用didReceiveLocalNotification

我想不通爲什麼handleActionWithIdentifier永遠不會調用

我用xcode6 .01和爲ios7/8開發,我做錯了什麼?

+2

當你選擇喜歡點擊按鈕,通知一些動作這將調用...請訪問此鏈接... https://開頭的github的.com/sgup77/SGNotification – 2014-10-14 11:41:50

回答

0

您是否附加了任何dispatch_get_main_queue?

我做主隊列循環和應用:handleActionWithIdentifier:forLocalNotification:completionHandler:方法不會被調用

dispatch_async(dispatch_get_main_queue(), ^{ 
    while (YES) { 
     [NSThread sleepForTimeInterval:1.0]; 
    } 
}); 

的解決方案是使用dispatch_get_global_queue

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 
    while (YES) { 
     [NSThread sleepForTimeInterval:1.0]; 
    } 
}); 
1

多德。看來您正在註冊遠程通知併爲本地通知放置處理程序。請註冊本地通知讓聽衆工作。

0

只需三步爲本地通知:

1.-在AppDelegate中,didFinishLaunchingWithOptions你告訴你的應用程序正在使用通知:

if(UIApplication.instancesRespondToSelector(Selector("registerUserNotificationSettings:"))) 
    { 
     application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: .Sound | .Alert | .Badge, categories: nil)) 
    } 

2:寫出nofications處理程序(這是什麼要happend當本地發生notificion):

func application(application: UIApplication, handleActionWithIdentifier identifier: String?, forLocalNotification notification: UILocalNotification, completionHandler:() -> Void) { 
    println("notification is here. Open alert window or whatever") 
    // Must be called when finished 
    completionHandler() 
} 

3.-在您的應用程序設置本地通知中這樣說:

var localNotification: UILocalNotification = UILocalNotification() 
    localNotification.alertAction = "Your Alarm!" 
    localNotification.alertBody = "Your description" 
    localNotification.fireDate = NSDate(timeIntervalSinceNow: 10) // your time 
    localNotification.soundName = UILocalNotificationDefaultSoundName 
    // there are more and different options 
    UIApplication.sharedApplication().scheduleLocalNotification(localNotification) 

對我來說,它的工作與iOS 8.3和1.2迅速

相關問題