繁体   English   中英

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

[英]application:didReceiveLocalNotification: not called when app in background

当我的应用程序进入后台时,将自动调用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];
}

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

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

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

以上场景经过测试和验证。

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

试试这段代码:

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM