如何在没有用户交互的情况下在后台接收iOS通知

我使用以下方法在AppDelegate接收通知。 当用户在前台通知收到如预期的。 但是,如果用户在后台通知,只有当用户点击一个popup窗口时才会收到(在AppDelegate中)通知。 当应用程序在后台运行时如何接收通知,而不涉及用户点击。

 - (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler { NSLog( @"Handle push from foreground" ); // custom code to handle push while app is in the foreground [[ACBRemoteNotificationManger sharedManager] addNotificationFromUNNotification:notification]; } - (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler { NSLog( @"Handle push from background or closed" ); // if you set a member variable in didReceiveRemoteNotification, you will know if this is from closed or background NSLog(@"%@", response.notification.request.content.userInfo); } 

如果这实际上是您的要求在后台模式下执行一些方法/代码接收PUSH你必须实现application:didReceiveRemoteNotification:fetchCompletionHandler:

这是在文档中说明

要支持这种背景模式,请从Xcode项目的Capabilities选项卡的Background modes部分启用Remote notifications选项。

对于触发某个操作的推送通知,通知的有效content-available必须包含content-available键,其值设置为1 。 当该键存在时,系统在后台唤醒应用程序(或将其启动到后台),并调用应用程序委托的application:didReceiveRemoteNotification:fetchCompletionHandler:方法。 你的实现该方法应该在后台执行你想要的tass。

也检查这个Doumentation Link

当远程通知到达时,系统会向用户显示通知,并在后台启动应用程序(如果需要),以便可以调用此方法。 在后台启动您的应用程序可让您有时间处理通知并下载与其关联的所有数据,从而最大限度地减less通知到达之间的时间,并将数据显示给用户。

否则在没有用户交互的情况下不能处理推送通知