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

Amplify.Notifications.Push.onNotificationOpened.listen wont trigger on Android #5474

Closed
2 of 14 tasks
gigamike opened this issue Sep 17, 2024 · 7 comments
Closed
2 of 14 tasks

Comments

@gigamike
Copy link

gigamike commented Sep 17, 2024

Description

I'm using Amplify for push notifications. In IOS, when I clicked the push message, it works fine. I can get the push payload i.e.

flutter: 🚀 onNotificationOpened PushNotificationMessage { "title": "Test title", "body": "Test body please ignore", "imageUrl": null, "deeplinkUrl": null, "goToUrl": null, "fcmOptions": null, "apnsOptions": { "subTitle": null }, "data": { "type": "friend_request", "data1": "data21" } }

But when using Android it seems the Amplify.Notifications.Push.onNotificationOpened.listen won't trigger. I just received a message like this

D/AmplifyAuthCognitoPlugin(11095): [onNewIntent] Got intent: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x14400000 pkg=au.movinghub.homehub cmp=au.movinghub.homehub/com.example.homehub.MainActivity (has extras) }
D/AmplifyAuthCognitoPlugin(11095): [onNewIntent] Not handling intent

Here is a sample code

Future<void> _configureAmplify() async {
    try {
      final authPlugin = AmplifyAuthCognito();
      final pushPlugin = AmplifyPushNotificationsPinpoint();

      pushPlugin.onNotificationReceivedInBackground(myCallback);

      if (!Amplify.isConfigured) {
        await Amplify.addPlugins([authPlugin, pushPlugin]);
        await Amplify.configure(amplifyconfig);

        Amplify.Notifications.Push.onTokenReceived.listen((event) {
          print('🚀 onTokenReceived $event');
          SessionManager().setString("device_id", event.toString());
        });

        // Required to call this after Amplify.configure.
        Amplify.Notifications.Push.onNotificationReceivedInForeground
            .listen((event) {
          print('🚀 onNotificationReceivedInForeground $event');
        });

        // Required to call this after Amplify.configure.
        // wont trigger in Android
        Amplify.Notifications.Push.onNotificationOpened.listen((event) {
          print('🚀 onNotificationOpened $event');
          print(event.data);
        });
      }
      setState(() {
        _amplifyConfigured = true;
      });

      /*
      final subscription = Amplify.Notifications.Push.onNotificationOpened
          .listen(myNotificationOpenedHandler);
      print(subscription);
      */
    } on Exception catch (e) {
      print(e.toString());
    }

    AmplifyLogger().logLevel = LogLevel.info;
  }

Im using https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-pinpoint-2016-12-01.html#sendmessages to trigger a push which works fine on both IOS and Android.

Categories

  • Analytics
  • API (REST)
  • API (GraphQL)
  • Auth
  • Authenticator
  • DataStore
  • Notifications (Push)
  • Storage

Steps to Reproduce

Setup Amplify push notification using Amazon Pinpoint.

Screenshots

No response

Platforms

  • iOS
  • Android
  • Web
  • macOS
  • Windows
  • Linux

Flutter Version

3.24.1

Amplify Flutter Version

2.2.0

Deployment Method

Amplify Gen 2

Schema

No response

@github-actions github-actions bot added pending-triage This issue is in the backlog of issues to triage pending-maintainer-response Pending response from a maintainer of this repository labels Sep 17, 2024
@Equartey
Copy link
Contributor

Hi @gigamike, sorry to hear you've ran into this issue.

We'll investigate it and provide an update here when we can, thanks.

@github-actions github-actions bot removed the pending-maintainer-response Pending response from a maintainer of this repository label Sep 17, 2024
@tyllark
Copy link
Member

tyllark commented Sep 17, 2024

Hello @gigamike I was unable to reproduce the issue on my Android device, could you please try running this sample app to see if it still occurs. When I run it I get the following logs:

[onNewIntent] Got intent: Intent { flg=0x10400000 cmp=com.example.amplify_skeleton/.MainActivity (has extras) }
[onNewIntent] Not handling intent
PushNotifications - Open Message - PushNotificationMessage {
  "title": "Test Title",
  "body": "Test Body",
  "imageUrl": null,
  "deeplinkUrl": null,
  "goToUrl": null,
  "fcmOptions": {
    "channelId": "PINPOINT.NOTIFICATION",
    "messageId": null
  },
  "apnsOptions": null,
  "data": {
    "pinpoint.openApp": "true",
    "pinpoint.notification.title": "Test Title",
    "pinpoint.notification.body": "Test Body",
    "pinpoint.campaign.campaign_id": "_DIRECT",
    "pinpoint.notification.silentPush": "0"
  }
}

