havesource / cordova-plugin-push

Register and receive push notifications
MIT License
147 stars 283 forks source link

Android Background Notifications do not show after Force Close App #188

Closed mcleodtech closed 1 year ago

mcleodtech commented 2 years ago

Bug Report

Expected Behaviour

The Cordova app is force closed, a push notification is sent to the device and the device displays the notification using the native android notification settings (the shade may show notification and notification appears in notification tray).

Actual Behaviour

A push notification is not received. Not sure if this is a bug, or just my own error.

Reproduce Scenario (including but not limited to)

I have only reproduced this on Android when force closing an app (to test the receipt of a notification when the app process is no longer running on Android). Background notifications seem to work fine, as well as foreground notifications on iOS and Android. I cannot reproduce this on iOS.

Steps to Reproduce

Build an Android App Force close app by going to Settings => Apps => {App Name} => Press "Force Stop" Send a push notification to the device

Platform and Version (eg. Android 5.0 or iOS 9.2.1)

Android 11

(Android) Device Vendor (e.g. Samsung, HTC, Sony...)

Samsung

cordova info Printout

Cordova Packages:

cli: 11.0.0
    common: 4.0.2
    create: 4.0.0
    lib: 11.0.0
        common: 4.0.2
        fetch: 3.0.1
        serve: 4.0.0

Project Installed Platforms:

android: 10.1.2

Project Installed Plugins:

@havesource/cordova-plugin-push: 3.0.1
cordova-plugin-device: 2.0.3
cordova-plugin-dialogs: 2.0.2
cordova-plugin-geolocation: 4.1.0
cordova-plugin-navigationbar-color: 0.0.8
cordova-plugin-statusbar: 2.4.3

Environment:

OS: Microsoft Windows 11 Pro 10.0.22000 (22000) (Windows 10.0.22000) x64
Node: v16.14.2
npm: 8.7.0

Project Setting Files:

config.xml:

<?xml version='1.0' encoding='utf-8'?>

Test Push Notifications A sample Apache Cordova application that responds to the deviceready event. Apache Cordova Team
package.json:

--- Start of Cordova JSON Snippet ---

{
  "plugins": {
    "cordova-plugin-geolocation": {
      "GPS_REQUIRED": "true"
    },
    "cordova-plugin-device": {},
    "cordova-plugin-statusbar": {},
    "cordova-plugin-navigationbar-color": {},
    "@havesource/cordova-plugin-push": {},
    "cordova-plugin-dialogs": {}
  },
  "platforms": [
    "android"
  ]
}

--- End of Cordova JSON Snippet ---

Sample Push Data Payload

'body' => $message,
'sound' => 'default',
'fcm' => [
    'notification' => [
        'body' => $message,
    ],

Sample Code that illustrates the problem

var registerationInit = window.PushNotification.init({

    android: {
        sound: true,
        vibrate: true,
        forceShow: false,
    },
    ios: {
        alert: true,
        badge: true,
        sound: true,
    },
});

var userId = this.getUser().id;
var platformId = window.cordova?.platformId;

registerationInit.on('registration', data => {
this.http
  .post(`/${userId}/push_tokens`, { 'token': data.registrationId, 'platform': platformId }, { observe: 'response' })
  .subscribe(
      (res) => {
          if (res.status === 200) {
              console.log("Device Token Binding Successful");
          }
      },
      (res) => {
          if (res.error) {
              console.log(`Binding Failed due to: ${res.error}`);
          }
      }
  );

})

registerationInit.on('notification', data => {console.log(data); window.navigator.notification.alert(data.message, data.title)})

Logs taken while reproducing problem

I don't have a log for a background notification.

MiguelQueiroz commented 1 year ago

I noticed one important thing. When the app is closed or in background the plugin does try to start the activity again, but for that it requires the permissions SYSTEM_ALERT_WINDOW, which is the ability to display popups, Have to manually set them. For the user to be able to permit you must have this:

Also some popular apps, do have this permitted automatically "popular apps" have that grant according to google. Maybe thats what helps to keep the notification happening.

 <edit-config file="AndroidManifest.xml" mode="merge" target="/manifest/uses-permission" xmlns:android="http://schemas.android.com/apk/res/android">
        <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
    </edit-config>

This will start the activity again if you data payload contains for example this:

"data" => [
        "content-available" => "1",
        "force-start" => "1",
    ],
mcleodtech commented 1 year ago

@MiguelQueiroz Thank you so much! I'm going to test it out and let you know how it works!

mcleodtech commented 1 year ago

This worked! Closing this out.