WIP add ios custom notification example

This commit is contained in:
Salvatore Giordano
2020-03-25 13:31:57 +01:00
parent 4356b4f0ea
commit 1de4fa37a8
7 changed files with 293 additions and 4 deletions
@@ -0,0 +1,36 @@
//
// NotificationService.swift
// Notifications
//
// Created by Salvatore Giordano on 25/03/2020.
// Copyright © 2020 The Chromium Authors. All rights reserved.
//
import UserNotifications
class NotificationService: UNNotificationServiceExtension {
var contentHandler: ((UNNotificationContent) -> Void)?
var bestAttemptContent: UNMutableNotificationContent?
override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
self.contentHandler = contentHandler
bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)
defer {
contentHandler(bestAttemptContent ?? request.content)
}
print("DID RECEIVE NOTIFICATION")
// Modify the notification content here...
bestAttemptContent?.title = "\(bestAttemptContent?.title ?? "<NoContent>") [modified]"
}
override func serviceExtensionTimeWillExpire() {
// Called just before the extension will be terminated by the system.
// Use this as an opportunity to deliver your "best attempt" at modified content, otherwise the original push payload will be used.
if let contentHandler = contentHandler, let bestAttemptContent = bestAttemptContent {
bestAttemptContent.title = "\(bestAttemptContent.title) [modified]"
contentHandler(bestAttemptContent)
}
}
}