Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Android 13+ app doesn't trigger on('notification') when clicking notification while app is closed or running in background #288

Closed
redbird19 opened this issue Sep 6, 2024 · 16 comments

Comments

@redbird19
Copy link

FYI, I'm using cordova-plugin-push 4.0.0 and testing with my Android 13 phone.

When the app is closed, or running in background, and a notification is received, clicking the notification will open the app, but not fire the on('notification') event, which prevents the app from automatically redirecting the user to the appropriate location in the app related to the notification. This only seems to affect Android 13+.

FYI, if I have the app open when the notification is received, the app correctly fires the on('notification') event, enabling automatic redirection to the appropriate location.

In iOS, things work perfectly, clicking the notification with the app closed, or running in background, opens the app AND fires the on('notification') event allowing the user to be automatically redirected to the appropriate location in the app.

@Guillermo-G-M
Copy link

Same issue here

@ciuffo81
Copy link

Hi,
we had the same issue, we found the solution here
#266 (comment)

You can try to put in your payload this
"click_action": "com.adobe.phonegap.push.background.MESSAGING_EVENT"

@redbird19
Copy link
Author

redbird19 commented Sep 12, 2024

Thanks @ciuffo81, however, I haven't been able to get it to work even after adding the "click_action". I tried different ways of setting the click_action, first trying to add it to the "notification" property/object, but received an invalid JSON payload error.

Here's my code showing all the places I've tried to include the "click_action" property, including the commented out places that caused invalid JSON payload errors:

var response = await _sender.SendAsync(
    new {
        message = new
        {
            token = devicePushToken,
            notification = new {
                title = subjectOrTitle,
                body = bodyContent,
                //click_action = "com.adobe.phonegap.push.background.MESSAGING_EVENT" // This throws invalid JSON payload received error
            },
            android = new {
                notification = new {
                    click_action = "com.adobe.phonegap.push.background.MESSAGING_EVENT" // This has no effect
                }
            },
            data = new
            {
                notificationType = notificationType,
                notificationArgs = notificationKey),
                click_action = "com.adobe.phonegap.push.background.MESSAGING_EVENT" // This has no effect
            },
            //click_action = "com.adobe.phonegap.push.background.MESSAGING_EVENT" // This throws invalid JSON payload received error
        },
        //click_action = "com.adobe.phonegap.push.background.MESSAGING_EVENT" // This throws invalid JSON payload received error
    }
);

In looking at this https://firebase.google.com/docs/cloud-messaging/concept-options#notifications, it appears that utilizing the "android.notification.click_action" property is the proper way of doing it, but it has a not effect from my tests.

I also looked at the issue you linked to and saw someone had a problem with a missing entry in the manifest, but I've verified my manifest file has the following:

<activity android:exported="true" android:name="com.adobe.phonegap.push.BackgroundHandlerActivity" android:permission="${applicationId}.permission.BackgroundHandlerActivity">
    <intent-filter>
        <action android:name="com.adobe.phonegap.push.background.MESSAGING_EVENT" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</activity>

Nothing seems to work.

@vlads63
Copy link

vlads63 commented Sep 18, 2024

Have you been able to find a solution? I am having the exact same issue and none of the suggested solutions work for me.

@redbird19
Copy link
Author

No, I haven't found a solution yet.

@ElliotNB
Copy link

ElliotNB commented Sep 18, 2024

I'm experiencing the same issue as well. I've made a few different attempts at a fix (including the example above), but I haven't been able to get it to work.

@erisu Would you be able to weigh in on this issue?

@ksch10bob
Copy link

Hi everyone,
i would like to help to narrow down the problem.

We use the plugin and everything is working fine for us.
Unfortenatly we do not have real Android 13 and 14 devices but our tests on emulated devices with android 13 and 14 went well.
here are our details

  • cordova 12.0.0
  • cordova-android 13.0.0
  • android-targetSdkVersion 34
  • android-minSdkVersion 24
  • android-buildToolsVersion 34.0.0
  • AndroidLaunchMode singleTask
  • cordova-plugin-push 4.0.0

