Is it possible to generate a Silent/Background notification using UNUserNotificationCenter in my NEAppPushProvider subclass?
Not sure about a silent or background notification, but you can send a local push notification that is received in the host app.
Container App:
import UserNotifications
class NetworkManager: NSObject {
// ...
private override init() {
super.init()
UNUserNotificationCenter.current().delegate = self
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound]) { granted, error in
// ....
}
}
}
extension NetworkManager: UNUserNotificationCenterDelegate {
func userNotificationCenter(_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
// ...
completionHandler([.badge, .sound, .banner])
}
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: @escaping () -> Void) {
// ...
completionHandler()
}
}
Provider:
import UserNotifications
class AppPushProvider: NEAppPushProvider {
override init() {
super.init()
}
override func start(completionHandler: @escaping (Error?) -> Void) {
showLocalNotification { error in
// ...
completionHandler(error)
}
}
// ...
func showLocalNotification(completionHandler: @escaping (Error?) -> Void) {
let content = UNMutableNotificationContent()
content.title = "Test Notification"
content.body = "Here is a test notification to verify that the provider is working"
content.sound = .default
content.userInfo = [
"message": "Here is a test notification to verify that the provider is working"
]
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 10, repeats: false)
let request = UNNotificationRequest(identifier: "ProviderNotification", content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request) { error in
if let error = error {
// ...
completionHandler(error)
return
}
// ...
completionHandler(nil)
}
}
}
Matt Eaton
DTS Engineering, CoreOS
meaton3@apple.com