2017-07-17 109 views
0

因此,當我的應用程序當前未打開並且收到推送通知時,當橫幅出現並按下時,「didrecieveremotenotification」完美無缺。但是當應用程序打開時,根本沒有橫幅出現。我希望推送通知在應用程序打開時工作。推送通知不工作迅速

我試過使用UNNotificationCenter,但一般都丟失了推送通知。

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { 
    UNUserNotificationCenter.current().delegate = self 
    UNUserNotificationCenter.current().requestAuthorization(options: [.badge, .sound, .alert], completionHandler: {(granted, error) in 
     if (granted) { 
      UIApplication.shared.registerForRemoteNotifications() 
     } else{ 
      print("Notification permissions not granted") 
     } 
    }) 
} 


//Called when a notification is delivered to a foreground app. 
public func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Swift.Void) { 
    completionHandler([.sound, .alert, .badge]) 
} 

//Called to let your app know which action was selected by the user for a given notification. 
public func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping() -> Swift.Void) { 
    completionHandler() 
} 

更新:UNUserNotificationCenter.current().requestAuthorization運行

func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) 

時授予許可。

但是,當我不使用UNNotificationCenter並使用舊方法時,它不會失敗。

爲什麼一個方法無法註冊遠程通知,但另一個不是?

更新2:在模擬器工作大聲笑,這就是爲什麼它失敗。但是現在,當我運行 UNUserNotificationCenterDelegate 方法只有

public func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive方法的工作,而不是

public func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent

更新3:我已經跑出去的可能的事情去嘗試。什麼會阻止代碼發射willPresent而不是didReceive。這是殘酷的。有沒有人曾經有過這個問題?

更新4:已解決。我用於推送通知的服務要求用戶與服務斷開連接以發送推送通知。當你在後臺時它會自動斷開你的連接,這就是爲什麼這個工作正常,並且它不在前臺工作。

奇怪的方式爲他們配置,我可能會發一封電子郵件。

+0

一切看起來不錯..嘗試刪除該應用。 – Bilal

+0

試過已經 – NickPali

+0

可以提供'didFailToRegisterForRemoteNotificationsWithError'錯誤嗎? – Imotep

回答

1

實施UNUserNotificationCenterDelegate委託方法獲取通知(托盤頂部),而應用程序在前臺。但它只會與IOS 10一起工作。

在你的didFinishLaunchingWithOptions方法集UNUserNotificationCenterDelegate委託像這樣。

UNUserNotificationCenter.current().delegate = self 

實現委託方法...

//Called when a notification is delivered to a foreground app. 
public func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Swift.Void) { 
    completionHandler([.sound, .alert, .badge]) 
} 

//Called to let your app know which action was selected by the user for a given notification. 
public func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping() -> Swift.Void) { 
    completionHandler() 
} 
+0

我試着用這個確切的代碼之前問這個問題,我的推送通知只是完全停止工作。 – NickPali

+0

請用代碼更新您的問題。 – Bilal

+0

我更新了問題 – NickPali