0

我一直試圖在我的應用中獲取Firebase推送通知。我已經嘗試了互聯網上的所有內容,但找不到解決方案。我一直在後臺收到通知,但是當App處於前臺時,我無法收到通知。但是,當我在「didReceiveRemoteNotification」中打印userInfo時,我在控制檯 中收到消息。無法使用FCM在前臺接收推送通知

import Firebase 
import UserNotifications 


@UIApplicationMain 
class AppDelegate: UIResponder, UIApplicationDelegate{ 

var window: UIWindow? 


func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { 

    //FirebaseApp.configure() 

    self.initializeFCM(application) 
    let token = InstanceID.instanceID().token() 
    debugPrint("GCM TOKEN = \(String(describing: token))") 

    return true 
} 

func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) 
{ 
    debugPrint("didFailToRegisterForRemoteNotificationsWithError: \(error)") 
} 

func application(received remoteMessage: MessagingRemoteMessage) 
{ 
    debugPrint("remoteMessage:\(remoteMessage.appData)") 
} 

func initializeFCM(_ application: UIApplication) 
{ 
    print("initializeFCM") 

    if #available(iOS 10.0, *) // enable new way for notifications on iOS 10 
    { 
     let center = UNUserNotificationCenter.current() 
     center.delegate = self 
     center.requestAuthorization(options: [.badge, .alert , .sound]) { (accepted, error) in 
      if !accepted 
      { 
       print("Notification access denied.") 
      } 
      else 
      { 
       print("Notification access accepted.") 
       UIApplication.shared.registerForRemoteNotifications(); 
      } 
     } 
    } 
    else 
    { 
     let type: UIUserNotificationType = [UIUserNotificationType.badge, UIUserNotificationType.alert, UIUserNotificationType.sound]; 
     let setting = UIUserNotificationSettings(types: type, categories: nil); 
     UIApplication.shared.registerUserNotificationSettings(setting); 
     UIApplication.shared.registerForRemoteNotifications(); 
    } 

    FirebaseApp.configure() 
    Messaging.messaging().delegate = self 
    Messaging.messaging().shouldEstablishDirectChannel = true 

} 

// enable new way for notifications on iOS 10 

func application(_ application: UIApplication, didRegister notificationSettings: UIUserNotificationSettings) 
{ 
    debugPrint("didRegister notificationSettings") 
    if (notificationSettings.types == .alert || notificationSettings.types == .badge || notificationSettings.types == .sound) 
    { 
     application.registerForRemoteNotifications() 
    } 
} 


func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) 
{ 
    debugPrint("didRegisterForRemoteNotificationsWithDeviceToken: NSDATA") 

    let token = String(format: "%@", deviceToken as CVarArg) 
    debugPrint("*** deviceToken: \(token)") 

    Messaging.messaging().apnsToken = deviceToken as Data 
    debugPrint("Firebase Token:",InstanceID.instanceID().token() as Any) 
} 

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) 
{ 
    debugPrint("didRegisterForRemoteNotificationsWithDeviceToken: DATA") 
    let token = String(format: "%@", deviceToken as CVarArg) 
    debugPrint("*** deviceToken: \(token)") 

    Messaging.messaging().apnsToken = deviceToken 
    debugPrint("Firebase Token:",InstanceID.instanceID().token() as Any) 
} 
//-------------------------------------------------------------------------// 

// [START receive_message] 
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) { 

    Messaging.messaging().appDidReceiveMessage(userInfo) 
    print(userInfo) 
} 

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any], 
       fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) { 
    Messaging.messaging().appDidReceiveMessage(userInfo) 
    print(userInfo) 

    completionHandler(UIBackgroundFetchResult.newData) 
} 
// [END receive_message] 

} 


// [START ios_10_message_handling] 
@available(iOS 10, *) 
extension AppDelegate : UNUserNotificationCenterDelegate { 

// Receive displayed notifications for iOS 10 devices. 
func userNotificationCenter(_ center: UNUserNotificationCenter, 
          willPresent notification: UNNotification, 
          withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) { 
    let userInfo = notification.request.content.userInfo 

    // With swizzling disabled you must let Messaging know about the message, for Analytics 
    Messaging.messaging().appDidReceiveMessage(userInfo) 
    print(userInfo) 

    // Change this to your preferred presentation option 
    completionHandler([]) 
} 

func userNotificationCenter(_ center: UNUserNotificationCenter, 
          didReceive response: UNNotificationResponse, 
          withCompletionHandler completionHandler: @escaping() -> Void) { 
    let userInfo = response.notification.request.content.userInfo 
    Messaging.messaging().appDidReceiveMessage(userInfo) 
    print(userInfo) 

    completionHandler() 
} 
} 
// [END ios_10_message_handling] 

extension AppDelegate : MessagingDelegate { 
// [START refresh_token] 
func messaging(_ messaging: Messaging, didRefreshRegistrationToken fcmToken: String) { 
    print("Firebase registration token: \(fcmToken)") 
} 

func messaging(_ messaging: Messaging, didReceive remoteMessage: MessagingRemoteMessage) { 
    print("Received data message: \(remoteMessage.appData)") 
} 
// [END ios_10_data_message] 
} 

回答

5

從iOS的10,您可以使用此功能顯示你的蘋果的默認通知旗幟。通知行爲將取決於您在completionHandler中返回的屬性。

func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) { 
     let content = notification.request.content 
     // Process notification content 
     print("\(content.userInfo)") 
     completionHandler([.alert, .sound]) // Display notification as 

    } 

正如我可以回答看,你在上面已經用空白completionHandler功能實現的()。因爲你在app處於前臺狀態時沒有收到通知。

此前iOS的10:

您需要自己處理這個問題。如果您希望在收到通知時顯示橫幅,而應用處於前臺狀態。你需要自己做。您必須設計自定義通知橫幅以在應用內顯示。

希望它可以幫助你。

+0

我很難接到通知是前臺,你能告訴我如何設計自定義通知的橫幅。 –

+0

你正在測量在iOS 10或更低? – Surjeet

+0

要創建自定義通知標題,請檢查此存儲庫https://github.com/Daltron/NotificationBanner以及更多可用的github。 – Surjeet