程序未運行的時候收到推送通知
在程序未運行的時候,點擊推送通知欄通知,需要在didFinishLaunchingWithOptions里面處理
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions:
[UIApplication.LaunchOptionsKey: Any]?) -> Bool {
UIApplication.shared.registerForRemoteNotifications() //注冊
//申請推送權限
if #available(iOS 10.0, *){
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]){ granted, error in
if error != nil {
print(error.debugDescription)
}
}
UNUserNotificationCenter.current().delegate = self
} else{
UIApplication.shared.registerUserNotificationSettings(UIUserNotificationSettings(types: [.alert, .badge, .sound] , categories: nil))
}
//點擊推送通知欄通知,進來之后 處理數(shù)據(jù),
if let userInfo = launchOptions?[.remoteNotification] as? [AnyHashable: Any] {
print(userInfo)
//處理數(shù)據(jù),這個userInfo與didReceiveRemoteNotification 方法內的 userInfo內容一致.
//比如跳轉頁面,或者進行一些存儲等處理.
}
}
程序啟動過程中,或者程序掛起狀態(tài)下
如果在程序啟動過程中,或者程序掛起狀態(tài)下,需要到application的代理方法內處理
//打印收到的apns信息
func application(_ application: UIApplication,
didReceiveRemoteNotification userInfo: [AnyHashable: Any],
fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void){
print("userInfo")
//比如發(fā)送通知,或者app內部彈框告知有消息等處理. !!!注意,程序啟動的時候,有通知,也會走這個方法
NotificationCenter.default.post(name: Notification.Name(rawValue: "RefreshMessages"), object: nil, userInfo: nil)
completionHandler(.newData)
}
如果我的文章對你有幫助,請給我一個??哦~~!