4

this tutorialthis project을 추천합니다. 이것은 AppDelegate의 코드입니다.swift 3, ios 10 - 푸시 알림 firebase을받지 못했습니다.

import UIKit 
import UserNotifications 
import Firebase 

@UIApplicationMain 
class AppDelegate: UIResponder, UIApplicationDelegate { 

    var window: UIWindow? 
    let gcmMessageIDKey = "gcm.message_id" 
    let preferences = UserDefaults.standard 

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

     Messaging.messaging().delegate = self 

     if #available(iOS 10.0, *) { 
      UNUserNotificationCenter.current().delegate = self 

      let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound] 
      UNUserNotificationCenter.current().requestAuthorization( 
       options: authOptions, 
       completionHandler: {_, _ in }) 
     } else { 
      let settings: UIUserNotificationSettings = 
       UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil) 
      application.registerUserNotificationSettings(settings) 
     } 

     application.registerForRemoteNotifications() 
     FirebaseApp.configure() 
     return true 
    } 

    func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) { 
     if let messageID = userInfo[gcmMessageIDKey] { 
      print("Message ID: \(messageID)") 
     } 

     print("message1:",userInfo) 

    } 

    func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any], 
       fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) { 
     if let messageID = userInfo[gcmMessageIDKey] { 
      print("Message ID: \(messageID)") 
     } 

     print("message2:", userInfo) 

     completionHandler(UIBackgroundFetchResult.newData) 

    } 

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

     print("Unable to register for remote notifications: \(error.localizedDescription)") 
    } 

    func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) { 

     print("APNs token retrieved: \(deviceToken)") 
     let apn = deviceToken.map { String(format: "%02.2hhx", $0) }.joined() 

    } 

    func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) { 

     Messaging.messaging().appDidReceiveMessage(userInfo) 

    } 
} 

@available(iOS 10, *) 
extension AppDelegate : UNUserNotificationCenterDelegate { 

    func userNotificationCenter(_ center: UNUserNotificationCenter, 
          willPresent notification: UNNotification, 
          withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) { 
     let userInfo = notification.request.content.userInfo 

     if let messageID = userInfo[gcmMessageIDKey] { 
      print("Message ID: \(messageID)") 
     } 

     print("userinfo", userInfo) 

     completionHandler([]) 
    } 

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

     if let messageID = userInfo[gcmMessageIDKey] { 
      print("Message ID: \(messageID)") 
     } 

     print("userinfo", userInfo) 

     completionHandler() 
    } 
} 


extension AppDelegate : MessagingDelegate { 

    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)") 

    } 
} 

나는 fcmtoken과 APNs 토큰을 가지고 있습니다. 내 개발 APN 인증서를 업로드했습니다. 기능에 대한 Push Notification을 ON으로 설정했습니다. 내 장치에서 해봤 어. 내 프로젝트에 GoogleService-Info.plist를 추가했습니다. 하지만 전경이나 배경에서 아무런 알림도받지 못했습니다.

코드 작성에 실수를 했습니까? 또는 인증서를 업데이트해야합니까? 나는 그 때까지 이것에 아주 새롭다.

누구든지 해결할 수 있습니까? 정말 감사.

감사합니다.

답변

0

프로덕션 APN 인증서를 업로드하지 않았기 때문입니다. this tutorial에서 보았고 개발 용 APN 인증서 만 사용 했으므로 필요하지 않다고 생각했습니다.

1

deviceTokenString 또는 deviceTokenId을 데이터베이스에 등록해야하며 필요에 따라 올바른 배포 또는 개발 인증서가 있어야합니다. 푸시 알림을 수신하기위한 기본 요구 사항입니다.

코드에서 사용자가 deviceTokenString/Id을 등록하지 않았습니다.

감사합니다.

+0

deviceTokenString을 데이터베이스에 추가하는 코드를 추가했지만 여전히 알림이 표시되지 않습니다. 나 또한 올바른 개발 인증서가 있습니다. 나는 이것에 대해 잘 모른다. 방법으로 답변을 주셔서 감사합니다 :) – cathrinnatalia