Can you also confirm if this issue occurs when triggering a push notification from your AWS console Pinpoint -> All projects -> [Project name] -> Test messaging?

@gigamike
Copy link
Author

gigamike commented Sep 17, 2024

Hi @tyllark ,

Thanks for reply. Your sample code is almost the same as mine. When sending push message via Pinpoint i.e. Pinpoint -> All projects -> [Project name] -> Test messaging?, I'm receiving this

I/FA ( 3621): Application backgrounded at: timestamp_millis: 1726613957096
I/flutter ( 3621): 🚀 Foreground Message PushNotificationMessage {
I/flutter ( 3621): "title": "test",
I/flutter ( 3621): "body": "test",
I/flutter ( 3621): "imageUrl": null,
I/flutter ( 3621): "deeplinkUrl": null,
I/flutter ( 3621): "goToUrl": null,
I/flutter ( 3621): "fcmOptions": {
I/flutter ( 3621): "channelId": "PINPOINT.NOTIFICATION",
I/flutter ( 3621): "messageId": null
I/flutter ( 3621): },
I/flutter ( 3621): "apnsOptions": null,
I/flutter ( 3621): "data": {
I/flutter ( 3621): "pinpoint.openApp": "true",
I/flutter ( 3621): "pinpoint.notification.title": "test",
I/flutter ( 3621): "pinpoint.notification.body": "test",
I/flutter ( 3621): "pinpoint.campaign.campaign_id": "_DIRECT",
I/flutter ( 3621): "pinpoint.notification.silentPush": "0"
I/flutter ( 3621): }
I/flutter ( 3621): }

BUT when I clicked the push message

D/AmplifyAuthCognitoPlugin( 3621): [onNewIntent] Got intent: Intent { flg=0x10400000 cmp=au.movinghub.homehub/com.example.homehub.MainActivity (has extras) }
D/AmplifyAuthCognitoPlugin( 3621): [onNewIntent] Not handling intent
E/OpenGLRenderer( 3621): Unable to match the desired swap behavior.

Is that really the behavior of Android? Because in IOS, when I clicked the push notification

flutter: 🚀 Open Message PushNotificationMessage {
"title": "test",
"body": "test",
"imageUrl": null,
"deeplinkUrl": null,
"goToUrl": null,
"fcmOptions": null,
"apnsOptions": {
"subTitle": null
},
"data": {}
}

So in Android the Open Message PushNotificationMessage is not triggered/listen or I was expecting that when you clicked the push message it will fetch the push payload via Open Message PushNotificationMessage. It only triggers the Foreground Message and Background Message, but not Open Message.

Did you add any in AndroidManifest aside from <uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>. ?

Thanks,
Mik

@github-actions github-actions bot added the pending-maintainer-response Pending response from a maintainer of this repository label Sep 17, 2024
@tyllark
Copy link
Member

tyllark commented Sep 18, 2024

Hi @gigamike,

For permissions <uses-permission android:name="android.permission.INTERNET"/> is the only permission I have in my AndroidManifest.xml. I do request push notification permissions at runtime via:

Amplify.Notifications.Push.requestPermissions(
      alert: true,
      badge: true,
      sound: true,
);

To clarify the intended functionality for both Android and iOS:

  • if the app is opened in foreground it will trigger onNotificationReceivedInForeground and no notification will be displayed, thus onNotificationOpened will not trigger.

  • If the app is in the background it will trigger onNotificationReceivedInBackground and display a notification to the user that will trigger onNotificationOpened when opened.

  • If the app is terminated it it will trigger onNotificationReceivedInBackground and display a notification to the user that will either trigger onNotificationOpened or set launchNotification when opened. You should handle both possibilities here since the OS may leave the app in background mode for after receiving a push notification.

Can you please:

  1. Confirm if launchNotification is being set incase you are launch the app from a terminated state.
  2. Confirm that you are not getting push notifications to click when receiving a foreground notification.

@github-actions github-actions bot removed the pending-maintainer-response Pending response from a maintainer of this repository label Sep 18, 2024
@gigamike
Copy link
Author

gigamike commented Sep 18, 2024

Hi @tyllark ,
Thanks for reply.

This one is not working on my end.
"If the app is in the background it will trigger onNotificationReceivedInBackground and display a notification to the user that will trigger onNotificationOpened when opened."

Yes it triggers onNotificationReceivedInBackground but when I click the push message it doesn't trigger the onNotificationOpened for Android only. For IOS it works. My goal is when users click the push message they will be redirected to a specific screen in my app depending on the push payload i.e. data

Confirm if launchNotification is being set incase you are launch the app from a terminated state.

  • Yes

