4

私はthis tutorialthis projectを参考にしています。これはAppDelegateのコードですswift 3、ios 10 - プッシュ通知火災基地が届かない

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証明書をアップロードしました。私は能力に関するプッシュ通知をONにしました。私は自分のデバイスで試してみました。私は自分のプロジェクトにGoogleService-Info.plistを追加しました。しかし、私はフォアグラウンドかバックグラウンドから通知を受けていません。

私のコードで間違いをしましたか?または、私の証明書を更新する必要がありますか?私は途中でこれにかなり新しいです。

私はそれを解決する手助けはできますか?ほんとうにありがとう。

ありがとうございます!

答えて

0

プロダクションAPN証明書をアップロードしていないためです。私はthis tutorialに見たので、私はそれが必要ではないと思って、それはちょうど開発APNsの証明書を使用しました。

1

deviceTokenStringまたはdeviceTokenIdをデータベースに登録する必要があります。必要に応じて適切な配布証明書または開発証明書を用意する必要があります。プッシュ通知を受信するための基本的な必要事項です。

コードから、deviceTokenString/Idを登録していません。

ありがとう

+0

deviceTokenStringをデータベースに追加するコードを追加しましたが、通知は表示されません。私も正しい開発証明書を持っています。私はこれを知らない。方法であなたの応答をありがとう:) – cathrinnatalia

関連する問題