我已成功为iOS 10.2实施本地通知。
但问题是只有在应用程序处于后台时才会发出通知警报如果应用程序是前台应用程序则不会发出警报。
是否可以在前台获得本地通知?
我的代码在这里
func notificationNow(){
print("notification will be triggered in five seconds..Hold on tight")
let content = UNMutableNotificationContent()
content.title = "Intro to Notifications"
content.subtitle = "Lets code,Talk is cheap"
content.body = "Sample code from WWDC"
content.sound = UNNotificationSound.default()
//To Present image in notification
if let path = Bundle.main.path(forResource: "menu2", ofType: "png") {
let url = URL(fileURLWithPath: path)
do {
let attachment = try UNNotificationAttachment(identifier: "sampleImage", url: url, options: nil)
content.attachments = [attachment]
} catch {
print("attachment not found.")
}
}
// Deliver the notification in five seconds.
let trigger = UNTimeIntervalNotificationTrigger.init(timeInterval: 5.0, repeats: false)
let request = UNNotificationRequest(identifier:requestIdentifier, content: content, trigger: trigger)
UNUserNotificationCenter.current().delegate = self
UNUserNotificationCenter.current().add(request){(error) in
if (error != nil){
print(error?.localizedDescription as Any)
}
}
}
答案 0 :(得分:1)
在UNUserNotificationCenterDelegate的文档中:
重要强>
您必须在应用程序完成启动之前将您的委托对象分配给UNUserNotificationCenter对象。例如,在iOS应用程序中,您必须在应用程序(:willFinishLaunchingWithOptions :)或应用程序(:didFinishLaunchingWithOptions :)方法中分配它。
您似乎要稍晚设置代理 - 就在通知添加到通知中心之前。
我创建了一个简单的Swift类单例,其扩展名符合UNUserNotificationCenterDelegate。在单例的init方法中,我将委托分配给self。然后我在AppDelegate的willFinishLaunchingWithOptions方法中初始化单例。
答案 1 :(得分:0)
当应用程序在Foreground中运行时。您需要通过委托方法捕获本地通知。
因此,在AppDelegate
中,didFinishLaunchingWithOptions
方法实现了对委托的监听:
在应用完成启动之前设置代理非常重要。
// Do NOT forget to retain your delegate somewhere
let notificationDelegate = UYLNotificationDelegate()
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
let center = UNUserNotificationCenter.current()
center.delegate = notificationDelegate
// ...
return true
}
如果您想在应用程序位于前台时响应可操作通知或接收通知,则需要实施UNUserNotificationCenterDelegate
。该协议定义了两种可选方法:
userNotificationCenter(_:willPresent:withCompletionHandler:)
是
通知发送到前台应用时调用。你
收到包含原件的UNNotification
对象
UNNotificationRequest
。你用完成处理程序调用完成处理程序
您要呈现的UNNotificationPresentationOptions
(使用.none到
忽略警报)。
userNotificationCenter(_:didReceive:withCompletionHandler:)
被调用
当用户在已投放的通知中选择操作时您
收到包含该对象的UNNotificationResponse
对象
用户操作和UNNotification
对象的actionIdentifier。
系统定义了标识符UNNotificationDefaultActionIdentifier
用户点击时会使用UNNotificationDismissActionIdentifier
通知打开应用程序或滑动以解雇
通知。
在这两种情况下,您必须在完成后调用完成处理程序。
#pragma mark - UNUserNotificationCenterDelegate Methods
func userNotificationCenter(_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
// Play sound and show alert to the user
completionHandler([.alert,.sound])
}
答案 2 :(得分:0)
在要观察本地通知的班级中编写以下代码(扩展名)
当您在应用程序处于后台时收到前台通知或用户点击通知时,这将会通知。
希望这能解决您的问题。
extension ViewController:UNUserNotificationCenterDelegate{
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
print("Tapped in notification")
}
//This is key callback to present notification while the app is in foreground
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
print("Notification being triggered")
//You can either present alert ,sound or increase badge while the app is in foreground too with ios 10
//to distinguish between notifications
// if notification.request.identifier == requestIdentifier
// {
completionHandler( [.alert,.sound,.badge])
// }
}
}
答案 3 :(得分:-1)
使用以下方法配置要在前台显示的通知,
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void)
您可以参考Apple documentation了解详情!