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

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

问题描述

是否可以接收静默推送通知(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:


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


  • 了解您的应用程序何时启动到后台


    支持后台执行的应用程序可能会被系统
    重新启动以处理传入的事件。 如果某个应用因任何原因被终止
    而非用户强制退出,系统会在发生以下任一事件时启动应用

    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类在
      后台下载内容的应用程序,与
      关联的所有任务会话对象成功完成或收到
      错误。由报亭应用程序启动的下载完成。在大多数
      的情况下,系统不会在用户强制退出
      后重新启动应用程序。

    • For location apps: The system receives a location update that meets the app’s configured criteria for delivery. The device entered or exited a registered region. (Regions can be geographic regions or iBeacon regions.)
    • For audio apps, the audio framework needs the app to process some data. (Audio apps include those that play audio or use the microphone.)
    • For Bluetooth apps: An app acting in the central role receives data from a connected peripheral. An app acting in the peripheral role receives commands from a connected central.
    • For background download apps: A push notification arrives for an app and the payload of the notification contains the content-available key with a value of 1. The system wakes the app at opportunistic moments to begin downloading new content.
    • For apps downloading content in the background using the NSURLSession class, all tasks associated with that session object either completed successfully or received an error. A download initiated by a Newsstand app finishes. In most cases, the system does not relaunch apps after they are force quit by the user.

    一个例外是位置应用,在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.

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

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