quickstart-ios: FCMSwift. Notifications don't appear

I send notifications like this: { “aps” : { “alert” : { “title” : “Game Request”, “body” : “Bob wants to play poker”

    },
    "badge" : 1

},

“data”:{ “to_user_ids”:[1], “subject”:“TEXT”, “msg”:“STRINGS” } }

My app receives the notifications and in the “func application(application: UIApplication, didReceiveRemoteNotification userInfo…” it prints userInfo with data. But the app doesn’t show any notifications at the device and, when the app in background it neither prints message on console nor shows notifications at device.

Could you give me clear clues how to recive notifications when the app in background, handle in the code and show them on the notifications area?

Thanks

P.S. I wasn’t able to figure out it on https://developer.apple.com/library/mac/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/TheNotificationPayload.html#//apple_ref/doc/uid/TP40008194-CH107-SW1

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Comments: 99 (7 by maintainers)

Most upvoted comments

I think Firebase Push service is not ready for prod yet… Waste on it almost one week.

I have same problem, tried all this solutions and i can’t make notifications work in background. I tried with this data:

{
  "registration_ids": ["APA91bEDB9dVf-..."]
  "collapse_key": null,
  "content_available": true,
  "data": {
    "msg": "{\"model\":\"article\",\"data\":\"reload\"}"
  },
  "notification": {
    "body": "my body",
    "sound": "default",
    "title": "my title"
  },
  "priority": "high",
  "time_to_live": 3600
}
curl --header "Authorization: key=MYKEY" --header "Content-Type: application/json" https://fcm.googleapis.com/fcm/send -d '{"registration_ids": ["APA91bEDB9dVf-..."],"collapse_key": null,"content_available": true,"notification": {"body": "my body","sound": "default","title": "my title"},"priority": "high","time_to_live": 3600,"data": {"msg":"{\"model\":\"article\",\"data\":\"reload\"}"}}'

when i open app i get all messages that were sent when app was closed. Anyone have suggestion? I tried with all solutions posted in answers, and still same.

Here’s some love for you gents: http://stackoverflow.com/questions/38065813/curl-sent-firebase-cloud-messaging-alert-not-visibly-appearing-on-ios-device

It’s a quick fix, simply add “priority”: “high” in your payload next to “notification” etc. and the iOS Notification will work when the app is in the background.

So this will work:

"to":"TOKEN ID",
"notification" : {
"body" : "test"
},
"priority": "high"
}

I’m seeing a similar issue where I get a nil token upon receiving the kFIRInstanceIDTokenRefreshNotification notification. In the (documentation)[https://firebase.google.com/docs/notifications/ios/console-device#access_the_registration_token], it says:

kFIRInstanceIDTokenRefreshNotification fires when tokens are generated, so calling [[FIRInstanceID instanceID] token] in its context ensures that you are accessing a current, available registration token.

This means I should expect a valid token after that notification, right? But in my case, FIRInstanceID.instanceID().token() is sometimes nil, which seems like a bug.

I don’t know if this is related, but I noticed that when successfully connecting to FCM via FIRMessaging.messaging().connectWithCompletion, the token is sometimes still nil. Also, sometimes connection to FCM fails with this undescriptive error:

Error Domain=com.google.fcm Code=2001 "(null)"

It’s not clear to me why connecting to FCM sometimes fails and sometimes succeeds. Even more confusing is, why do I get a nil token even after connecting successfully and upon receiving a notification of a token change.

i got response {“multicast_id”:5165050424619311455,“success”:1,“failure”:0,“canonical_ids”:0,“results”:[{“message_id”:“0:1465956637529105%c722c384c722c384”}]}

But ios device do not display notification

@shehang like this? screenshot 2016-07-08 12 37 32

@diamo1213 try setting the FirebaseAppDelegateProxyEnabled to Boolean YES in your info.plist file. screen shot 2016-06-02 at 10 45 27 pm

@diamo1213 @iboylmz a notification message should be formatted like this:

{
  "to" : "IID-TOKEN-OR-TOPIC",
  "notification" : {
    "body" : "great match!",
    "title" : "Portugal vs. Denmark",
    "icon" : "myicon"
  }
}

see here for more on FCM message formats. The format is the same for both iOS and Android. FCM makes the necessary platform adjustments automatically depending on the downstream client.

@kroikie i’ve just sent { “notification”:{ “body”:“test…” }, “priority”: “high”, “content_available”: true } to https://gcm-http.googleapis.com/gcm/send.

the same happens

Now it works,Ι added the follow lines in the first View Controller app launches , and NOT in AppDelegate.swift file

import FirebaseMessaging and in viewDidLoad()

DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(4), execute: {
            FIRMessaging.messaging().subscribe(toTopic: "/topics/news")
            print("Subscribed to news topic")
        })

I also added the follow in AppDelegate for device token (Swift 3)

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        var token: String = ""
        for i in 0..<deviceToken.count {
            token += String(format: "%02.2hhx", deviceToken[i] as CVarArg)
        }

        //Tricky line
        FIRInstanceID.instanceID().setAPNSToken(deviceToken, type: FIRInstanceIDAPNSTokenType.unknown)
        print("Device Token:", token)
    }

and in Info.plist add FirebaseAppDelegateProxyEnabled to YES

I think the original issue here has been sorted out, so I’m going to close this, feel free to create more issues if you have different or similar issues.

I was facing the same issue. Fixed it by building the response in the following way:

{
        "to" : "/topics/{userId}"
        "content_available":true,
        "priority":"high"
        "notification" : {
                "title": "",
                "body":""
        },
        "data" : {
                //custom key value pairs
        }
}

“priority”:”high” makes FCM push through APNS. This allowed the app to receive the notification when the app is switched to background state.

Note: My FirebaseAppDelegateProxyEnabled is set to NO in my plist file.

@diamo1213 you mentioned that while your app is not running you can still get notifications if the notification is sent from the console but not when you send using the fcm endpoint: https://fcm.googleapis.com/fcm/send

Could you try the same with the gcm endpoint: https://gcm-http.googleapis.com/gcm/send"

and report if the same happens?

@diamo1213 have you subscribed the channel in iOS app? I have subscribed by “FIRMessaging.messaging().subscribeToTopic(”/topics/news")".

In my experience,

FIRapp.configure() FIRMessaging.messaging().subscribeToTopic(“/topics/news”) -> this call fail. I guess the configuration task is async.

so, I have called “FIRMessaging.messaging().subscribeToTopic(”/topics/news")" in delegate function(didRegisterUserNotificationSettings). and work properly.

hope it helps.

@kingleo33 i’ve sent from my appserver: { “to”:“/topics/global”, “notification”:{ “body”:“rttrtht” } } but nothing in iOS. in android app alright

@erickva, just sent from my server app: { “notification”: { “title”: “test”, “body”: “my message” }, “priority”: “high” }

server app got {“message_id”:9150563171907108451} in response from FCM.

but in device received nothing.

At the same time if i send from Firebase console notifications are showed on devices’ natifs bar.

In background works fine with data notifs