我在我的消息应用程序中使用 OneSignal 来发送通知。我有一个标准的OneSignal 的NotificationServiceExtension。我还需要添加一个NotificationContentExtension 来自定义我的通知的UI。我到底需要定制什么:我需要将通知左侧的应用程序图标替换为向我发送消息的人的头像。我目前看到的唯一方法是使用NotificationContentExtension,但如果有更简单的方法,请告诉我。我不需要定制其他任何东西 - 只需这张图片,但苹果没有给我一个简单的方法来做到这一点。
出了什么问题:当通知出现时 - 我在通知中心看到标准的推送 UI。当我长按时,我会看到我的自定义用户界面(顺便说一句,不是很快,只是过了一会儿)。我需要的是从一开始就只看到我的自定义用户界面。我认为 OneSignal 不太可能与此有关,但无论如何我不能没有它就尝试。
我创建了我的应用程序的基本版本 - 仅包含 noti 设置,这里是其所有代码,没有 api 密钥
import SwiftUI
import OneSignalFramework
@main
struct qqqqApp: App {
@UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
struct ContentView: View {
var body: some View {
VStack {
Image(systemName: "globe")
.imageScale(.large)
.foregroundStyle(.tint)
Text("Hello, world!")
}
.padding()
}
}
class AppDelegate: NSObject, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
if #available(iOS 10.0, *) {
let options: UNAuthorizationOptions = [.alert]
UNUserNotificationCenter.current().requestAuthorization(options: options) { (authorized, error) in
if authorized {
let categoryIdentifier = "OSNotificationCarousel"
let carouselNext = UNNotificationAction(identifier: "OSNotificationCarousel.next", title: "👉", options: [])
let carouselPrevious = UNNotificationAction(identifier: "OSNotificationCarousel.previous", title: "👈", options: [])
let carouselCategory = UNNotificationCategory(identifier: categoryIdentifier, actions: [carouselNext, carouselPrevious], intentIdentifiers: [], options: [])
UNUserNotificationCenter.current().setNotificationCategories([carouselCategory])
}
}
}
// Remove this method to stop OneSignal Debugging
OneSignal.Debug.setLogLevel(.LL_VERBOSE)
// OneSignal initialization
OneSignal.initialize("API_KEY", withLaunchOptions: launchOptions)
// requestPermission will show the native iOS notification permission prompt.
// We recommend removing the following code and instead using an In-App Message to prompt for notification permission
OneSignal.Notifications.requestPermission({ accepted in
print("User accepted notifications: \(accepted)")
}, fallbackToSettings: true)
return true
}
}
import UserNotifications
import OneSignalExtension
class NotificationService: UNNotificationServiceExtension {
var contentHandler: ((UNNotificationContent) -> Void)?
var receivedRequest: UNNotificationRequest!
var bestAttemptContent: UNMutableNotificationContent?
override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
self.receivedRequest = request
self.contentHandler = contentHandler
self.bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)
if let bestAttemptContent = bestAttemptContent {
/* DEBUGGING: Uncomment the 2 lines below to check this extension is executing
Note, this extension only runs when mutable-content is set
Setting an attachment or action buttons automatically adds this */
// print("Running NotificationServiceExtension")
// bestAttemptContent.body = "[Modified] " + bestAttemptContent.body
OneSignalExtension.didReceiveNotificationExtensionRequest(self.receivedRequest, with: bestAttemptContent, withContentHandler: self.contentHandler)
}
}
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 {
OneSignalExtension.serviceExtensionTimeWillExpireRequest(self.receivedRequest, with: self.bestAttemptContent)
contentHandler(bestAttemptContent)
}
}
}
import UIKit
import UserNotifications
import UserNotificationsUI
class NotificationViewController: UIViewController, UNNotificationContentExtension {
@IBOutlet var titleLabel: UILabel?
@IBOutlet var subtitle: UILabel?
override func viewDidLoad() {
super.viewDidLoad()
// Do any required interface initialization here.
}
func didReceive(_ notification: UNNotification) {
self.titleLabel?.text = notification.request.content.title
self.subtitle?.text = notification.request.content.body
}
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>NSExtension</key>
<dict>
<key>NSExtensionAttributes</key>
<dict>
<key>UNNotificationExtensionDefaultContentHidden</key>
<true/>
<key>UNNotificationExtensionCategory</key>
<string>OSNotificationCarousel</string>
<key>UNNotificationExtensionInitialContentSizeRatio</key>
<real>0.3</real>
</dict>
<key>NSExtensionMainStoryboard</key>
<string>MainInterface</string>
<key>NSExtensionPointIdentifier</key>
<string>com.apple.usernotifications.content-extension</string>
</dict>
</dict>
</plist>
您可以使用 Intents 更改通知图标。这称为通信通知。您不需要使用NotificationContentExtension。
有官方文档可以参考实现。 https://developer.apple.com/documentation/usernotifications/implementing-communication-notifications
您也可以参考以下链接;问题中有 OneSignal 的快速实现。 iOS 通讯通知图标