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