以前,我使用以下代码来区分应用启动时通知是本地通知还是远程通知
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions:
[UIApplication.LaunchOptionsKey: Any]?) -> Bool {
if (launchOptions?[UIApplication.LaunchOptionsKey.localNotification] != nil)
{
}
if (launchOptions?[UIApplication.LaunchOptionsKey.remoteNotification] != nil)
{
}
}
条件是我的应用程序被杀,我正在从通知中打开它。
问题在于此方法
if (launchOptions?[UIApplication.LaunchOptionsKey.localNotification] != nil)
{
}
从通知中心打开应用程序时,已过时,并且不调用以下方法
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {}
答案 0 :(得分:2)
您也可以在userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler:
中查看通知类型,
类层次结构是:
UNNotificationResponse
>UNNotification
>UNNotificationRequest
>UNNotificationTrigger
UNNotificationRequest
中有四种类型的触发器:
UNLocationNotificationTrigger
UNPushNotificationTrigger
UNTimeIntervalNotificationTrigger
UNCalendarNotificationTrigger
只需使用
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
if response.notification.request.trigger is UNPushNotificationTrigger {
print("remote notification");
}
}
答案 1 :(得分:1)
在创建本地通知时,在通知中设置标识符,可用于识别处理通知中的差异
以下是使用标识符创建本地通知的示例。
let content = UNMutableNotificationContent()
content.title = "Title"
content.body = "Body"
content.sound = UNNotificationSound.default()
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)
let request = UNNotificationRequest(identifier: "TestIdentifier", content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
处理带有标识符的本地通知。
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
if response.notification.request.identifier == "TestIdentifier" {
print("handling notifications with the TestIdentifier Identifier")
}
completionHandler()
}
对于处理远程通知,您可以使用以下行
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
print("handling notification")
if let notification = response.notification.request.content.userInfo as? [String:AnyObject] {
let message = parseRemoteNotification(notification: notification)
print(message as Any)
}
completionHandler()
}
private func parseRemoteNotification(notification:[String:AnyObject]) -> String? {
if let aps = notification["aps"] as? [String:AnyObject] {
let alert = aps["alert"] as? String
return alert
}
return nil
}
您可以通过检查第一个标识符来添加其他条件,以同一方法处理两个通知 行。
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
print("handling notification")
if response.notification.request.identifier == "TestIdentifier" {
print("handling notifications with the TestIdentifier Identifier")
}else {
if let notification = response.notification.request.content.userInfo as? [String:AnyObject] {
let message = parseRemoteNotification(notification: notification)
print(message as Any)
}
}
completionHandler()
}
private func parseRemoteNotification(notification:[String:AnyObject]) -> String? {
if let aps = notification["aps"] as? [String:AnyObject] {
let alert = aps["alert"] as? String
return alert
}
return nil
}
答案 2 :(得分:1)
您可以在content.userInfo中设置本地通知键值。
content.userInfo = ["isMyLocationNotification" : true] //you can set anything
然后签入UNUserNotificationCenterDelegate的didReceive响应方法:
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
print(response.notification.request.content.userInfo) //you can check your notification types
}
在输出部分,您将通过isMyLocationNotification键使用Information数据,现在您可以确定天气通知是远程本地的。