gpt4 book ai didi

notifications - iOS10:点击本地通知中的操作不会将应用程序带到前台

转载 作者:行者123 更新时间:2023-12-04 05:30:23 25 4
gpt4 key购买 nike

我正在尝试从通知中执行操作。到目前为止,我能够触发正确的委托(delegate)功能,但在点击应用程序后并没有被带到前台。

相关代码:

@available(iOS 10.0, *)
func registerCategory() -> Void{
print("register category")
let callNow = UNNotificationAction(identifier: "call", title: "Call now", options: [])
let clear = UNNotificationAction(identifier: "clear", title: "Clear", options: [])
let category : UNNotificationCategory = UNNotificationCategory.init(identifier: "IDENT123", actions: [callNow, clear], intentIdentifiers: [], options: [])

let center = UNUserNotificationCenter.currentNotificationCenter()
center.setNotificationCategories([category])
}

@available(iOS 10.0, *)
func scheduleNotification(event : String, interval: NSTimeInterval) {

print("schedule ", event)

let content = UNMutableNotificationContent()

content.title = event
content.body = "body"
content.categoryIdentifier = "CALLINNOTIFICATION"
let trigger = UNTimeIntervalNotificationTrigger.init(timeInterval: interval, repeats: false)
let identifier = "id_"+event
let request = UNNotificationRequest.init(identifier: identifier, content: content, trigger: trigger)

let center = UNUserNotificationCenter.currentNotificationCenter()
center.addNotificationRequest(request) { (error) in

}
}

@available(iOS 10.0, *)
func userNotificationCenter(center: UNUserNotificationCenter, willPresentNotification notification: UNNotification, withCompletionHandler completionHandler: (UNNotificationPresentationOptions) -> Void) {

print("willPresent")
completionHandler([.Badge, .Alert, .Sound])
}

@available(iOS 10.0, *)
func userNotificationCenter(center: UNUserNotificationCenter, didReceiveNotificationResponse response: UNNotificationResponse, withCompletionHandler completionHandler: () -> Void) {

let notification: UNNotification = response.notification
let UUID = notification.request.content.userInfo["UUID"] as! String

switch (response.actionIdentifier) {
case "COMPLETE":

UNUserNotificationCenter.currentNotificationCenter().removeDeliveredNotificationsWithIdentifiers([UUID])

case "CALLIN":

let call = Call()

CalendarController.sharedInstance.fetchMeetingByUUID(UUID, completion: { (thisMeeting) -> Void in
if(!CallIn.Yield(thisMeeting).ConferenceCallNumber.containsString("None")){
call._call(thisMeeting)
}else{
//will open detail view, in case that no number were detected
NSNotificationCenter.defaultCenter().postNotificationName("OpenDetailViewOfMeeting", object: self, userInfo: ["UUID":UUID])
}
})
UNUserNotificationCenter.currentNotificationCenter().removeDeliveredNotificationsWithIdentifiers([UUID])
default: // switch statements must be exhaustive - this condition should never be met
log.error("Error: unexpected notification action identifier: \(UUID)")
}

completionHandler()
}

我可以使用断点来击中委托(delegate)函数 didReceiveNotificationResponse() ,它会执行一些我放在那里的操作,但不是以预期的方式(它必须启动设备调用,而只是关闭通知列表,并且没有任何 react ,但是当我再次手动打开应用程序时,调用开始,好像没有从通知中打开应用程序的权限)。

最佳答案

我自己找到了原因,所以这可能对将来的某人有所帮助。答案很简单。在创建通知的 Action 时,有这个参数:options。当您注册类别时,您需要将其放入 .Foreground 或 .Destructive 中,如下所示:

func reisterCategory () {
let callNow = UNNotificationAction(identifier: NotificationActions.callNow.rawValue, title: "Call now", options: UNNotificationActionOptions.Foreground)
let clear = UNNotificationAction(identifier: NotificationActions.clear.rawValue, title: "Clear", options: UNNotificationActionOptions.Destructive)
let category = UNNotificationCategory.init(identifier: "NOTIFICATION", actions: [callNow, clear], intentIdentifiers: [], options: [])
let center = UNUserNotificationCenter.currentNotificationCenter()
center.setNotificationCategories([category])
}

关于notifications - iOS10:点击本地通知中的操作不会将应用程序带到前台,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39792850/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com