当前位置: 首页 > 知识库问答 >
问题:

如何在iOS10中处理用户通知操作

湛宜春
2023-03-14

所以我可以安排这样的通知;

//iOS 10 Notification
if #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)
        }
        })
}

我已在appServer ate中请求权限,并且通知在我使用通知扩展的自定义视图中显示良好。

我已经在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
    }
}

我在通知扩展< code >中设置了相同的类别。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)
    }

}

但是,文本没有改变,也没有调用任何语句;

在应用程序中,我有以下内容;

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, *)
private func userNotificationCenter(_ center: UNUserNotificationCenter, 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 中的函数没有被调用?
  • 如何使用应用程序委派中的通知内容来执行操作?

PS:在过去的几周里,我一直在研究并试图弄明白这一点,这似乎相当直截了当,我不确定我遗漏了什么。我知道我不是唯一一个有这些问题的人。

共有1个答案

盛城
2023-03-14

我没有检查你的整个代码,但至少,这些函数头需要改变如下:

func userNotificationCenter(_ center: UNUserNotificationCenter,
                            willPresent notification: UNNotification,
                            withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {

func userNotificationCenter(_ center: UNUserNotificationCenter,
                            didReceive response: UNNotificationResponse,
                            withCompletionHandler completionHandler: @escaping () -> Void) {

func didReceive(_ response: UNNotificationResponse,
                completionHandler done: @escaping (UNNotificationContentExtensionResponseOption) -> Void) {

一个简单的规则是:删除私有,添加@转义

您可能从Xcode收到了错误的建议,但是如果将它们设为< code>private,Objective-C入口点就不会生成。iOS运行时在内部使用Objective-C选择器,所以它找不到你的方法,因此它们不会被执行。

 类似资料:
  • 如果这被认为是一个可接受的实践,我需要什么-如果有-错误处理?我的理解是,task.wait()将重新抛出异步操作抛出的任何异常,并且我没有提供任何取消异步操作的机制。仅仅调用task.wait()就足够了吗?

  • 问题内容: 如何禁用/取消已设置的通知? 这是我的日程安排功能。 问题答案: 要取消所有待处理的通知,可以使用以下方法: 要取消特定通知,

  • 有多个例子,您应该如何设置您的项目添加丰富的通知,使用‘媒体附件’技术显示图像。我读过其中的大部分,但有些东西我错过了,因为我的项目没有显示任何带有该有效负载的丰富通知(用APNS-Tool和Boodle测试): 通知是可见的,但在3D Touch上,没有额外的信息显示(如图像或修改的标题)。通知扩展断点和消息也不起作用。 下面是我的演示项目:https://github.com/gklka/ri

  • 本文向大家介绍iOS10推送通知开发教程,包括了iOS10推送通知开发教程的使用技巧和注意事项,需要的朋友参考一下 虽然通知经常被过度使用,但是通知确实是一种获得用户关注和通知他们需要更新或行动的有效方式。iOS 10有了新的通知,如新消息、商业信息和时间表的变化。在本教程中,我将向你展示如何使用通知在你的iOS应用程序,并且显示iOS 10引入了新特性。开发iOS 10推送通知你需要最新版本的X

  • 3错误: 1.intent intent=新的intent(this,programslist2.class); 2.PendingIntent pi=PendingIntent.getActivity(this,0,intent,0);