Firebase在收到推送通知时没有弹出窗口

16
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]

还有应用程序配置 输入图像描述


可能的解决方案 :) https://dev59.com/Epffa4cB1Zd3GeqP9pG9#38093732 - DiAvisoo
我也遇到了同样的问题.. 试试这个.. http://stackoverflow.com/questions/37929979/firebase-remote-notifications-not-receiving - V1P3R
6个回答

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
嗨,Alaa,欢迎来到StackOverflow。我建议对您的答案进行编辑,添加代码格式(4个缩进空格),以便更容易阅读。您是否介意还编辑一下您的答案,解释一下为什么这样可以解决问题?谢谢! - Tim Malone
如果令牌类型设置为FIRInstanceIDAPNSTokenTypeUnknown,则InstanceID将读取配置文件以查找令牌类型。来自文档:https://firebase.google.com/docs/reference/ios/firebaseinstanceid/interface_f_i_r_instance_i_d#a7cd9dde64e899e05a27397d3ee3fb398 - Rabs G
2
你救了我的一天,伙计。我一直在苦苦挣扎为什么我的通知在后台模式下无法工作,但是在添加了这个之后它开始工作了。但是这在 Firebase Messaging 的 https://github.com/firebase/quickstart-ios/blob/master/messaging/FCM/AppDelegate.m 示例代码中并没有找到,这很奇怪。 - Antarix
我集成了这段代码,但它没有起作用。当我从Firebase控制台发送消息并添加设备令牌时,我收到错误消息“无效的注册令牌。请检查令牌格式。” - Krutarth Patel
FIRInstanceIDAPNSTokenTypeSandbox 是应用程序的 APNS 令牌类型。如果令牌类型设置为 UNKNOWN,则 InstanceID 将隐式尝试从预配文件中确定实际的令牌类型。我在使用 Match 时遇到了这个问题。 - Nike Kov

1
我猜测你在测试时应用程序正在前台运行。当应用程序在前台运行时,不会触发可见通知,而是会收到回调didReceiveRemoteNotification。有关更多信息,请参见文档
要验证,请将您的应用程序置于后台,然后再次尝试发送推送通知。

抱歉,我需要澄清一下。当应用程序进入后台时,在前30秒内它还没有沉睡,因此我仍然会收到didReceiveRemoteNotification的调用。之后便不再接收。 - Svitlana
那么,在这种情况下,我同意我的回答无法帮助你,而且我也不确定问题可能是什么。抱歉! - AdamK

0

我和你的配置一样,按照AdamK所说的方式运行正常。(在后台模式下,通知会出现。)同时,请检查您的证书。


请查看此视频,https://www.youtube.com/watch?v=VpbNFIY1qJ0,时间为13:45。他们说如果您将Info.plist FirebaseAppDelegateProxyEnabled设置为NO,则应调用“setAPNSToken”和“appDidReceiveMessage”。 - wseries

0

首先,请在 Firebase 通知控制台上检查通知是否发送成功。如果成功,则问题可能出现在代码方面;否则,请检查 Firebase 中出现的错误信息。如果您收到 APNs 缺失的错误消息,则需要在“项目设置”->“云消息传递”选项卡中检查开发/生产 .p12 文件。


0

你是否正在使用https://pushtry.com来测试FCM通知? 那么不要使用,因为我在使用这个网站测试通知时遇到了很多问题,有时它能正常工作,有时却不能。它的结果不一致,可能会影响FCM流程并完全阻止接收通知。

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


0

在您的应用程序委托沙盒中使用此函数进行开发,生产环境中使用prod

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

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

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接