Files
OrderScheduling/OrderSchedulingNotificationService/NotificationService.swift
DDIsFriend c52d0edc21 update
2023-08-28 14:11:06 +08:00

53 lines
2.1 KiB
Swift

//
// NotificationService.swift
// OrderSchedulingNotificationService
//
// Created by on 2023/8/28.
//
import UserNotifications
import AVFoundation
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)
if let bestAttemptContent = bestAttemptContent {
// Modify the notification content here...
// bestAttemptContent.title = "\(bestAttemptContent.title) [modified]"
let userInfo = bestAttemptContent.userInfo
let aps = userInfo["aps"] as? [String:Any]
if let aps, (aps["alert"] as? String) == "新订单" {
let juheVehcileName = userInfo["juheVehcileName"] as? String
if juheVehcileName?.isEmpty == false {
bestAttemptContent.sound = UNNotificationSound(named: UNNotificationSoundName(rawValue: "juheNewOrderAudio.wav"))
}else{
bestAttemptContent.sound = UNNotificationSound(named: UNNotificationSoundName(rawValue: "newOrderAudio.wav"))
}
}
JPushNotificationExtensionService.jpushSetAppkey("259b546bb2aaf5f02ffa2547")
JPushNotificationExtensionService.jpushReceive(request) {
contentHandler(request.content)
}
contentHandler(bestAttemptContent)
}
}
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)
}
}
}