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

查看:33
本文介绍了如何在没有用户交互的情况下在后台接收 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: 方法.您对该方法的实现应该在后台执行您想要的 tass.

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.

也请查看此文档链接

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

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天全站免登陆