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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37458530/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 09:10:27  来源:igfitidea点击:

Firebase when receive Push Notification did not receive the popup

iosfirebasegoogle-cloud-messagingfirebase-cloud-messagingfirebase-notifications

提问by Svitlana

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.")
      }
    }
  }

Also to done in Info.plist FirebaseAppDelegateProxyEnabled = NO

也在 Info.plist FirebaseAppDelegateProxyEnabled = NO 中完成

I don't know for now but I got the print(...) in didReceiveRemoteNotification but don't get the popup. I send the message from Firebase -> Console -> Notification -> Single device and copy here the token which I got from xCode Console -> func tokenRefreshNotificaiton

我现在不知道,但我在 didReceiveRemoteNotification 中得到了 print(...) 但没有得到弹出窗口。我从 Firebase -> Console -> Notification -> Single device 发送消息,然后将我从 xCode Console 获得的令牌复制到这里 -> func tokenRefreshNotificaiton

Get the next in console, but don't get popup

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

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

Also app configurations enter image description here

还有应用程序配置 在此处输入图片说明

回答by Ala'a AbuNemeh

set the following code in AppDelegate.m

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


    }

回答by AdamK

I'm guessing your app is in the foreground when testing. When your app is in the foreground no visible notification is triggered, instead you receive the callback to didReceiveRemoteNotification. See the documentationfor more info.

我猜你的应用程序在测试时处于前台。当您的应用程序在前台时,不会触发任何可见的通知,而是您会收到对didReceiveRemoteNotification. 有关更多信息,请参阅文档

To verify, put your app in the background and try sending the push notification again.

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

回答by wseries

I have same configuration you have and it works like AdamK said. (While in background mode, notification appears.) Also check your certificates.

我有和你一样的配置,它像 AdamK 说的那样工作。(在后台模式下,会出现通知。)还要检查您的证书。

回答by Kavin Kumar Arumugam

First check with Firebase Notification Console to see if the notification is sending or not. If it is success, then the problem is in the code side; otherwise, check what error is coming in Firebase. If you receive error message as APNs missing, you need to check with development/production .p12 file in Project Setting->Cloud Messaging tab.

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

回答by Gowtham Sooryaraj

Just use this function in your app delegate sandbox for development prod for prodction

只需在您的应用程序委托沙箱中使用此功能进行开发生产即可

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

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

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