接收推送通知时Firebase未收到弹出窗口

时间:2016-05-26 10:43:10

标签: ios firebase google-cloud-messaging firebase-cloud-messaging firebase-notifications

import Firebase
import FirebaseInstanceID
import FirebaseMessaging
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

    registerForPushNotifications(application)
    FIRApp.configure()

    // Add observer for InstanceID token refresh callback.
    NSNotificationCenter
     .defaultCenter()
     .addObserver(self, selector: #selector(AppDelegate.tokenRefreshNotificaiton),
                                                     name: kFIRInstanceIDTokenRefreshNotification, object: nil)

    // Override point for customization after application launch.
    return true
  }

func registerForPushNotifications(application: UIApplication) {
      let settings: UIUserNotificationSettings =
        UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil)
      application.registerUserNotificationSettings(settings)
      application.registerForRemoteNotifications()
  }


  func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject],
                   fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
    print("===== didReceiveRemoteNotification ===== %@", userInfo)
  }


 func tokenRefreshNotificaiton(notification: NSNotification) {
    let refreshedToken = FIRInstanceID.instanceID().token()!
    print("InstanceID token: \(refreshedToken)")

    // Connect to FCM since connection may have failed when attempted before having a token.
     connectToFcm()
  }

  func connectToFcm() {
    FIRMessaging.messaging().connectWithCompletion { (error) in
      if (error != nil) {
        print("Unable to connect with FCM. \(error)")
      } else {
        print("Connected to FCM.")
      }
    }
  }

还要在Info.plist中完成FirebaseAppDelegateProxyEnabled = NO

我现在不知道但是我在didReceiveRemoteNotification中得到了print(...)但没有得到弹出窗口。我从Firebase发送消息 - >控制台 - >通知 - >单个设备并在此复制我从xCode控制台获得的令牌 - > func tokenRefreshNotificaiton

在控制台中获取下一个,但不要弹出

<FIRAnalytics/INFO> Firebase Analytics enabled
InstanceID token: TOKEN_ID
Connected to FCM.
===== didReceiveRemoteNotification ===== %@ [notification: {
    body = test;
    e = 1;
}, collapse_key: com.pf.app, from: 178653764278]

还有app配置 enter image description here

6 个答案:

答案 0 :(得分:11)

在AppDelegate.m中设置以下代码

   - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {

    // for development 
        [[FIRInstanceID instanceID] setAPNSToken:deviceToken type:FIRInstanceIDAPNSTokenTypeSandbox];

    // for production 
   //     [[FIRInstanceID instanceID] setAPNSToken:deviceToken type:FIRInstanceIDAPNSTokenTypeProd];


    }

答案 1 :(得分:1)

我猜你的应用程序在测试时处于前台。当您的应用位于前台时,不会触发可见通知,而是会收到didReceiveRemoteNotification的回调。有关详细信息,请参阅documentation

要验证,请将您的应用置于后台并尝试再次发送推送通知。

答案 2 :(得分:0)

我有相同的配置,就像AdamK说的那样。 (在后台模式下,会显示通知。)另请检查您的证书。

答案 3 :(得分:0)

首先检查Firebase Notification Console以查看通知是否正在发送。如果成功,则问题出在代码方面;否则,请检查Firebase中出现的错误。如果您在缺少APN时收到错误消息,则需要在项目设置 - >云消息传递选项卡中查看开发/生产.p12文件。

答案 4 :(得分:0)

只需在您的app委托沙箱中使用此功能进行生产的开发prod

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

    FIRInstanceID.instanceID().setAPNSToken(deviceToken, type: FIRInstanceIDAPNSTokenType.sandbox)

    FIRInstanceID.instanceID().setAPNSToken(deviceToken, type: FIRInstanceIDAPNSTokenType.prod)
}

答案 5 :(得分:0)

您是否使用https://pushtry.com测试FCM通知然后不使用,因为该网站在测试通知有时会正常工作,有时却无法正常工作,因此存在很多问题。它没有给出一致性结果,可能会影响FCM流程并完全阻塞接收通知。

我建议使用https://fcm.snayak.dev测试通知。