cordova-plugin-firebase: Problem with iOS notifications

Describe the bug Notifications does not appear.

Steps to reproduce the behavior:

1. npm i cordova
2. cordova plugin add cordova-plugin-firebase --save
3. cordova platform add ios

Expected behavior Notifications appear when app is closed or background

Implemented code

window.FirebasePlugin.getToken(function(token) {
            console.log('token: ' + token);
        }, function(error) {
            console.error(error);
        });
window.FirebasePlugin.hasPermission(function(data){
                if (!data.isEnabled) {
                    window.FirebasePlugin.grantPermission().then(data => {
                        window.FirebasePlugin.subscribe('ios_test');
                        console.log('permission granted');
                    })
                    .catch(error => {
                        console.error('Error getting permissions', error);
                        return false;
                    });
                } 
                else {
                    console.log('Already has permission');
                    window.FirebasePlugin.subscribe('ios_test');
                }
            });

Console Logs

[3214:2441143]token: eOPj1NEPqb0:APA91bEkVtc2CA...
[3214:2441143]Already has permission
[3214:2441278]5.7.0 - [Firebase/Messaging][I-FCM002024] Format '/topics/ios_test' is deprecated. Only 'ios_test' should be used in subscribeToTopic.

With Notification payload:

(
    [notification] => Array
        (
            [title] => Title test
            [body] => Body test
        )

    [data] => Array
        (
            [title] => Title test
            [text] => Text test
        )

    [priority] => high
    [to] => /topics/ios_test
)

Nothing happens. But with Data only payload:

(
    [data] => Array
        (
            [title] => Title test
            [text] => Text test
        )

    [priority] => high
    [to] => /topics/ios_test
)

In this case, I get the data when the app is in foreground but no notification when the app is closed or in background.

[3231:2468704] Received data message: {
    from = "/topics/ios_test";
    text = "Text test";
    title = "Title test";
}

Plugin Version cordova-plugin-firebase 2.0.5

Smartphone (please complete the following information):

  • Device: iPhone6
  • OS: iOS 11.4.1

Additional context I know this is possible with this plugin, since I already had this version working. I feel like I’m missing something. Can you help me.

About this issue

Most upvoted comments

@charlie-rushton for the past two days i’ve spent 16+ hours trying to get this to work. these three steps worked for me:

<platform name="ios">
  <config-file target="*-Info.plist" parent="UIBackgroundModes">
      <array>
        <string>remote-notification</string>
      </array>
    </config-file>
</platform>

Didn’t do anything else of your guide but this still fixed it for me. Thanks so much.

Hey @evinkuraga, yes I fixed this issue. Do you have a slack or email so I could send to you all of the stuffs I did? And If it did work, I’ll add it here on this thread to share it to others 😁

Hi, Add just window.FirebasePlugin.grantPermission();

it works for me.

@RicardoMSFaria : Here is the exact code I have after the ondeviceready event.

window.FirebasePlugin.hasPermission((data) => {
      if (data.isEnabled) {
        console.log("Permission already granted");
      } else {
        window.FirebasePlugin.grantPermission(() => {
          console.log("Permission granted", data.isEnabled);
        }, (error) => {
          console.error("unable to grant permission", error);
        });
      }
    }, error => {
      console.log("hasPermission failed", error);
    });

    window.FirebasePlugin.getToken((token) => {
      console.log(`Firebase Token from cordova: ${token}`);
    }, (error) => {
      console.error("unable to create token", error);
    });

In firebase (here : https://console.firebase.google.com/project/YOUR-PROJECT/settings/cloudmessaging/ ), add the .p12 file

To test easily, you can launch Postman and import this collection :

{
  "id": "f6e6ac05-6b9c-e7e1-7f91-23ca7ca55b5d",
  "name": "Test Firebase",
  "description": "",
  "order": [
    "b95718b2-6464-887a-ffc3-481b46ea180b"
  ],
  "folders": [],
  "folders_order": [],
  "timestamp": 1511419780805,
  "owner": "80140",
  "public": false,
  "requests": [
    {
      "id": "b95718b2-6464-887a-ffc3-481b46ea180b",
      "headers": "Authorization: key=<ENTER_YOUR_LEGECY_KEY - OR - SERVER_KEY>\nContent-Type: application\/json\n",
      "headerData": [
        {
          "key": "Authorization",
          "value": "key=<ENTER_YOUR_LEGECY_KEY - OR - SERVER_KEY>",
          "description": "",
          "enabled": true
        },
        {
          "key": "Content-Type",
          "value": "application\/json",
          "description": "",
          "enabled": true
        }
      ],
      "url": "https:\/\/fcm.googleapis.com\/fcm\/send",
      "queryParams": [],
      "preRequestScript": null,
      "pathVariables": [],
      "pathVariableData": [],
      "method": "POST",
      "data": [],
      "dataMode": "raw",
      "tests": null,
      "currentHelper": "normal",
      "helperAttributes": [],
      "time": 1511614084647,
      "name": "FCM Message POST",
      "description": "Send Direct Message to Device with given id. HTTP Request.",
      "collectionId": "f6e6ac05-6b9c-e7e1-7f91-23ca7ca55b5d",
      "responses": [],
      "rawModeData": "{\n\t\"to\" : \"YOUR TOKEN\",\n\t\"collapse_key\" : \"type_a\",\n\t\"data\" : {\n\t\t\"body\" : \"First Notification\",\n\t\t\"title\": \"ALT App Testing\",\n\t\t\"key_1\" : \"Data for key one\",\n\t\t\"key_2\" : \"Hellowww\"\n\t}\n}"
    }
  ]
}

It’s easier to debug