提问人:nirav 提问时间:6/6/2017 最后编辑:nirav 更新时间:8/8/2017 访问量:2179
UNNotificationAction 未显示在远程通知中
UNNotificationAction not showing in remote notification
问:
我正在实施可操作的远程通知,但操作按钮在来时没有显示。notification
有效载荷
{“aps”:{“alert”:“测试.. (11)“,”badge“:1,”sound“:”default“},”category“:”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()
}
答:
3赞
Akram
7/5/2017
#1
我认为如果你转向你的职能,问题就会解决。像这样的东西:UNUserNotificationCenter.current()
.setNotificationCategories([category])
registerForRemoteNotification
if #available(iOS 8.0, *) {
let settings: UIUserNotificationSettings =
UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: Set([category]))
application.registerUserNotificationSettings(settings)
application.registerForRemoteNotifications()
}
评论
1赞
Brian Sachetta
4/8/2022
不完全确定为什么会起作用,但它确实有效。谢谢。
评论
setNotificationCategorys
?当你调用它时,它会取代以前的类别。因此,在注册后,您应该准备好所有类别