Confirm that you are not getting push notifications to click when receiving a foreground notification.

  • Im getting the push notification and I can see logs for onNotificationReceivedInBackground. But when I click or open the push it doesn't return the payload.

Any ideas?

Thanks,
Mik

@github-actions github-actions bot added the pending-maintainer-response Pending response from a maintainer of this repository label Sep 18, 2024
@gigamike
Copy link
Author

gigamike commented Sep 19, 2024

Hi @tyllark ,

Ok, I figure it out. The problem was I'm using newer version of com.google.gms.google-services i.e.

plugins {
id "dev.flutter.flutter-plugin-loader" version "1.0.0"
id "com.android.application" version "7.3.0" apply false
id "org.jetbrains.kotlin.android" version "1.9.23" apply false
id("com.google.gms.google-services") version "4.3.2" apply false
}

because currently as of writing, Firebase messaging "Add Firebase to your Android app"
Screenshot 2024-09-19 at 8 23 29 AM

So I downgraded to 4.3.14

plugins {
id "dev.flutter.flutter-plugin-loader" version "1.0.0"
id "com.android.application" version "7.3.0" apply false
// id "org.jetbrains.kotlin.android" version "1.7.10" apply false
id "org.jetbrains.kotlin.android" version "1.9.23" apply false
id("com.google.gms.google-services") version "4.3.14" apply false
}

I/FA (18734): Application backgrounded at: timestamp_millis: 1726704920445
I/flutter (18734): 🚀 Background Message PushNotificationMessage {
I/flutter (18734): "title": "Mik Test title",
I/flutter (18734): "body": "Mik Test body please ignore",
I/flutter (18734): "imageUrl": null,
I/flutter (18734): "deeplinkUrl": null,
I/flutter (18734): "goToUrl": null,
I/flutter (18734): "fcmOptions": {
I/flutter (18734): "channelId": "PINPOINT.NOTIFICATION",
I/flutter (18734): "messageId": null
I/flutter (18734): },
I/flutter (18734): "apnsOptions": null,
I/flutter (18734): "data": {
I/flutter (18734): "pinpoint.openApp": "true",
I/flutter (18734): "pinpoint.notification.title": "Mik Test title",
I/flutter (18734): "pinpoint.notification.body": "Mik Test body please ignore",
I/flutter (18734): "type": "friend_request",
I/flutter (18734): "pinpoint.campaign.campaign_id": "_DIRECT",
I/flutter (18734): "pinpoint.notification.silentPush": "0"
I/flutter (18734): }
I/flutter (18734): }
D/AmplifyAuthCognitoPlugin(18734): [onNewIntent] Got intent: Intent { flg=0x10400000 cmp=au.movinghub.homehub/com.example.homehub.MainActivity (has extras) }
D/AmplifyAuthCognitoPlugin(18734): [onNewIntent] Not handling intent
I/flutter (18734): 🚀 Open Message PushNotificationMessage {
I/flutter (18734): "title": "Mik Test title",
I/flutter (18734): "body": "Mik Test body please ignore",
I/flutter (18734): "imageUrl": null,
I/flutter (18734): "deeplinkUrl": null,
I/flutter (18734): "goToUrl": null,
I/flutter (18734): "fcmOptions": {
I/flutter (18734): "channelId": "PINPOINT.NOTIFICATION",
I/flutter (18734): "messageId": null
I/flutter (18734): },
I/flutter (18734): "apnsOptions": null,
I/flutter (18734): "data": {
I/flutter (18734): "pinpoint.openApp": "true",
I/flutter (18734): "pinpoint.notification.title": "Mik Test title",
I/flutter (18734): "pinpoint.notification.body": "Mik Test body please ignore",
I/flutter (18734): "type": "friend_request",
I/flutter (18734): "pinpoint.campaign.campaign_id": "_DIRECT",
I/flutter (18734): "pinpoint.notification.silentPush": "0"
I/flutter (18734): }
I/flutter (18734): }

I can trigger now both Background Message and Open Message.

Hopefully they can fix the Flutter Amplify documentation i.e.
Screenshot 2024-09-19 at 8 27 19 AM

This doesn't work coz of the column symbol i.e. com.google.gms:google-services, it should be com.google.gms.google-services that is why I need to refer to Firebase docs which in them they have updated version

Thank you so much

@github-actions github-actions bot removed pending-triage This issue is in the backlog of issues to triage pending-maintainer-response Pending response from a maintainer of this repository labels Sep 19, 2024
Copy link

This issue is now closed. Comments on closed issues are hard for our team to see.
If you need more assistance, please open a new issue that references this one.

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

No branches or pull requests

3 participants