Firebase收到推送通知时未收到弹出窗口 [英] Firebase when receive Push Notification did not receive the popup

查看:241
本文介绍了Firebase收到推送通知时未收到弹出窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 导入Firebase 
导入FirebaseInstanceID
导入FirebaseMessaging
func应用程序(application:UIApplication,didFinishLaunchingWithOptions launchOptions:[NSObject:AnyObject]?) - > Bool {

registerForPushNotifications(应用程序)
FIRApp.configure()

//为InstanceID令牌刷新回调添加观察者。
NSNotificationCenter
.defaultCenter()
.addObserver(self,selector:#selector(AppDelegate.tokenRefreshNotificaiton),
name:kFIRInstanceIDTokenRefreshNotification,object:nil)

//覆盖应用程序启动后自定义的点。
返回true

$ b $ func registerForPushNotifications(application:UIApplication){
let settings:UIUserNotificationSettings =
UIUserNotificationSettings(forTypes:[.Alert,.Badge ,.sound],类别:nil)
application.registerUserNotificationSettings(设置)
application.registerForRemoteNotifications()
}


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


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

//连接到FCM,因为在有令牌之前尝试连接可能失败。
connectToFcm()
}

func connectToFcm(){
FIRMessaging.messaging()。connectWithCompletion {(error)in
if(error!=无){
print(无法连接FCM。\(错误))
} else {
print(Connected to FCM。)
}




$ b $ p
$ b

也在Info.plist中完成FirebaseAppDelegateProxyEnabled = NO



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



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

 < FIRAnalytics / INFO>已启用Firebase Analytics 
InstanceID令牌:TOKEN_ID
已连接到FCM。
===== didReceiveRemoteNotification =====%@ [通知:{
body = test;
e = 1;
},collapse_key:com.pf.app,from:178653764278]


在AppDelegate.m中设置以下代码

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

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

//用于生产
// [[FIRInstanceID instanceID] setAPNSToken:deviceToken type:FIRInstanceIDAPNSTokenTypeProd];


}


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

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

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

解决方案

set the following code in 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];


    }

这篇关于Firebase收到推送通知时未收到弹出窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