iOS 10静音推送通知不会触发后台应用程序

Ash*_*lls 14 apple-push-notifications ios10

我有一个应用程序,我正在尝试接收和处理SILENT推送通知.

我正在注册APN如下:

UNUserNotificationCenter.currentNotificationCenter().delegate = self
UNUserNotificationCenter.currentNotificationCenter().requestAuthorizationWithOptions([.Badge, .Sound, .Alert]) {(granted, error) in
    if granted {
        application.registerForRemoteNotifications()
    }
}
Run Code Online (Sandbox Code Playgroud)

实现:

func userNotificationCenter(center: UNUserNotificationCenter, didReceiveNotificationResponse response: UNNotificationResponse, withCompletionHandler completionHandler: () -> Void) {
    // Handle notification
}

func userNotificationCenter(center: UNUserNotificationCenter, willPresentNotification notification: UNNotification, withCompletionHandler completionHandler: (UNNotificationPresentationOptions) -> Void) {
    // Handle notification
}
Run Code Online (Sandbox Code Playgroud)

该应用程序有UIBackgroundModes fetchremote-notification

APN内容如下:

{
    "aps":{
        "content-available":1,
        "badge":0
    },
    "action":"shareDelete",
    "shareId":633
}
Run Code Online (Sandbox Code Playgroud)

当应用程序位于前台时,userNotificationCenter(centre:withCompletionHandler:willPresentNotification)会在收到通知时触发,但是当应用程序背景化时,没有任何反应.

我可以通过更改徽章编号看到收到的APN,但在应用程序中没有触发任何内容.

我错过了什么?

(也在Apple开发论坛上询问)

Ogr*_*amp 5

方法

func userNotificationCenter(center: UNUserNotificationCenter, didReceiveNotificationResponse response: UNNotificationResponse, withCompletionHandler completionHandler: () -> Void) {
    // Handle notification
}
Run Code Online (Sandbox Code Playgroud)

当用户点击通知或在 Apple Watch 上查看通知时调用:

调用以让您的应用知道用户为给定通知选择了哪个操作。

您需要实现旧方法

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {

}
Run Code Online (Sandbox Code Playgroud)

  • 这让苹果感到困惑。他们替换了大部分以前的框架,但仍然决定保留最后一个旧的委托方法。我想重点是当有用户交互时...... (2认同)

mck*_*nut -1

你在设置吗minimumBackgroundFetchInterval?正如文档中提到的:

启动另一个后台提取之前必须经过的最短秒数。该值仅供参考,并不表示提取操作之间预期的确切时间量。

应用程序的默认获取间隔是 UIApplicationBackgroundFetchIntervalNever。因此,您必须在应用程序获得后台执行时间之前调用此方法并设置获取间隔。

因此,如果您没有设置此项,则很可能您的提取处理程序永远不会在后台被调用。