How to handle data type fcm notifications when app is not running or closed state in iOS similar to android?
Asked Answered
A

1

7

In android, We can receive data type of notifications when app is not running or closed state. And can parse and display notification with code and can do anything like updating old message, etc..

public class FCMListenerService extends FirebaseMessagingService {
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        Log.d("onMessageReceived for FCM", remoteMessage.getData().toString());
    }
}

How can I do similar thing in ios?

I'm able to get data type of notifications when app is in background or foreground. So I'm able to do what ever I want. But I'm unable to debug how to handle data type notifications when app is not running? Can someone help me?

Below is the payload I'm trying to send.

{
  "to": "..............",
  "data": {
    "body": "data",
    "title": "Portugal vs. Denmark",
    "icon": "myicon"
  },
  "content_available": true
}
Altimetry answered 17/7, 2017 at 7:43 Comment(2)
Correct me if I'm wrong, but isn't it that iOS doesn't allow apps that are closed/not running to receive notifications? AFAIK, that's it's default behavior.Ratline
@AL Then can't we receive fcm data messages when app is not running in iOS?Altimetry
R
0

You are not allowed to put custom tags inside aps tag. Here's what documentations says about it:

Providers can specify custom payload values outside the Apple-reserved aps namespace. Custom values must use the JSON structured and primitive types: dictionary (object), array, string, number, and Boolean. So in your case you should do something like:

{
    "aps" : {
        "alert" : {
            "title" : "Game Request",
            "body" : "Bob wants to play poker",
            "action-loc-key" : "PLAY"
        },
        "badge" : 5
    },
    "acme1" : "bar",
    "acme2" : [ "bang",  "whiz" ]
}

Therefore you can read your custom payload with looking for it's key in main JSON, rather than in "aps":

For more info:

https://developer.apple.com/library/content/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/CreatingtheNotificationPayload.html

Ryeland answered 17/7, 2017 at 7:54 Comment(1)
Thanks for the answer, But here it's internally done by FCM I guess. And is there any way to sent some object to device through APN, parsing it and displaying notification with custom logic when app is not runningAltimetry

© 2022 - 2024 — McMap. All rights reserved.