Our Tests included the cases

  • only data payload with click_action = "com.adobe.phonegap.push.background.MESSAGING_EVENT" set
    Result: on('notification') event was fired in backround or closed state

  • only data payload without click_action = "com.adobe.phonegap.push.background.MESSAGING_EVENT" set
    Result: on('notification') event was fired in backround or closed state

  • mixed notification and data payload with click_action = "com.adobe.phonegap.push.background.MESSAGING_EVENT" set
    Result: on('notification') event was fired in backround or closed state

  • mixed notification and data payload without click_action = "com.adobe.phonegap.push.background.MESSAGING_EVENT" set
    Result: on('notification') event was NOT fired in backround or closed state

So everything is working as described in the documentation

So here are my questions:

  • what are your build details ?
  • which payload test did you do and what was the result?
  • can you reproduce the issue on emulated devices?
  • did you try to buld a minimal reproduction scenario by only using the cordova app template and the push plugin?

@vlads63
Copy link

vlads63 commented Sep 24, 2024

Turns out my code was missing "click_action": "com.adobe.phonegap.push.background.MESSAGING_EVENT"

I got it working with the following payload:

{
    "message": {
        "token": "<TOKEN>",
        "notification": {
            "body": "Test",
            "title": "Hello"
        },
        "android": {
            "notification": {
                "click_action": "com.adobe.phonegap.push.background.MESSAGING_EVENT"
            }
        },
        "apns": {
            "payload": {
                "aps": {
                    "alert": {
                        "title": "Hello",
                        "body": "Test"
                    }
                }
            }
        },
        "data": {
            "key1": "value1",
            "key2": "value2"
        }
    }
}

@ksch10bob
Copy link

@vlads63 thats good news

@redbird19 @ElliotNB do you have new insights? what are your buildetails?

@redbird19
Copy link
Author

redbird19 commented Sep 30, 2024

Hi @ksch10bob, thanks for willing to help out.

I use Ionic AppFlow to build my app, and the build details are as follows:

Cordova CLI 12.0.0
Ionic CLI 7.1.1
Node.js 18.18.2 / 20.9.0
npm 10.1.1
Yarn 1.22.19
Debian 11.6
Gradle 8.4
OpenJDK 1.8.0_332 / 11.0.18 / 17.0.6
Android SDK 27-34

I also use the following Environment overides:

ANDROID_HOME /opt/android-sdk
OVERRIDE_JAVA_VERSION 17

My config.xml file uses:

<plugin name="@havesource/cordova-plugin-push" spec="4.0.0" />

And uses the following preferences:

<preference name="android-minSdkVersion" value="24" />
<preference name="android-targetSdkVersion" value="34" />
<preference name="android-compileSdkVersion" value="34" />

My server code that sends the notifications uses the following payload:

var response = await _sender.SendAsync(
    new {
        message = new
        {
            token = devicePushToken,
            notification = new {
                title = subjectOrTitle,
                body = bodyContent
            },
            android = new {
                notification = new {
                    click_action = "com.adobe.phonegap.push.background.MESSAGING_EVENT"
                }
            },
            data = new
            {
                notificationType = notificationType,
                notificationArgs = notificationKey
            }
        }
    }
);

