braze-inc / braze-flutter-sdk

Public repo for the Braze Flutter SDK
Other
15 stars 31 forks source link

Payload not received when opening the app from a terminated state in Android. #77

Open Neelansh-ns opened 1 month ago

Neelansh-ns commented 1 month ago

Actions performed:

App opened -> App sent to Inactive state by back press -> Notification sent -> Notification clicked to open the app.

These are the event logs:

21:46:57.849  W  Neelansh activeplugin added
21:46:57.923  I  Running Flutter BrazePlugin automatic initialization
21:46:57.985  I  Firebase Cloud Messaging found. Setting up Firebase Cloud Messaging.
21:46:57.989  I  Automatic ADM registration not enabled in configuration. Braze will not register for ADM.
21:46:58.196  I  Geofences implicitly disabled via server configuration.
21:46:58.196  I  ***Geofence API not found. Please include the android-sdk-location module***
21:46:58.199  I  ***Location API not found. Please include android-sdk-location module***
21:46:58.319  I  Feature flags not enabled. Not refreshing feature flags.
21:46:58.323  I  Session [ed809059-1382-4c2b-8c25-aa522e6db429] being sealed because its end time is over the grace period. Session: 
                 MutableSession(sessionId=ed809059-1382-4c2b-8c25-aa522e6db429, startTime=1.719245549692E9, endTime=1.719245549709E9, isSealed=false, duration=0)
