Fireground通知(仅限数据)未在Foreground中接收

时间:2017-03-06 13:25:32

标签: ios swift firebase firebase-cloud-messaging

嘿大家我都在阅读人们对Firebase和iOS通知的一些问题,但似乎任何人都有同样的问题。

目前我正在谈论 FOREGROUND 应用状态:

应用程序会收到包含参数通知的通知,例如:

let notificationsParameters:[String:AnyObject] = [
        "to": "iphoneID",
        "content-available":true,
        "priority":"high",
// "notification" : [
//                "body" : "great match!",
//                "title" : "Portugal vs. Denmark",
//                "icon" : "myicon"
//            ],
        "data" : [
            "Nick" : "Mario",
            "Room" : "PortugalVSDenmark"
        ]
    ]

但是一旦我删除了通知部分应用程序没有收到任何内容,甚至保持在前景,因为我看到每个人都应该收到通知。

我在接收甚至已关闭/后台和内容可用时添加了优先级,我读到的是为了解决我的问题但是不是这样。

这里有相关代码:

APP DELEGATE

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // Override point for customization after application launch.



        GMSServices.provideAPIKey(Constants.GoogleMaps.APIKey)
        FIRApp.configure()


        /* NOTFICATIONS */

        let notificationsType:UIUserNotificationType = [UIUserNotificationType.Alert, UIUserNotificationType.Badge, UIUserNotificationType.Sound]
        let notificationSettings = UIUserNotificationSettings(forTypes: notificationsType, categories: nil)
        application.registerForRemoteNotifications()
        application.registerUserNotificationSettings(notificationSettings)

        NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(self.tokenRefreshNotification(_:)), name: kFIRInstanceIDTokenRefreshNotification, object: nil)


        return true
    }

通知(在应用程序代理中)

据我所知,这就是我将收到通知数据

的内容
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject],
                     fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
        // If you are receiving a notification message while your app is in the background,
        // this callback will not be fired till the user taps on the notification launching the application.
        // TODO: Handle data of notification

        // Print message ID.
        print("Message ID: \(userInfo["gcm.message_id"]!)")

        // Print full message.
        print("%@", userInfo)
    }

    func tokenRefreshNotification(notification:NSNotification) {
        let refreshedToken = FIRInstanceID.instanceID().token()
        print("InstanceID token: \(refreshedToken)")

        connectToFCM()
    }

    func connectToFCM() {
        FIRMessaging.messaging().connectWithCompletion { (error) in
            if error != nil {
                print("GMS ERROR: \(error)")
            }
            else {
                print("Connected to GMS")
            }
        }
    }

致电消防通知

func sendNotification() {

        let notificationsParameters:[String:AnyObject] = [
            "to": "iphoneID",
            "content-available":true,
            "priority":"high",
            //            "notification" : [
            //                "body" : "great match!",
            //                "title" : "Portugal vs. Denmark",
            //                "icon" : "myicon"
            //            ],
            "data" : [
                "Nick" : "Mario",
                "Room" : "PortugalVSDenmark"
            ]
        ]


        let URL = NSURL(string: "https://fcm.googleapis.com/fcm/send")!
        let URLRequest = NSMutableURLRequest(URL: URL)

        URLRequest.HTTPMethod = "POST"

        URLRequest.setValue("key=\(Constants.Firebase.NotificationsKey)", forHTTPHeaderField: "Authorization")
        URLRequest.setValue("application/json", forHTTPHeaderField: "Content-Type")

        let encoding = Alamofire.ParameterEncoding.JSON

        let encoded = encoding.encode(URLRequest, parameters: (notificationsParameters)).0
        Alamofire.request(encoded)
    }

您需要的任何进一步信息只是让我知道!

2 个答案:

答案 0 :(得分:2)

您在正确的轨道上,但在content_available参数中需要下划线而不是连字符。如果您想要使用FCM进行仅数据(静默)通知,则无需使用notification对象。

例如,在您的FCM发送请求正文中:

{
    "to": "iPhoneID",
    "content_available": true,
    "priority": "high",
    "data": {
        "nick": "mario",
        "room": "PortugalVSDenmark"
    }
}

答案 1 :(得分:1)

对于那些与我有同样问题的人,一种解决方案是设置通知参数,如下所示:

let notificationsParameters:[String:AnyObject] = [
            "to": "iphoneID",
            "content-available":true,
            "priority":"high",
           "notification" : [
               "sound" : " "
           ],
            "data" : [
                "Nick" : "Mario",
                "Room" : "PortugalVSDenmark"
            ]
        ]

当应用被杀时,你不会收到通知,但是当你处于前台和后台时,你只能收到数据通知,祝你好运!