本地通知不会触发后台获取

时间:2016-04-01 12:25:52

标签: ios objective-c push-notification

有人可以看看下面的代码并告诉我为什么本地通知没有解雇。我在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);
}

2 个答案:

答案 0 :(得分:3)

您是否询问用户是否允许推送?

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

答案 1 :(得分:1)

使用以下代码检查 RespondToSelector:

,而不是检查设备版本
if ([UIApplication instancesRespondToSelector:@selector(registerUserNotificationSettings:)])
{
    [application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil]];
}