iOS   发布时间:2022-05-04  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了swift – 如何创建URL按下通知操作时运行?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

概述

我创建了一个包含两个操作的通知.我的一个动作叫做“取消”,而另一个叫做“呼叫”.如何使“调用”操作运行我添加到代码中的注释中的URl.这是我的代码: func notificationFires(){ /** URL Code: if let url = URL(String: "tel://") { UIApplication.shared.open(u
@H_673_14@
@H_673_14@
我创建了一个包含两个操作的通知.我的一个动作叫做“取消”,而另一个叫做“呼叫”.如何使“调用”操作运行我添加代码中的注释中的URl.这是我的代码

func notificationFires(){

    /**
     URL Code:

     if let url = URL(String: "tel://") {
     UIApplication.shared.open(url,options: [:])
     }


**/

    let call = UNNotificationAction(identifier:"call",title:"Call",options:[.foreground])
    let cancel = UNNotificationAction(identifier: "cancel",title: "Cancel",options: [.destructive ])
    let category = UNNotificationCategory(identifier: "category",actions: [call,cancel],intentIdentifiers: [],options: [])
    UNUserNotificationCenter.current().setNotificationCategories([category])

    let notification = UIlocalnotification()
    notification.category = "category"
    // 2

    notification.soundName = UIlocalnotificationDefaultSoundName
    notification.fireDate = datePicker.date

    // 3
    if textField.text == "" {

        notification.alertBody = "You have a call right Now!"

    }
    else{

        notification.alertBody = self.textField.text

    }
    // 4
    notification.timeZone = NSTimeZone.default
    // 5
    // 6
    notification.applicationIconBadgenumber = 1
    // 7

    func application(application: UIApplication!,handleActionWithIdentifier identifier:string!,forlocalnotification notification:UIlocalnotification!,completionHandler: (() -> Void)!){

        if (identifier == "call"){
            if let url = URL(String: "tel://2162964785") {
                UIApplication.shared.open(url,options: [:])
            }
        }else if (identifier == "cancel"){

        }

    }

    UIApplication.shared.schedulelocalnotification(notification)



    func application(application: UIApplication,didReceivelocalnotification userInfo: [NSObject : AnyObject],fetchCompletionHandler completionHandler: (UIBACkgroundFetchResult) -> Void) {
        print("Recieved: notification")



        let center = UNUserNotificationCenter.current()
        center.removeDeliveredNotifications(withIdentifiers: ["notification"])




    }

}

解决方法

假设您的通知正常,您可以符合UNUserNotificationCenterDelegate来处理“调用”操作.

就像是:

func userNotificationCenter(_ center: UNUserNotificationCenter,didReceive response: UNNotificationResponse,withCompletionHandler completionHandler: @escaping () -> Void) {
        if response.actionIdentifier == "call" {
            let body = response.notification.request.content.body

            if let url = URL(String: "tel://\(body)") {
                UIApplication.shared.open(url,options: [:],completionHandler: nil)
            }
        }
    }

主体常量将设置为您要“打开”的电话号码.

此外,重要的是要注意这必须在实际设备上进行测试.打开tel方案在模拟器中什么都不做.

UNUserNotificationCenterDelegate API参https://developer.apple.com/reference/usernotifications/unusernotificationcenterdelegate

编辑:

您不需要调用委托方法.相反,你实现它.委托方法由UNUserNotificationCenter调用.

为了使其正常工作,确保将uNUserNotificationCenter.current()委托属性设置为符合UNUserNotificationCenterDelegate协议的类非常重要.

例如,如果您在AppDelegate中处理通知,则可能具有以下方法

func callNotification() {
    let center = UNUserNotificationCenter.center()

    // TODO: - Create your actions,category,content,trigger and request...

    center.delegate = self // Important!

    center.removeAllPendingNotificationrequests()
    center.add(request,withCompletionHandler: nil)
}

上面的方法将负责定义您的通知并安排它.为简洁起见,我已经省略了所有定义通知代码,因为您表明这是有效的.相反,您应该注意委托属性设置为self.

然后在扩展中,您将使AppDelegate符合UNUserNotificationCenterDelegate并实现所需的方法.

extension AppDelegate: UNUserNotificationCenterDelegate {
    func userNotificationCenter(_ center: UNUserNotificationCenter,completionHandler: nil)
            }
        }
    }
}

现在,因为您的AppDelegate符合UNUserNotificationCenterDelegate协议并且您将self(AppDelegatE)设置为UNUserNotificationCenter的委托,所以将调用userNotification(_:didReceive:withCompletionHandler :)方法的实现.

@H_673_14@

大佬总结

以上是大佬教程为你收集整理的swift – 如何创建URL按下通知操作时运行?全部内容,希望文章能够帮你解决swift – 如何创建URL按下通知操作时运行?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签:swift创建如何按下操作运行通知
猜你在找的iOS相关文章
其他相关热搜词更多
phpJavaPython程序员load如何string使用参数jquery开发安装listlinuxiosandroid工具javascriptcap