Kau*_*anu 6 apple-push-notifications swift3 ios10 xcode8 xcode8-beta3
如果应用程序未处于活动状态(甚至不在后台或已终止),则在3D Touch之后单击"通知中的操作按钮聊天"时不会调用UNUserNotificationCenter功能.我在Xcode中使用"附加到名称处理"来在应用程序终止时调试应用程序.这是代码:
import UIKit
import Mixpanel
import UserNotifications
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
//setup mixpanel
self.handlePushNotificationWithUserInfo(launchOptions: launchOptions)
//ask for push notification perms
return true
}
Run Code Online (Sandbox Code Playgroud)
当通知弹出窗口(从MixPanel发送)时,首先调用此函数,
致电1:
func handlePushNotificationWithUserInfo(launchOptions: [NSObject: AnyObject]?) {
//Handle PushNotification when app is opened
}
Run Code Online (Sandbox Code Playgroud)
然后它就在这里,
电话2:
//register for notification
func application(_ application: UIApplication, didRegister notificationSettings: UIUserNotificationSettings) {
if #available(iOS 10.0, *) {
let center = UNUserNotificationCenter.current()
center.requestAuthorization(options: [.alert, .sound]) { (granted, error) in
// Enable or disable features based on authorization.
}
center.delegate = self
let actionChat = UNNotificationAction(identifier: Constants.ActionType.CHAT.rawValue, title: "Chat", options: [.foreground])
let categoryOptions = UNNotificationCategoryOptions(rawValue: 0)
let customerSupportCategory = UNNotificationCategory(identifier: Constants.NotificationType.CUSTOMER_SUPPORT.rawValue, actions: [actionChat], intentIdentifiers: [], options: categoryOptions)
center.setNotificationCategories([customerSupportCategory])
}
application.registerForRemoteNotifications()
}
Run Code Online (Sandbox Code Playgroud)
致电3:
// remote notification
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
....Some Code....
}
Run Code Online (Sandbox Code Playgroud)
但是没有调用下面的函数.但是如果应用程序在后台运行,则调用下面的函数并且一切正常.其他方式应用程序来到前台,之后聊天不会.
// action buttons in enhanced Notification
@available(iOS 10, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: () -> Void) {
guard let action = Constants.ActionType(rawValue: response.actionIdentifier) else {
completionHandler()
return
}
switch action {
case .CHAT:
_ = self.handleRemoteUrl(NSURL(string: "chat") as? URL)
default:
_ = self.handleRemoteUrl(NSURL(string: "chat") as? URL)
}
completionHandler()
}
@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: (UNNotificationPresentationOptions) -> Void) {
completionHandler([.alert, .sound])
}
}
Run Code Online (Sandbox Code Playgroud)
永远不会调用此函数可能是因为它在iOS 10中通过userNotificationCenter()折旧,不确定.请解释一下..
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
....Some Code....
}
Run Code Online (Sandbox Code Playgroud)
我使用iPhone 6s iOS 10作为调试设备. XCode 8 beta-3
Kyl*_*len 10
根据我自己的实验,在Swift 3和Xcode 8中接收本地通知如下:
一致性
符合 UNUserNotificationCenterDelegate
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate { .... }
Run Code Online (Sandbox Code Playgroud)注册为通知中心的代表:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
let center = UNUserNotificationCenter.current()
center.delegate = self
return true
}
Run Code Online (Sandbox Code Playgroud)代表方法
回复错过的通知(例如,在发送通知时用户查看应用程序)
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: (UNNotificationPresentationOptions) -> Void) {
print(notification.request.content.userInfo)
}
Run Code Online (Sandbox Code Playgroud)响应操作通知(例如用户打开的通知)
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: () -> Void) {
print(response.notification.request.content.userInfo)
}
Run Code Online (Sandbox Code Playgroud)