2020-06-29 13:16:19 -07:00
|
|
|
// Copyright 2020 Google LLC
|
2020-02-13 15:52:58 -08:00
|
|
|
//
|
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
|
//
|
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
//
|
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
|
|
import UserNotifications
|
|
|
|
|
import FirebaseMessaging
|
|
|
|
|
|
|
|
|
|
class NotificationService: UNNotificationServiceExtension {
|
|
|
|
|
var contentHandler: ((UNNotificationContent) -> Void)?
|
|
|
|
|
var bestAttemptContent: UNMutableNotificationContent?
|
|
|
|
|
|
2020-04-08 10:48:24 -07:00
|
|
|
override func didReceive(_ request: UNNotificationRequest,
|
|
|
|
|
withContentHandler contentHandler: @escaping (UNNotificationContent)
|
|
|
|
|
-> Void) {
|
2020-02-13 15:52:58 -08:00
|
|
|
self.contentHandler = contentHandler
|
|
|
|
|
bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)
|
|
|
|
|
|
|
|
|
|
if let bestAttemptContent = bestAttemptContent {
|
|
|
|
|
// Modify the notification content here...
|
|
|
|
|
bestAttemptContent.title = "\(bestAttemptContent.title) 👩💻"
|
|
|
|
|
|
|
|
|
|
// Add images
|
2020-04-08 10:48:24 -07:00
|
|
|
Messaging.serviceExtension()
|
|
|
|
|
.populateNotificationContent(bestAttemptContent, withContentHandler: self.contentHandler!)
|
2020-02-13 15:52:58 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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 {
|
|
|
|
|
contentHandler(bestAttemptContent)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|