1

작동하지, 나는 통지를 가지고 있으며 상단에 표시하고, 그러나푸시 알림 전경

func messaging(_ messaging: Messaging, didReceive remoteMessage: MessagingRemoteMessage) 

firebase 대리인이 호출 중이며 데이터를 가져올 수 있지만 알림이 맨 위에 표시되지 않습니다.

내 코드는 다음과 같습니다 :

func registerForPushNotification(_ application: UIApplication) 
{ 

    if #available(iOS 10.0, *) { 
     // For iOS 10 display notification (sent via APNS) 
     UNUserNotificationCenter.current().delegate = self 
     let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound] 
     UNUserNotificationCenter.current().requestAuthorization(
      options: authOptions, 
      completionHandler: {_, _ in }) 
     // For iOS 10 data message (sent via FCM 
     //Messaging.messaging().delegate = self 
    } else { 
     let settings: UIUserNotificationSettings = 
      UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil) 

     application.registerUserNotificationSettings(settings) 
    } 
    application.registerForRemoteNotifications() 
    application.applicationIconBadgeNumber = 0 

} 

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) 
{ 
    print("device token %@",deviceToken) 

    var deviceTokenString = String(format: "%@", deviceToken as CVarArg) 
    deviceTokenString = deviceTokenString.trimmingCharacters(in: CharacterSet(charactersIn: "<>")) 
    deviceTokenString = deviceTokenString.replacingOccurrences(of: " ", with: "") 

    print(deviceTokenString) 
    //saveDeviceIdinDefaults(deviceId: deviceTokenString) 
    Messaging.messaging().apnsToken = deviceToken 

//  InstanceID.instanceID().setAPNSToken(deviceToken, type:InstanceIDAPNSTokenType.sandbox) 
//  InstanceID.instanceID().setAPNSToken(deviceToken, type:InstanceIDAPNSTokenType.prod) 

} 

// The callback to handle data message received via FCM for devices running iOS 10 or above. 
func application(received remoteMessage: MessagingRemoteMessage) { 
    print(remoteMessage.appData) 

} 

@available(iOS 10.0, *) 
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping() -> Void) { 
    print("Userinfo \(response.notification.request.content.userInfo)") 

    if response.actionIdentifier == UNNotificationDismissActionIdentifier { 
     print ("Message Closed") 
    } 
    else if response.actionIdentifier == UNNotificationDefaultActionIdentifier { 
     print ("App is Open") 
    } 

    // Else handle any custom actions. . . 
    completionHandler() 


    // print("Userinfo \(response.notification.request.content.userInfo)") 
} 

func application(_ application: UIApplication, 
       didFailToRegisterForRemoteNotificationsWithError error: Error) { 
    print("Failed to register: \(error)") 
} 

@objc func tokenRefreshNotification(_ notification: NSNotification) { 
    // print("refresh token call") 
    guard let contents = InstanceID.instanceID().token() 
     else { 
      return 
    } 
    // let refreshedToken = FIRInstanceID.instanceID().token()! 
    let refreshedToken = contents//InstanceID.instanceID().token() 
    print("InstanceID token: \(refreshedToken)") 

    Constant.saveFcmToken(fcmToken: refreshedToken, key: "deviceid") 
    //  UserDefaults.standard.set(refreshedToken, forKey: "deviceid") 

    print("InstanceID token: \(contents)") 
    connectToFcm() 
} 

func connectToFcm() { 
    // Won't connect since there is no token 
    guard InstanceID.instanceID().token() != nil else { 
     print("FCM: Token does not exist.") 
     return 
    } 

    // Disconnect previous FCM connection if it exists. 
    Messaging.messaging().disconnect() 

    Messaging.messaging().connect { (error) in 
     if error != nil { 
      print("FCM: Unable to connect with FCM. \(error.debugDescription)") 
     } else { 
      print("Connected to FCM.") 
     } 
    } 
} 
// [START refresh_token] 

//MARK: FCM Token Refreshed 
func messaging(_ messaging: Messaging, didRefreshRegistrationToken fcmToken: String) { 
    // FCM token updated, update it on Backend Server 
    print("Firebase registration token: \(fcmToken)") 
    saveDeviceIdinDefaults(deviceId: fcmToken) 
} 


func messaging(_ messaging: Messaging, didReceive remoteMessage: MessagingRemoteMessage) { 
    print("remoteMessage: \(remoteMessage)") 
    UNUserNotificationCenter.current().requestAuthorization(
     options: [.alert,.sound,.badge], 
     completionHandler: { (granted,error) in 
      NotificationCenter.default.post(name: NSNotification.Name("GetPollsUpdateNotification"), object: nil) 
      //self.isGrantedNotificationAccess(data: remoteMessage.appData) 
    } 
    ) 

    //   let notifiDict = remoteMessage.appData as! [String:String] 
    //   print(notifiDict) 
    //   let title = notifiDict["title"] 
    //   let message = notifiDict["message"] 
    //   let image = notifiDict["image"] 
} 

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

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) { 
    print(userInfo) 
    if application.applicationState == .active { 

     //write your code here when app is in foreground 
    } else { 
     //write your code here for other state 
    } 
} 

@available(iOS 10.0, *) 
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) 
{ 

    completionHandler([UNNotificationPresentationOptions.alert]) 

    let userInfo = notification.request.content.userInfo as! NSDictionary 
    print("\(String(describing: userInfo))") 

    print(notification.description) 
} 

어느 한 나 좀 도와 주 시겠어요?

+2

앱이 포 그라운드 일 때 푸시 알림이 표시되지 않습니다. 앱에 적합한 경우 콜백이 호출 될 때 자신의 알림을 표시 할 수 있습니다. –

답변

0

나는이 문제를 처리하는 방법은 다음과 위임 방법을 사용하여 믿습니다 :

@available(iOS 10.0, *) 
    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) { 

      completionHandler([.alert, .sound, .badge]) 

    } 

희망이 도움이됩니다.