//iOS 10 Notificationif #available(iOS 10.0,*) { var displayDate: String { let dateFormatter = DateFormatter() dateFormatter.dateStyle = DateFormatter.Style.full return dateFormatter.string(from: datePicker.date as Date) } let notif = UNMutableNotificationContent() notif.Title = "I am a Reminder" notif.subTitle = "\(displayDate)" notif.body = "Here's the body of the notification" notif.sound = UNNotificationSound.default() notif.categoryIDentifIEr = "reminderNotification" let today = NSDate() let interval = datePicker.date.timeIntervalSince(today as Date) let notifTrigger = UNTimeIntervalNotificationTrigger(timeInterval: interval,repeats: false) let request = UNNotificationRequest(IDentifIEr: "reminderNotif",content: notif,trigger: notifTrigger) UNUserNotificationCenter.current().add(request,withCompletionHandler: { error in if error != nil { print(error) // completion(Success: false) } else { //completion(Sucess: true) } })}
我已经在appDelegate中请求了权限,并且使用通知扩展程序的自定义视图显示通知正常.
我在appDelegate中为通知类别添加了通知 *** 作;这些也出现了.
//Notifications Actions private func configureUserNotifications() { if #available(iOS 10.0,*) { let tomorrowAction = UNNotificationAction(IDentifIEr: "tomorrowReminder",Title: "Remind Me Tomorrow",options: []) let dismissAction = UNNotificationAction(IDentifIEr: "dismissReminder",Title: "dismiss",options: []) let category = UNNotificationcategory(IDentifIEr: "reminderNotification",actions: [tomorrowAction,dismissAction],intentIDentifIErs: [],options: [.customdismissAction]) UNUserNotificationCenter.current().setNotificationCategorIEs([category]) } else { // Fallback on earlIEr versions }}
我在通知扩展名.pList文件中设置了相同的类别.在通知扩展中,我有以下内容在用户点击 *** 作时更改文本.
//Handle Notification Actions And Update Notification Window private func dIDReceive(_ response: UNNotificationResponse,completionHandler done: (UNNotificationContentExtensionResponSEOption) -> VoID) { if response.actionIDentifIEr == "tomorrowReminder" { print("Tomrrow button pressed") subLabel.text = "Reminder For Tomorrow" subLabel.textcolor = UIcolor.blue done(.dismissAndForwardAction) } if response.actionIDentifIEr == "dismissReminder" { print("dismiss button pressed") done(.dismiss) } else { print("Else response") done(.dismissAndForwardAction) }}
但是,文本没有改变,也没有调用任何语句;
在appDelegate中我有以下内容;
func application(_ application: UIApplication,dIDFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool { if #available(iOS 10.0,*) { UNUserNotificationCenter.current().delegate = self configureUserNotifications() }}extension AppDelegate: UNUserNotificationCenterDelegate {@available(iOS 10.0,*)private func userNotificationCenter(_ center: UNUserNotificationCenter,willPresent notification: UNNotification,withCompletionHandler completionHandler: (UNNotificationPresentationoptions) -> VoID) { completionHandler([.alert,.sound])}@available(iOS 10.0,dIDReceive response: UNNotificationResponse,withCompletionHandler completionHandler: () -> VoID) { print("RecIEved Action For \(response.actionIDentifIEr)") if response.actionIDentifIEr == "tomorrowReminder" { print("Tomorrow Reminder") //Set new reminder for tomorrow using the notification content Title completionHandler() } if response.actionIDentifIEr == "dismissReminder" { print("dismiss Reminder...") completionHandler() }}}
这些函数都不是在appDelegate中实际调用的.我不确定更新扩展视图的问题是否与应用程序委托有关.我不这么认为,我已经关注了Apple的WWDC视频以及其他教程并查看了文档API并且无法弄清楚;
>为什么通知扩展文本标签不更新?
>为什么appDelegate中的函数没有被调用?
>我如何使用app delegate中的通知内容来使用
为了行动?
PS:过去几周我一直在研究并试图解决这个问题,看起来相当直接,我不确定我错过了什么.我知道我不是唯一有这些问题的人.
解决方法 我没有检查你的整个代码,但至少,这些函数头需要改变如下:func userNotificationCenter(_ center: UNUserNotificationCenter,withCompletionHandler completionHandler: @escaPing (UNNotificationPresentationoptions) -> VoID) {func userNotificationCenter(_ center: UNUserNotificationCenter,withCompletionHandler completionHandler: @escaPing () -> VoID) {func dIDReceive(_ response: UNNotificationResponse,completionHandler done: @escaPing (UNNotificationContentExtensionResponSEOption) -> VoID) {
简单的规则:
删除私有,添加@escaPing.
您可能从Xcode收到了错误的建议,但是如果将它们设为私有,则不会生成Objective-C入口点. iOS运行时在内部使用Objective-C选择器,因此无法找到您的方法,因此它们不会被执行.
总结以上是内存溢出为你收集整理的如何处理iOS 10中的UserNotifications *** 作全部内容,希望文章能够帮你解决如何处理iOS 10中的UserNotifications *** 作所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)