当应用程序未运行时,通过静默推送通知在后台更新本地数据库 [英] Update local database in the background through silent push notification when app is not running

查看:15
本文介绍了当应用程序未运行时,通过静默推送通知在后台更新本地数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以接收静默推送通知(content-available":1")并运行函数来更改 NSUserDefaults.standardUserDefaults() 中的数据,而无需用户点击通知(通知中心没有通知,因为它是无声推送通知).应用程序根本没有运行(例如,用户强制退出).我已经尝试了下面的代码,如果应用程序正在运行或在后台运行,它就可以工作,但如果应用程序被强制退出(根本没有运行),我也希望它可以工作.

Is it possible to receive a silent push notification ("content-available": "1") and run a function to change data in NSUserDefaults.standardUserDefaults() without the user having to tap the notification (there is no notification in the notification center, since it's a silent push notification). The app is not running at all (e.g. force-quit by user). I have tried the code below and it worked if the app was running or in the background, but I also want it to work if the app was force-quit (not running at all).

我已启用后台提取和远程通知.

I have enabled background fetch and remote notifications.

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
    handleNotification()
}

//this works, the someData is not a problem
func handleNotification() -> Void{
    let defaults = NSUserDefaults.standardUserDefaults()
    defaults.setValue(someData, forKey: "key")
    defaults.synchronize()
}

当我发送推送通知时,如果应用处于活动状态或在后台,数据会更新,但如果应用根本没有运行,则数据不会更新.

推荐答案

简短回答:当应用程序被强制退出(未运行)时,如果没有用户交互,(几乎)无法执行任何操作.

有关说明,请参阅文档:

For explanation see the documentation:

使用此方法为您的应用处理传入的远程通知.与 application:didReceiveRemoteNotification: 方法不同,它是仅当您的应用程序在前台运行时调用,系统当您的应用程序在前台运行时调用此方法或背景.此外,如果您启用了远程通知后台模式,系统启动您的应用程序(或从挂起状态)并在远程调用时将其置于后台状态通知到达.但是系统不会自动如果用户强制退出,则启动您的应用程序.在这种情况下,用户必须在系统之前重新启动您的应用程序或重新启动设备尝试再次自动启动您的应用.

Use this method to process incoming remote notifications for your app. Unlike the application:didReceiveRemoteNotification: method, which is called only when your app is running in the foreground, the system calls this method when your app is running in the foreground or background. In addition, if you enabled the remote notifications background mode, the system launches your app (or wakes it from the suspended state) and puts it in the background state when a remote notification arrives. However, the system does not automatically launch your app if the user has force-quit it. In that situation, the user must relaunch your app or restart the device before the system attempts to launch your app automatically again.

  • 了解您的应用何时进入后台:

    支持后台执行的应用可能会被系统重新启动处理传入事件.如果应用因任何原因终止除用户强制退出外,系统启动应用当发生以下事件之一时:

    Apps that support background execution may be relaunched by the system to handle incoming events. If an app is terminated for any reason other than the user force quitting it, the system launches the app when one of the following events happens:

    • 对于位置应用:系统收到满足以下条件的位置更新应用程序配置的交付标准.设备进入或退出注册区域.(区域可以是地理区域或iBeacon 区域.)
    • 对于音频应用,音频框架需要应用处理一些数据.(音频应用包括那些播放音频或使用麦克风.)
    • 对于蓝牙应用:起到核心作用的应用从连接的外围设备接收数据.一个应用程序外围角色从连接的中心接收命令.
    • 对于后台下载应用程序:应用程序的推送通知到达并且通知的有效负载包含内容可用密钥值为 1.系统在机会时刻唤醒应用程序开始下载新内容.
    • 对于下载内容的应用程序后台使用 NSURLSession 类,所有与相关的任务该会话对象要么成功完成,要么收到错误.由报亭应用程序启动的下载完成.多数情况在这种情况下,系统在强制退出后不会重新启动应用程序用户.

    定位应用是个例外,在 iOS 8 及更高版本中被用户强制退出后重新启动. 但在其他情况下,用户必须明确启动应用程序或重新启动设备之前该应用程序可以自动启动到后台系统.

    One exception is location apps, which in iOS 8 and later are relaunched after being force quit by the user. In other cases, though, the user must launch the app explicitly or reboot the device before the app can be launched automatically into the background by the system.

  • 如您所见,只有位置应用是例外,但这对您无济于事.只需处理这样一个事实,这是操作系统限制,您对此无能为力.

    As you see, only exception are location apps, but this won't help you. Just deal with the fact, that this is an OS limitation and you can't do anything about it.

    这篇关于当应用程序未运行时,通过静默推送通知在后台更新本地数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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