I created a notification with two actions. One of my actions is called "Cancel", while the other is called "Call". How do I make the "Call" action run a URL that is in a comment that I added to my code. Here is my code:
我创建了一个包含两个操作的通知。我的一个动作叫做“取消”,另一个叫做“呼叫”。如何使“调用”操作运行我添加到代码中的注释中的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"])
}
}
1 个解决方案
#1
1
Assuming your notification is working correctly, you can conform to UNUserNotificationCenterDelegate
to handle the "call" action.
假设您的通知正常,您可以符合UNUserNotificationCenterDelegate来处理“呼叫”操作。
Something like:
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)
}
}
}
The body
constant would be set to the phone number that you want to "open".
正常值将设置为您要“打开”的电话号码。
Also, its important to note that this has to be tested on an actual device. Opening a tel
scheme does nothing in the simulator.
此外,重要的是要注意这必须在实际设备上进行测试。打开tel方案在模拟器中什么都不做。
UNUserNotificationCenterDelegate API Reference: https://developer.apple.com/reference/usernotifications/unusernotificationcenterdelegate
UNUserNotificationCenterDelegate API参考:https://developer.apple.com/reference/usernotifications/unusernotificationcenterdelegate
EDIT:
You do not call the delegate method. Instead you implement it. The delegate method is called by the UNUserNotificationCenter
.
您没有调用委托方法。相反,你实现它。委托方法由UNUserNotificationCenter调用。
To get this working, its important to ensure that you set the UNUserNotificationCenter.current()
delegate property to a class that will conform to the UNUserNotificationCenterDelegate
protocol.
为了使其正常工作,确保将UNUserNotificationCenter.current()委托属性设置为符合UNUserNotificationCenterDelegate协议的类非常重要。
For example, if you are handling your notification in your AppDelegate
, you may have something like the following method:
例如,如果您在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)
}
The method above would be responsible for defining your notification and scheduling it. For brevity, I have left out all of the code that would define the notification since you indicated that this is working. Instead you should note that the delegate
property is being set to self
.
上面的方法将负责定义您的通知并安排它。为简洁起见,我已经省略了所有定义通知的代码,因为您表明这是有效的。相反,您应该注意委托属性设置为self。
Then in an extension, you would make the AppDelegate
conform to the UNUserNotificationCenterDelegate
and implement the required methods.
然后在扩展中,您将使AppDelegate符合UNUserNotificationCenterDelegate并实现所需的方法。
extension AppDelegate: 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)
}
}
}
}
Now because your AppDelegate
conforms to the UNUserNotificationCenterDelegate
protocol and you set self
(AppDelegate
) as the UNUserNotificationCenter
's delegate, your implementation of the userNotification(_:didReceive:withCompletionHandler:)
method will be called.
现在,因为您的AppDelegate符合UNUserNotificationCenterDelegate协议并且您将self(AppDelegate)设置为UNUserNotificationCenter的委托,所以将调用userNotification(_:didReceive:withCompletionHandler :)方法的实现。
#1
1
Assuming your notification is working correctly, you can conform to UNUserNotificationCenterDelegate
to handle the "call" action.
假设您的通知正常,您可以符合UNUserNotificationCenterDelegate来处理“呼叫”操作。
Something like:
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)
}
}
}
The body
constant would be set to the phone number that you want to "open".
正常值将设置为您要“打开”的电话号码。
Also, its important to note that this has to be tested on an actual device. Opening a tel
scheme does nothing in the simulator.
此外,重要的是要注意这必须在实际设备上进行测试。打开tel方案在模拟器中什么都不做。
UNUserNotificationCenterDelegate API Reference: https://developer.apple.com/reference/usernotifications/unusernotificationcenterdelegate
UNUserNotificationCenterDelegate API参考:https://developer.apple.com/reference/usernotifications/unusernotificationcenterdelegate
EDIT:
You do not call the delegate method. Instead you implement it. The delegate method is called by the UNUserNotificationCenter
.
您没有调用委托方法。相反,你实现它。委托方法由UNUserNotificationCenter调用。
To get this working, its important to ensure that you set the UNUserNotificationCenter.current()
delegate property to a class that will conform to the UNUserNotificationCenterDelegate
protocol.
为了使其正常工作,确保将UNUserNotificationCenter.current()委托属性设置为符合UNUserNotificationCenterDelegate协议的类非常重要。
For example, if you are handling your notification in your AppDelegate
, you may have something like the following method:
例如,如果您在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)
}
The method above would be responsible for defining your notification and scheduling it. For brevity, I have left out all of the code that would define the notification since you indicated that this is working. Instead you should note that the delegate
property is being set to self
.
上面的方法将负责定义您的通知并安排它。为简洁起见,我已经省略了所有定义通知的代码,因为您表明这是有效的。相反,您应该注意委托属性设置为self。
Then in an extension, you would make the AppDelegate
conform to the UNUserNotificationCenterDelegate
and implement the required methods.
然后在扩展中,您将使AppDelegate符合UNUserNotificationCenterDelegate并实现所需的方法。
extension AppDelegate: 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)
}
}
}
}
Now because your AppDelegate
conforms to the UNUserNotificationCenterDelegate
protocol and you set self
(AppDelegate
) as the UNUserNotificationCenter
's delegate, your implementation of the userNotification(_:didReceive:withCompletionHandler:)
method will be called.
现在,因为您的AppDelegate符合UNUserNotificationCenterDelegate协议并且您将self(AppDelegate)设置为UNUserNotificationCenter的委托,所以将调用userNotification(_:didReceive:withCompletionHandler :)方法的实现。