application:didReceiveLocalNotification:在后台app时不调用

时间:2015-06-23 12:34:20

标签: ios objective-c iphone ios7 uilocalnotification

当我的应用程序进入后台时,会自动调用 applicationDidEnterBackground ,在此方法中我们会触发本地通知。但 didReceiveLocalNotification :方法未被调用

- (void)applicationDidEnterBackground:(UIApplication *)application {

UIApplication *app = [UIApplication sharedApplication];
bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
    bgTask = UIBackgroundTaskInvalid;
}];

UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.fireDate = [NSDate date];
localNotification.alertBody = textString;
localNotification.alertAction = @"View";
localNotification.soundName = UILocalNotificationDefaultSoundName;
localNotification.applicationIconBadgeNumber =  1;

[[UIApplication sharedApplication] presentLocalNotificationNow:localNotification];
}

3 个答案:

答案 0 :(得分:5)

您对本地通知的期望不正确:

当您的应用程序进入后台时,您会在手机上看到即时通知,但您必须点击该通知才能触发didReceiveLocalNotification委托。

如果您在前台收到本地通知,则会自动触发didReceiveLocalNotification。

以上方案经过测试和验证。

更新:您必须阅读此文档: http://www.thekspace.com/home/component/content/article/62-uilocalnotification-demystified.html

答案 1 :(得分:2)

试试这段代码:

NSDate *pickerDate = [date_picker date];
// Schedule the notification
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = pickerDate;
localNotification.alertBody = @"Alert Body Message";
localNotification.alertAction = @"Alert Action Message";
localNotification.timeZone = [NSTimeZone defaultTimeZone];
[[UIApplication sharedApplication]     scheduleLocalNotification:localNotification];

并在App委托类的didFinishLaunchingWithOptions中使用此代码。

    UILocalNotification *locationNotification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if (locationNotification) {
    // Set icon badge number to zero
    application.applicationIconBadgeNumber = 0;
}

答案 2 :(得分:0)

在构建通知之前,您必须将其注册为指定类型:

    - (void)applicationDidEnterBackground:(UIApplication *)application {

    UIApplication *app = [UIApplication sharedApplication];
    bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
        bgTask = UIBackgroundTaskInvalid;
    }];

    UILocalNotification* localNotification = [[UILocalNotification alloc] init];
    localNotification.timeZone = [NSTimeZone defaultTimeZone];
    localNotification.fireDate = [NSDate date];
    localNotification.alertBody = textString;
    localNotification.alertAction = @"View";
    localNotification.soundName = UILocalNotificationDefaultSoundName;
    localNotification.applicationIconBadgeNumber =  1;
    // register notification settings
    if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) {
#ifdef __IPHONE_8_0
  UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIRemoteNotificationTypeBadge
      |UIRemoteNotificationTypeSound
      |UIRemoteNotificationTypeAlert) categories:nil];
  [application registerUserNotificationSettings:settings];
#endif
} else {
  UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound;
  [application registerForRemoteNotificationTypes:myTypes];
}

    [[UIApplication sharedApplication] presentLocalNotificationNow:localNotification];
    }