【问题标题】:Firebase when receive Push Notification did not receive the popupFirebase 收到推送通知时没有收到弹出窗口
【发布时间】:2016-09-24 07:41:51
【问题描述】:
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 -> Console -> Notification -> Single device 发送消息,并在此处复制我从 xCode Console -> 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]

还有应用配置

【问题讨论】:

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


【解决方案1】:

您是否使用https://pushtry.com 来测试 FCM 通知? 那就不要使用,因为我在这个网站上测试通知有很多问题,有时它可以工作,有时不能。它没有给出一致的结果,它可能会在 FCM 流中起作用并完全阻止接收通知。

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

【讨论】:

    【解决方案2】:

    只需在您的应用委托沙箱中使用此功能进行开发 prod for prodction

    func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
    
        FIRInstanceID.instanceID().setAPNSToken(deviceToken, type: FIRInstanceIDAPNSTokenType.sandbox)
    
        FIRInstanceID.instanceID().setAPNSToken(deviceToken, type: FIRInstanceIDAPNSTokenType.prod)
    }
    

    【讨论】:

      【解决方案3】:

      首先使用 Firebase 通知控制台检查通知是否正在发送。如果是成功,那么问题出在代码端;否则,请检查 Firebase 中出现了什么错误。如果您收到 APNs 缺失的错误消息,您需要检查项目设置->云消息选项卡中的开发/生产 .p12 文件。

      【讨论】:

        【解决方案4】:

        在 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];
        
        
            }
        

        【讨论】:

        • 嗨阿拉,欢迎来到 StackOverflow。我建议对您的答案进行编辑,以添加代码格式(4 个缩进空格)以使其更易于阅读。你介意编辑你的答案来解释为什么这可以解决问题吗?谢谢!
        • 非常感谢 - 这对 GCM 快速入门示例很有帮助。添加了此内容后会弹出通知。我想知道为什么!!
        • 如果令牌类型设置为 FIRInstanceIDAPNSTokenTypeUnknown InstanceID 将读取配置文件以找出令牌类型。来自文档:firebase.google.com/docs/reference/ios/firebaseinstanceid/…
        • 你拯救了我的一天,我一直在苦苦挣扎,为什么我的通知在后台模式下无法正常工作,但添加后它开始工作。但这在 firebase 消息 github.com/firebase/quickstart-ios/blob/master/messaging/FCM/… 示例代码中找不到,这很奇怪
        • 我集成了此代码但无法正常工作。当我从 firebase 控制台发送消息并添加设备令牌时,我收到错误注册令牌无效。检查令牌格式。
        【解决方案5】:

        我有与您相同的配置,它的工作方式与 AdamK 所说的一样。 (在后台模式下,会出现通知。)同时检查您的证书。

        【讨论】:

        • 查看此视频,youtube.com/watch?v=VpbNFIY1qJ0 时间 13:45。他们说如果你将 Info.plist FirebaseAppDelegateProxyEnabled 设置为 NO 你应该调用“setAPNSToken”和“appDidReceiveMessage”
        【解决方案6】:

        我猜您的应用在测试时处于前台。当您的应用程序处于前台时,不会触发可见通知,而是会收到对didReceiveRemoteNotification 的回调。请参阅documentation 了解更多信息。

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

        【讨论】:

        • 谢谢,我又检查了一遍。但是,当应用程序处于后台模式时,我有相同的情况,调用了 didReceiveRemoteNotification 处理程序但没有弹出消息。我还尝试在应用程序进入睡眠状态时等待,并且在我没有接到任何 didReceiveRemoteNotification 电话之后,显然根本没有弹出窗口。
        • 对不起,我不确定。文档确实说,didReceiveRemoteNotification 应该只在应用程序处于前台时调用,所以即使在后台您收到它的事实意味着它可能被检测为在前台运行(以及为什么弹出窗口不显示)。
        • 对不起,我需要澄清一下。当应用程序进入后台时,在前 30 秒内它还没有进入睡眠状态,因此我仍然会收到 didReceiveRemoteNotification 调用。在没有电话之后。
        • 啊,在这种情况下,我同意我的回答无济于事,我不确定问题可能是什么。对不起!
        • 感谢您的帮助,无论如何!
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-11-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-05-25
        • 1970-01-01
        相关资源
        最近更新 更多