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

Svi*_*ana 16 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.")
      }
    }
  }
Run Code Online (Sandbox Code Playgroud)

还要在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]
Run Code Online (Sandbox Code Playgroud)

还有app配置 在此输入图像描述

Ala*_*meh 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];


    }
Run Code Online (Sandbox Code Playgroud)

  • 你救了我一天的男人,我很难为什么我的通知不能在后台模式下工作,但是在添加它之后就开始工作了.但这在firebase消息传递中找不到https://github.com/firebase/quickstart-ios/blob/master/messaging/FCM/AppDelegate.m示例代码很奇怪 (2认同)

Ada*_*amK 1

我猜您的应用程序在测试时位于前台。当您的应用程序位于前台时,不会触发可见的通知,而是您会收到 的回调didReceiveRemoteNotification。请参阅文档以获取更多信息。

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