静默推送通知无法唤醒应用

时间:2019-11-07 05:46:51

标签: swift push-notification background-mode silent-notification

测试环境:

  • XCode 10.2
  • 迅速4.2

测试前:

  • 确保已创建并正确设置了配置文件。

  • 启用背景模式(包括远程通知)和项目中的推送通知

测试步骤:

  1. 从Mac将电缆连接到移动设备,让应用程序处于后台模式。

  2. 通过Easy APNs Provider工具发送APN,将content-available设置为1

// This is payload content:
{
  "aps" : {
    "alert" : "hello",
    "sound" : "default",
    "content-available" : 1
  }
}
  1. 设备收到通知,首先从APN服务器弹出“你好”通知,然后隐式唤醒应用程序,然后从应用程序弹出“内部”通知。

当移动设备连接到Mac时,此行为是正常的,但是当我将移动设备与Mac断开连接时,它不再弹出“内部”通知。

这是用于无声远程推送通知的简单测试代码:

//
// ViewController.swift
//

import UIKit
import Foundation
import UserNotifications

class ViewController: UIViewController, UIPickerViewDelegate {

    @IBAction func createNotification(_ sender: AnyObject) {

        let content = UNMutableNotificationContent()
        content.title = "TITLE"
        content.subtitle = "SUBTITLE"
        content.body = "Inside"
        content.badge = 1
        content.sound = UNNotificationSound.default

        let request = UNNotificationRequest(identifier: "notification1", content: content, trigger: nil)

        UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        // When launching the app into foreground mode, pop up notification every two seconds
        let _ = Timer.scheduledTimer(timeInterval: 2.0, target: self, selector: #selector(showMsg), userInfo: nil, repeats: true)
    }

    @objc func showMsg() {
        createNotification(UIButton())
    }
}

//
//  AppDelegate.swift
//  

import UIKit
import UserNotifications

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

        UNUserNotificationCenter.current().delegate = self

        UNUserNotificationCenter.current().requestAuthorization(options: [.alert,.sound,.badge], completionHandler: { (granted, error) in
            if granted { print("granted") } 
            else { print("denied") }
        })

        // register remote push notification
        application.registerForRemoteNotifications()

        return true
    }


    func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        let deviceTokenString = deviceToken.reduce("", {$0 + String(format: "%02X", $1)})
        print("deviceTokenString: \(deviceTokenString)")
    }

    func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
        print("error: \(error)")
    }

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

}

extension AppDelegate: UNUserNotificationCenterDelegate {

    // Show push notifications in the foreground
    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
        completionHandler([.alert, .badge, .sound])
    }

}

0 个答案:

没有答案