21:46:58.338  I  Event was published, but no subscribers were found. Saving event for later publishing to a matching subscriber. Event class: class com.braze.events.SessionStateChangedEvent this bo.app.fv@f0bf0d6
21:46:58.338  I  New session created with ID: 1e3eb1f8-222f-4f0a-9bd1-f55fde75392d
21:46:58.357  I  Event was published, but no subscribers were found. Saving event for later publishing to a matching subscriber. Event class: class com.braze.events.SessionStateChangedEvent this bo.app.fv@f0bf0d6
21:46:58.368  I  Completed the openSession call. Starting or continuing session 1e3eb1f8-222f-4f0a-9bd1-f55fde75392d
21:46:59.843  I  Updating offline feed for user with id: 4f89addd-a5d5-4c32-ba55-0a29b6a1ad84
21:46:59.846  I  Event was published, but no subscribers were found. Saving event for later publishing to a matching subscriber. Event class: class com.braze.events.FeedUpdatedEvent this bo.app.fv@f0bf0d6
21:46:59.852  I  Event was published, but no subscribers were found. But not saving event for publishing later. Event class: class com.braze.events.NoMatchingTriggerEvent bo.app.fv@f0bf0d6
21:47:00.252  W  Tried to confirm outboundObject [bo.app.ep@1ece871] with success [true], but the cache wasn't locked, so not doing anything.
21:47:00.532  I  Braze feature flags subscription not present. Removing any queued flags and adding only the recent refresh.
21:48:00.232  W  Neelansh onDetachedFromEngine
21:48:07.556  I  Got remote message from FCM: {a=Test message 2, p=0, t=Test title, _ab=true, cid=dGVzdF90ZXN0X2RpPTY2Nzk5YzNlZjJiNGZjNTI4MTNhM2EyMDg3OTdiY2Zh, ab_li=https://bellard.org/bpg/2.png, ab_nc=invideo_ai_video_generation, extra={"appboy_image_url":"https://bellard.org/bpg/2.png"}}
21:48:07.565  I  Received broadcast message. Message: Intent { act=firebase_messaging_service_routing_action (has extras) }
21:48:07.581  I  Push message payload received: Bundle[{a=Test message 2, p=0, t=Test title, _ab=true, cid=dGVzdF90ZXN0X2RpPTY2Nzk5YzNlZjJiNGZjNTI4MTNhM2EyMDg3OTdiY2Zh, ab_li=https://bellard.org/bpg/2.png, ab_nc=invideo_ai_video_generation, extra={"appboy_image_url":"https://bellard.org/bpg/2.png"}}]
21:48:07.644  W  There are no active Braze Plugins. Not calling 'handleBrazePushNotificationEvent'.
21:48:10.182  I  Session [1e3eb1f8-222f-4f0a-9bd1-f55fde75392d] being sealed because its end time is over the grace period. Session: 
                 MutableSession(sessionId=1e3eb1f8-222f-4f0a-9bd1-f55fde75392d, startTime=1.719245818338E9, endTime=1.719245880154E9, isSealed=false, duration=61)
21:48:10.215  I  Event was published, but no subscribers were found. Saving event for later publishing to a matching subscriber. Event class: class com.braze.events.SessionStateChangedEvent this bo.app.fv@f0bf0d6
21:48:33.072  I  Received broadcast message. Message: Intent { act=com.braze.action.BRAZE_PUSH_CLICKED cmp=io.invideo.ai/com.braze.push.BrazePushReceiver (has extras) }
21:48:33.079  I  Logging push click. Campaign Id: dGVzdF90ZXN0X2RpPTY2Nzk5YzNlZjJiNGZjNTI4MTNhM2EyMDg3OTdiY2Zh
21:48:33.079  W  There are no active Braze Plugins. Not calling 'handleBrazePushNotificationEvent'.
21:48:33.082  I  Requesting trigger update due to trigger-eligible push click event
21:48:33.262  W  Neelansh onAttachedToEngine
21:48:33.262  W  Neelansh activeplugin added
21:48:33.378  I  New session created with ID: c50e6c37-564b-4728-8742-5f50dfd55960
21:48:33.383  I  Event was published, but no subscribers were found. Saving event for later publishing to a matching subscriber. Event class: class com.braze.events.SessionStateChangedEvent this bo.app.fv@f0bf0d6
21:48:33.412  I  Completed the openSession call. Starting or continuing session c50e6c37-564b-4728-8742-5f50dfd55960
21:48:34.142  I  Updating offline feed for user with id: 4f89addd-a5d5-4c32-ba55-0a29b6a1ad84
21:48:34.143  I  Event was published, but no subscribers were found. Saving event for later publishing to a matching subscriber. Event class: class com.braze.events.FeedUpdatedEvent this bo.app.fv@f0bf0d6
21:48:34.150  I  Event was published, but no subscribers were found. But not saving event for publishing later. Event class: class com.braze.events.NoMatchingTriggerEvent bo.app.fv@f0bf0d6

PS: The Warning logs are added by me for debugging in the respective functions in the plugin.

How to receive the payload in the app, after the notification is tapped from inactive/terminated state. This works fine in iOS as I get the payload in the subscribeToPushNotificationEvents listener. The same is not happening in Android.

From the logs, I can see that event is published before the activePlugin is set.

vanessaland commented 1 month ago

Hi @Neelansh-ns ,

Thanks for reaching out! Here are some things you can try to help resolve your issue:

If you continue to run into issues, please contact our Support Team by emailing support@braze.com. They will be able to help with your specific integration. In your email, please include:

Thanks!

Neelansh-ns commented 1 month ago

I'm able to solve this by adding a slight delay in the processPushNotificationEvent function, as there is a race condition happening here. The BrazePushReceiver sends out click event as soon as the notification is tapped.

2024-06-25 02:32:53.655 12273-12273 Braze v30....shReceiver io.invideo.ai                        I  Received broadcast message. Message: Intent { act=com.braze.action.BRAZE_PUSH_CLICKED cmp=io.invideo.ai/com.braze.push.BrazePushReceiver (has extras) }
2024-06-25 02:32:53.658 12273-12381 Braze v30.4.0 .Braze    io.invideo.ai                        I  Logging push click. Campaign Id: dGVzdF90ZXN0X2RpPTY2NzlkZWVjZGFlZWI4MzFlYjNmMGE0Yzc1YTVhN2Jj

And then the onAttachedToEngine is called which initializes the BrazeSDK.

2024-06-25 02:32:53.843 12273-12273 Braze v30....razePlugin io.invideo.ai                        W  Neelansh onAttachedToEngine
2024-06-25 02:32:53.843 12273-12273 Braze v30....razePlugin io.invideo.ai                        W  Neelansh activeplugin added

By adding a few milliseconds delay in the processPushNotificationEvent, I get the correct notification payload on calling subscribeToPushNotificationEvents from the dart side.

Neelansh-ns commented 1 month ago

Please refer to this customized Plugin file, where I have added logs. I have used the approach of adding a delay of 200ms to validate the bug.

Adding the delay fixes the bug. https://gist.github.com/Neelansh-ns/2e862e3f7522d78081c99d0ee36d7f37

Neelansh-ns commented 1 month ago

I've opened a PR for the same here, where I'm storing the pending events while an activity gets attached. Please review: #78