1
我正在執行可操作的遠程通知,但是notification
出現時不顯示動作按鈕。UNNotificationAction不在遠程通知中顯示
有效載荷
{ 「APS」:{ 「警告」: 「測試.. (11)」, 「徽章」:1, 「聲音」: 「默認」},「類別「:」 newnote「}
我的代碼
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
self.configureUserNotifications()
self.registerForRemoteNotification()
return true
}
func registerForRemoteNotification() {
let center = UNUserNotificationCenter.current()
center.delegate = self
center.requestAuthorization(options: [.sound, .alert, .badge]) { (granted, error) in
if error == nil{
UIApplication.shared.registerForRemoteNotifications()
}
}
func configureUserNotifications() {
let acceptAction = UNNotificationAction(identifier:
"accept", title: "✅ Accept note", options: [])
let rejectAction = UNNotificationAction(identifier:
"reject", title: "❌ Reject note", options: [])
let category =
UNNotificationCategory(identifier: "newnote",
actions: [acceptAction,rejectAction],
intentIdentifiers: [],
options: [])
UNUserNotificationCenter.current()
.setNotificationCategories([category])
}
func application(_ application: UIApplication,
didFailToRegisterForRemoteNotificationsWithError error: Error) {
print("Registration for remote notifications failed")
print(error.localizedDescription)
}
func application(_ application: UIApplication,
didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
print("Registered with device token: \(deviceToken.hexString)")
self.deviceToken = deviceToken.hexString
}
//Called when a notification is delivered to a foreground app.
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
print("User Info = ",notification.request.content.userInfo)
completionHandler([.alert, .badge, .sound])
}
//Called to let your app know which action was selected by the user for a given notification.
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping() -> Void) {
print("User Info = ",response.notification.request.content.userInfo)
print("Response received for \(response.actionIdentifier)")
if response.actionIdentifier == "accept" {
}
else if response.actionIdentifier == "reject" {
}
completionHandler()
}