Push Notifications not received when Ionic Cordova app is in background

130 views Asked by At

I'm currently working on a mobile app developed with Cordova and Ionic.

Problem I'm faceing is notification messages are received only when app is in foreground or in background. If the app is closed, notifications are no more received.

Here is my environment configuration:

Cordova CLI version
    11.0.0
Cordova platform version
    android 12.0.1
Plugins & versions installed in project (including this plugin)
cordova-plugin-advanced-http 3.3.1 "Advanced HTTP plugin"
cordova-plugin-device 2.1.0 "Device"
cordova-plugin-file 7.0.0 "File"
cordova-plugin-firebasex 16.2.0 "Google Firebase Plugin"
cordova-plugin-ionic-keyboard 2.2.0 "cordova-plugin-ionic-keyboard"
cordova-plugin-ionic-webview 5.0.0 "cordova-plugin-ionic-webview"
cordova-plugin-splashscreen 5.0.2 "Splashscreen"
cordova-plugin-statusbar 2.4.2 "StatusBar"
cordova-plugin-x-socialsharing 6.0.4 "SocialSharing"
cordova.plugin.autostart.permission 0.0.1 "AutoStartPermission"
es6-promise-plugin 4.2.2 "Promise"
Dev machine OS and version, e.g.
    Windows 10

This is the code I wrote to manage notifications (it's in app.component.ts file, inside the ngOnInit() function):

`this.platform.ready().then(() => {

  this.firebase.grantPermission().then(hasPermission => {
    console.log(":::::HAS PERMISSION: ", hasPermission);
  }).catch(e => {
    console.log(":::::HAS PERMISSION ERROR: ", e);
  });

  this.firebase.getToken().then(token => {
    this.sendToken(token!);
  }).catch(e => {
    // Error
  });

  this.firebase.onMessageReceived().subscribe(data => {
    
    console.log(":::::::RECEIVED NOTIFICATION: "+JSON.stringify(data));
    
  });

});`

The code in this.firebase.grantPermission() is ignored, no permission is requested and no logs are written in console (but it should be correct, at least if I correctly understood what's written in the documentation about plugin usage under Android).

I also tried to add following configuration in config.xml file:

<config-file parent="/*" target="AndroidManifest.xml"> <uses-permission android:name="android.permission.POST_NOTIFICATIONS" /> </config-file>

With this configuration when application is executed on recent phones (android 13+) a message appears asking to give permission to manage notifications, but nothing changes. Even with permission granted, no notification is received when app is closed.

Push notifications I'm sending have are created with this format:

{ "data": { "title": "TITLE", "body": "MSG", "forceStart": "1" }, "priority": "high", "to": "....token here..." }

This issue seems to be present in recent versions of cordova platform. In apps developed with older versions of cordova (for example cordova 6.3.0) notifications are received.

I also tried with: -Test project provided with this same plugin -A clean cordova project with only firebasex plugin installed -A clean capacitor project with only the push plugin (in this case it's not the cordova-plugin-firebasex, but a similar one)

In all these cases I experienced same issue: notifications are not received when the app is closed... Isn't it a way to receive notifications on newer phones?

0

There are 0 answers