The server code utilizes CorePush (https://github.com/andrei-m-code/net-core-push-notifications) to make the call to: https://fcm.googleapis.com/v1/projects/{settings.ProjectId}/messages:send, and POST the serialized payload.

Everything works great when the app is open on my Android 13 phone. The on('notfication') event is fired, and I can parse the message and redirect the user to the appropriate place in the app based on the arguments in the "data" properties. But if the app is closed or in the background when the notification is received, clicking the notification will open the app but the on('notification') event is NOT triggered.

Unfortunately, I only have my personal Android 13 phone I'm testing with, so I can't be sure if this is exclusively a problem with my phone, which is an Android Moto G Play (XT2413V) phone.

Edit:

I've now also tested with several Android emulated devices via https://appetize.io (Pixel 8, Pixel 7, Galaxy Tab S7) running Android 13 and 14, and all have the same behavior described above (when app is closed or in background, clicking the received notification opens the app but does NOT trigger the on('notification') event).

@ksch10bob
Copy link

@redbird19 thanks for the information.
unfortunately i am not familiar with ionic appflow.
here are some more questions?
Does your app use the capacitor framwork? if so, what version of capacitor does it use? If not, which version of cordova-android does it use?

@ksch10bob
Copy link

@redbird19
everything you posted looks good to me so far.
To figure out if the issue is within the cordova app setup or in the message sending setup i would do the following

  • Build a local minimal pushregistration cordova app
  • test the message paload against this minimal app

@redbird19
Copy link
Author

@ksch10bob, thanks for taking a look. The Ionic Appflow build stack uses cordova 12.0.0 (so I'm assuming Cordova Android 12.0.0).

FYI, Ionic Appflow is sort of the successor to Adobe's PhoneGap Build (or at least the alternative that I chose). When Adobe shutdown PhoneGap Build back in August 2020, I switched over to Ionic Appflow following this migration guide (https://ionic.io/docs/appflow/cookbook/phonegap-build-migration). The migration was pretty painless and only required a handful of mostly minor changes, and they have been pretty good at updating the build stacks to support the latest versions of Cordova.

The iOS version of the app works flawlessly, and the Android version is nearly perfect as well, just need to fix this last issue with on('notification') not firing when the app is closed/in-background.

I'm not really sure how to simplify the app's code. Everything else seems to be working fine, and no errors are thrown, it's just that the on('notification') event is not fired when the app is closed/in-background. I even connected the phone to Chrome's debugging and added breakpoints, but they just aren't hit. I'll keep trying though.

@redbird19
Copy link
Author

I added some more debugging code and after a lot of trial and error finally got the on('Notification') event to fire when the app is closed or in the background! :)

You definitely need to add/include the android.notification.click_action property:

    android: {
        notification: {
            click_action = "com.adobe.phonegap.push.background.MESSAGING_EVENT"
        }
    }

However, one oddity I didn't expect is that the data received by the on('Notification') event differs depending on whether the app was open or not. With the app open, the message received looks like this:

{
    "title": "Testing Subject 123",
    "message": "Testing Body 123",
    "additionalData": {
        "notificationArgs": "123",
        "notificationType": "Test Type",
        "coldstart": false,
        "foreground": true
    }
}

But when the notification received when the app is in the background is clicked, the app is opened and the message received by the on('Notification') event looks like this:

{
    "additionalData": {
        "google.delivered_priority": "normal",
        "google.original_priority": "normal",
        "dismissed": false,
        "notificationArgs": "123",
        "notificationType": "Test Type",
        "coldstart": false,
        "foreground": false
    }
}

Note the lack of "title" and "message" properties in the payload.

I think I can work around this by adding the "title" and "message" to the "data" property when sending the notification and then checking the "additionalData" property for the title and/or message when receiving the message in on('notification').

Oh....strangely, I just tried to add the "title" property and "message" to the "data" property when sending, and message received in on('notification') shifted the "title" and "message" properties to the top level

{
    "title": "Testing Subject 123",
    "message": "Testing Body 123",
    "additionalData": {
        "google.delivered_priority": "normal",
        "google.original_priority": "normal",
        "dismissed": false,
        "notificationArgs": "123",
        "notificationType": "Test Type",
        "coldstart": false,
        "foreground": false
    }
}

It's still a bit of a mystery to my why the data payload is different, or why I need to send the notification a little differently to get it to work when the app is closed, but at least I got it working now. Thanks for the help!

@ksch10bob
Copy link

@redbird19 thats good news.

The FCM SDK behaves differently for foreground and background receiving when using mixed payloads (notification and data payload)

It's still a bit of a mystery to my why the data payload is different,

This is standardbehavior documented here
https://firebase.google.com/docs/cloud-messaging/concept-options#notification-messages-with-optional-data-payload

@redbird19
Copy link
Author

@ksch10bob, thanks for the help and pointing me to the documentation, good to know!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants