2016-04-01 74 views
0

有人可以看看下面的代碼,並告訴我爲什麼本地通知不會觸發。我在XCode中運行應用程序,並使用調試選項來模擬後臺獲取,但本地通知不會觸發。本地通知不會觸發後臺抓取

- (void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler 
{ 
    NSLog(@"performFetchWithCompletionHandler"); 


UILocalNotification *localNotif = [[UILocalNotification alloc] init]; 

if (localNotif) { 
    localNotif.alertBody = @"Update demo text!"; 
    localNotif.alertAction = @"OK"; 
    localNotif.soundName = UILocalNotificationDefaultSoundName; 
    localNotif.fireDate = nil; 

    NSLog(@"Local Notification"); 

    [[UIApplication sharedApplication] scheduleLocalNotification:localNotif]; 
} 

//Perform some operation 
completionHandler(UIBackgroundFetchResultNewData); 
} 

回答

3

您是否詢問用戶是否允許推送?

if ([UIDevice currentDevice].systemVersion.floatValue >= 8.0) { 
     UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge | UIUserNotificationTypeAlert | UIUserNotificationTypeSound categories:nil]; 
     [application registerUserNotificationSettings:settings]; 
    } 
+0

啊,我不認爲可能是它。 – ORStudios

+0

完美,這是問題所在。謝謝 – ORStudios

1

而不是檢查設備的版本,使用低於檢查RespondToSelector代碼:

if ([UIApplication instancesRespondToSelector:@selector(registerUserNotificationSettings:)]) 
{ 
    [application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil]]; 
}