0

I send this to Firebase:

{
    "to" : "/topics/12344",
    "priority" : "high",
    "data" : {
      "c" : "sometext",
      "t" : "someothertext"
    },
  }

On Android I can create custom notification using the "data" payload with accessing its objects. I don't have to send the message title and body because I create custom text. This works even if the app is closed.

On iOS I can access the custom data using:

func messaging(_ messaging: Messaging, didReceive remoteMessage: MessagingRemoteMessage) {
    print("Received data message: \(remoteMessage.appData)")
}

But it only prints if the the app is opened, not when closed. If I send the "notification array" to Firebase, it creates a notification when the app is closed, but I am not able to access the custom payload.

My question: How can I access the custom payload on iOS when the app is closed and create a notification?

Francis
  • 303
  • 2
  • 5
  • 16

1 Answers1

0

If app is closed or die , only Notification can be passed to the app https://firebase.google.com/docs/cloud-messaging/concept-options

When in the background, apps receive the notification payload in the notification tray, and only handle the data payload when the user taps on the notification.

When in the foreground, your app receives a message object with both payloads available.

 {
    "to" : "APA91bHun4MxP5egoKMwt2KZFBaFUH-1RYqx...",
    "notification" : {
      "body" : "great match!",
      "title" : "Portugal vs. Denmark",
      "icon" : "myicon"
    },
    "data" : {
      "Nick" : "Mario",
      "Room" : "PortugalVSDenmark"
    }
  }
  • is there a way to receive the push notification in background only using the data payload? – Andrey Aug 15 '19 at 17:46
  • @Andrea do you mean silent notification? – Stanislav Kramarenko Aug 16 '19 at 18:17
  • yes, I want to handle myself the notification, I only need the data payload, since in Android when only data is added, it can be handled in background and in foreground, i would like the same behaviour in iOS, is it possible? – Andrey Aug 17 '19 at 03:00
  • 1
    Shortly, yes, it is possible. You can read this answer https://stackoverflow.com/a/37621274/3527461 and after that, read FireBase documentation ( link in answer ) – Stanislav Kramarenko Aug 17 '19 at 08:36