У моего приложения происходит сбой при открытии из состояния уничтожения через Push-уведомление .Это работает хорошо, если приложение уже запущено, но когда приложение убито, тогда, если получено какое-либо push-уведомление, и я нажимаю на него, оно вызывает сбой приложения.Я не нашел ни одной ошибки, может ли кто-нибудь помочь мне решить эту проблему?
Если я прокомментирую UNUserNotificationCenter
код из didFinishLaunchingWithOptions
в AppDelegate.swift
, тогда приложение не будет аварийно завершено, но представление не будет загружено с уведомлением,Я отправляю URL в push-уведомлении и проверяю его не пустым, а затем загружаю как представление.
AppDelegate.swift
import UIKit
import UserNotifications
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {
var window: UIWindow?
var apiUrl = "http://www.example.com/api/";
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
let token = deviceToken.map { String(format: "%02.2hhx", $0) }.joined()
// Device Registration with API
deviceRegistration(token)
//print("Token: \(token)")
}
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
}
// Device Registration with API
func deviceRegistration(_ token: String) {
let parameters = ["UUID": UIDevice.current.identifierForVendor!.uuidString, "Token": token, "DevOption": "Dev", "MID": "0"]
let url = URL(string: apiUrl + "ios-register")!
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
let httpBody = try? JSONSerialization.data(withJSONObject: parameters, options: [])
request.httpBody = httpBody
let session = URLSession.shared.dataTask(with: request) { (data, response, error) in
if let data = data {
do {
let json = try JSONSerialization.jsonObject(with: data, options: [])
print(json)
} catch {
}
}
}
session.resume()
}
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
completionHandler([.alert, .sound])
}
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
// Check User Tap the notification
if let notification = response.notification.request.content.userInfo as? [String: AnyObject] {
let message = parseRemoteNotification(notification: notification)
guard let url = message?["url"] as? String else {
return;
}
// If url exists then load the url
if !(url.isEmpty) {
loadView(url)
}
}
completionHandler()
}
private func parseRemoteNotification(notification:[String:AnyObject]) -> NSDictionary? {
if let aps = notification["aps"] as? [String:AnyObject] {
let alert = aps["alert"] as? NSDictionary
return alert
}
return nil
}
func loadView(_ url: String) {
let data: [String: String] = ["url": url]
NotificationCenter.default.post(name: NSNotification.Name("loadWebView"), object: nil, userInfo: data)
}
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
if let sb = UIApplication.shared.value(forKeyPath: "statusBarWindow.statusBar") as? UIView {
sb.backgroundColor = UIColor.init(red: 252/255, green: 153/255, blue: 0/255, alpha: 1)
}
// Local Notification
//if(application.applicationState == .active) {
UNUserNotificationCenter.current().delegate = self
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound]) { (granted, error) in
//print("Granted: \(granted)")
}
//}
// Push Notifications
UIApplication.shared.registerForRemoteNotifications()
return true
}
func applicationWillResignActive(_ application: UIApplication) {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}
func applicationDidEnterBackground(_ application: UIApplication) {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
func applicationWillEnterForeground(_ application: UIApplication) {
// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}
func applicationDidBecomeActive(_ application: UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
func applicationWillTerminate(_ application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
}
ViewControllwer.swift
import UIKit
import WebKit
import UserNotifications
class ViewController: UIViewController, WKNavigationDelegate {
@IBOutlet var mWebKit: WKWebView!
@IBOutlet var indicator: UIActivityIndicatorView!
public var defaultUrl = "https://www.example.com";
public var viewUrl = URL(string: "https://www.example.com")!
override func viewDidLoad() {
super.viewDidLoad()
mWebKit.navigationDelegate = self
self.mWebKit.addObserver(self, forKeyPath: "URL", options: .new, context: nil)
self.mWebKit.addObserver(self, forKeyPath: "estimatedProgress", options: .new, context: nil)
loadWebView(viewUrl)
// On Notification Receive
NotificationCenter.default.addObserver(forName: NSNotification.Name("loadWebView"), object: nil, queue: nil) { (Notification) in
//print("notification is \(Notification)")
let url = URL(string: Notification.userInfo?["url"] as? String ?? self.defaultUrl)
self.loadWebView(url ?? self.viewUrl)
}
// Do any additional setup after loading the view, typically from a nib.
}
func loadWebView(_ url: URL) {
var request = URLRequest(url: url)
request.setValue("com.example.in", forHTTPHeaderField: "X-REQUESTED-WITH")
self.mWebKit.load(request)
}
deinit {
NotificationCenter.default.removeObserver(self)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning();
// Dispose of any resources that can be recreated
}
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
if keyPath == #keyPath(WKWebView.url) {
indicator.startAnimating()
loadWebView(self.mWebKit.url!)
}
if keyPath == #keyPath(WKWebView.estimatedProgress) {
if(self.mWebKit.estimatedProgress == 1) {
indicator.stopAnimating()
}
}
}
}