如何在没有用户交互的情况下在后台接收iOS通知 [英] How to receive iOS notifications in background without user interaction

查看:139
本文介绍了如何在没有用户交互的情况下在后台接收iOS通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下方法在AppDelegate中接收通知.当用户在前台时,通知将按预期方式接收.但是,如果用户处于后台通知,则将收到(在AppDelegate中)通知 仅当用户点击弹出窗口时.当应用在后台运行时,如何在不涉及用户点击的情况下接收通知.

I have use following methods to receive notifications in AppDelegate. When user is on foreground notification receives as expected. But if user is on background notification will receive(in AppDelegate) notification only if user tapped a popup. How to receive notification without involving user tap when app running on background.

- (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:

If this is really your requirement to execute some method/code on receiving PUSH in background mode you have to implement application:didReceiveRemoteNotification:fetchCompletionHandler:

在文档中进行了说明

要支持此后台模式,请在Xcode项目的功能标签的背景模式部分中启用远程通知选项.

To support this background mode, enable the Remote notifications option from the Background modes section of the Capabilities tab in your Xcode project.

要使推送通知触发某些操作,通知的有效负载必须包含content-available键,其值设置为1.出现该键时,系统会在后台唤醒应用程序(或将其启动到后台),并调用应用程序委托的application:didReceiveRemoteNotification:fetchCompletionHandler:方法.您对该方法的实现应在后台执行所需的提示.

For a push notification to trigger a some operation, the notification’s payload must include the content-available key with its value set to 1. When that key is present, the system wakes the app in the background (or launches it into the background) and calls the app delegate’s application:didReceiveRemoteNotification:fetchCompletionHandler: method. Your implementation of that method should perform your desired tass in background.

也请检查此Doumentation链接

当远程通知到达时,系统将向用户显示通知,并在后台启动应用程序(如果需要),以便可以调用此方法.在后台启动应用程序使您有时间处理通知并下载与之相关的任何数据,从而最大程度地缩短了从通知到达到向用户显示数据之间的时间.

When a remote notification arrives, the system displays the notification to the user and launches the app in the background (if needed) so that it can call this method. Launching your app in the background gives you time to process the notification and download any data associated with it, minimizing the amount of time that elapses between the arrival of the notification and displaying that data to the user.

否则,如果没有用户交互,PUSH通知将无法处理

这篇关于如何在没有用户交互的情况下在后台接收iOS通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