有人可以看看下面的代码,并告诉我为什么本地通知不触发。我在XCode中运行该应用程序,并使用debug选项模拟了后台抓取,但本地通知不会触发。

- (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);
}

最佳答案

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

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

10-06 03:43