gpt4 book ai didi

xcode - UNUserNotificationCenter 确实收到了完成处理程序的响应,从未被称为 iOS10,swift 2.3

转载 作者:行者123 更新时间:2023-12-03 07:34:54 24 4
gpt4 key购买 nike

我正在 iOS10 中安排新通知,如下所示:

func scheduleNotification (event : Meeting, todaysBadgeCounter: Int) {

if #available(iOS 10.0, *) {

let minutesBefore = 10
//interval in seconds from current point in time to notification
let interval : NSTimeInterval = NSTimeInterval(secondsFromNowTo(event.startTime.dateByAddingTimeInterval(-minutesBefore * 60)))

//only schedule in the future
if(interval > 0){


let category = NotificationsController.notificationCategory

let center = NotificationsController.notificationCenter
center.setNotificationCategories([category])
let content = UNMutableNotificationContent()

content.title = NSString.localizedUserNotificationStringForKey(event.title, arguments: nil)
if(minutesBefore <= 1){
content.body = NSString.localizedUserNotificationStringForKey("IOS10: Your \(event.title) is about to start", arguments: nil)
}else{
content.body = NSString.localizedUserNotificationStringForKey("IOS10: You have \(event.title) in \(Int(minutesBefore)) minutes", arguments: nil)

}
content.sound = UNNotificationSound.defaultSound()

let trigger = UNTimeIntervalNotificationTrigger.init(timeInterval: interval, repeats: false)
let identifier = NSString.localizedUserNotificationStringForKey("sampleRequest\(event.UUID)", arguments: nil)
let request = UNNotificationRequest.init(identifier: identifier, content: content, trigger: trigger)

//setting the delegate
center.delegate = self

center.addNotificationRequest(request, withCompletionHandler: { (error) in
// handle the error if needed
log.error(error?.localizedDescription)
print("SCHEDULING >=iOS10:", event.title, ", interval:", interval)
})
}


//return category
@available(iOS 10.0, *)
class var notificationCategory : UNNotificationCategory {
struct Static {
static let callNow = UNNotificationAction(identifier: NotificationActions.callNow.rawValue, title: "Call now", options: [])
static let clear = UNNotificationAction(identifier: NotificationActions.clear.rawValue, title: "Clear", options: [])
static let category : UNNotificationCategory = UNNotificationCategory.init(identifier: "CALLINNOTIFICATION", actions: [callNow, clear], intentIdentifiers: [], options: [])

}
return Static.category
}

我能够安排通知,并在正确的时间接收本地通知。 但是:我根据教程使用的委托(delegate)方法从未执行,但是每次我点击通知时都会执行 didReceiveLocalNotification:

extension NotificationsController: UNUserNotificationCenterDelegate {

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

print("IOS10 delivered")

// Response has actionIdentifier, userText, Notification (which has Request, which has Trigger and Content)
switch response.actionIdentifier {
case NotificationActions.NotifyBefore.rawValue:
print("notify")
break

case NotificationActions.callNow.rawValue:
print("callNow")
break
case NotificationActions.clear.rawValue:
print("clear")
default: break
}
}

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

// Delivers a notification to an app running in the foreground.
print("IOS10 delivered 2222")

}
}

didReceiveLocalNotification 是否已被弃用?如何让这些方法被调用呢?

更新:

我用这里的一些建议更新了我的代码,即:

  1. 我添加了将 UNUserNotificationCenter.delegate 分配给 applicationDidFinishLaunchingWithOptions。
  2. 我还尝试将这些方法(委托(delegate)方法)从扩展中移出到NotificationsController.swift类,并将此类设置为UNUserNotificationCenterDelegate。对我来说也不起作用。

最佳答案

请求标识符不是通知类别。

只需添加这一行:

content.categoryIdentifier = identifier

更新:刚刚做了一个简单的应用程序。一切似乎都工作正常:

class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {

var window: UIWindow?

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

UNUserNotificationCenter.current().delegate = self

UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound]) { (granted, error) in
if granted {
self.registerCategory()
self.scheduleNotification(event: "test", interval: 3)
self.scheduleNotification(event: "test2", interval: 5)
}
}

return true
}

func registerCategory() -> Void{

let callNow = UNNotificationAction(identifier: "call", title: "Call now", options: [])
let clear = UNNotificationAction(identifier: "clear", title: "Clear", options: [])
let category : UNNotificationCategory = UNNotificationCategory.init(identifier: "CALLINNOTIFICATION", actions: [callNow, clear], intentIdentifiers: [], options: [])

let center = UNUserNotificationCenter.current()
center.setNotificationCategories([category])

}

func scheduleNotification (event : String, interval: TimeInterval) {
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.current()
center.add(request, withCompletionHandler: { (error) in
})

}

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

func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
print("willPresent")
completionHandler([.badge, .alert, .sound])
}

}

更新 2:用 Swift 2.3 重写

class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {

var window: UIWindow?

func applicationDidFinishLaunching(application: UIApplication) {
UNUserNotificationCenter.currentNotificationCenter().delegate = self
UNUserNotificationCenter.currentNotificationCenter().requestAuthorizationWithOptions([.Badge, .Sound, .Alert]) { (granted, error) in
if granted {
self.registerCategory()
self.scheduleNotification("test", interval: 3)
self.scheduleNotification("test2", interval: 5)
}
}
}


func registerCategory() -> Void{

let callNow = UNNotificationAction(identifier: "call", title: "Call now", options: [])
let clear = UNNotificationAction(identifier: "clear", title: "Clear", options: [])
let category : UNNotificationCategory = UNNotificationCategory.init(identifier: "CALLINNOTIFICATION", actions: [callNow, clear], intentIdentifiers: [], options: [])

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

}

func scheduleNotification(event : String, interval: NSTimeInterval) {
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

}
}

func userNotificationCenter(center: UNUserNotificationCenter, willPresentNotification notification: UNNotification, withCompletionHandler completionHandler: (UNNotificationPresentationOptions) -> Void) {

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

func userNotificationCenter(center: UNUserNotificationCenter, didReceiveNotificationResponse response: UNNotificationResponse, withCompletionHandler completionHandler: () -> Void) {

print("didReceive")
completionHandler()
}

}

关于xcode - UNUserNotificationCenter 确实收到了完成处理程序的响应,从未被称为 iOS10,swift 2.3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38954496/

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