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: Handling received push notifications (in app's foreground/background state) #1801

Closed
robertgreso opened this issue Jan 1, 2021 · 14 comments

Comments

@robertgreso
Copy link

According to docs, onNotification is "Called when a remote is received or opened". I would like to ask, if its possible to somehow get the behaviour that onNotification would be on Android called once push notification is opened, not just once its received (when Android app is in foreground/background)?

When implemented on iOS, onNotification is always called just when I click on received push notification, not immediately once its received. I would like to get the same behaviour on Android as well but it behaves differently there.
On Android, if the app is in the foreground/background onNotification is called immediately once the remote notification is received, not when user actually clicks on received notification.

Is it possible to get the same behaviour as described above on iOS also for Android, ie to be able to handle received push notification on Android while the app is in foreground/background just when user opens received push notification, not just when the notification is received?

Thanks for any suggestions!

@Dallas62
Copy link
Collaborator

Dallas62 commented Jan 1, 2021

Hi @robertgreso

This question has already been answered in the last ten issues.

Regards,

@robertgreso
Copy link
Author

Hi @robertgreso

This question has already been answered in the last ten issues.

Regards,

Hi @Dallas62, I checked all the issues before creating this one and also double checked now but couldnt find the answer how to make it work to get onNotification called on Android once user taps on notification, when in foreground/background.

I just found as you pointed someone else to troubleshooting page where it says "Tapping the alert in the notification centre will start or bring the app to the foreground and onNotification will be called.", so I guess onNotification should be called in the case I described but it is not working for me.
Could you please help me here? Much appreciate it! Thank you!

@Dallas62
Copy link
Collaborator

Dallas62 commented Jan 2, 2021

#1795 (comment)

@robertgreso
Copy link
Author

Hi @Dallas62 !

Thanks for quick response! 


I checked that comment and based on it: 
We use the newest 7.0.0 version and we call .configure() outside of any component.


 

I searched for SplashScreen in the issues (https://github.com/zo0r/react-native-push-notification/issues?q=is%3Aissue+splashscreen). 
The relevant issues I found which are about push notifications:



#1795
- this one is just about killed state where I have issue with foreground/backgroud state, killed state works fine for me

#1592
- we dont use react native splash screen in our app

#868
- we do send only data key in payload so that should be fine

I wasnt able to find any info in this issue that would help me to fix the issue I am having. Do you have any idea what could be the root of the issue? I would really appreciate your help.
Thank you!

@robertgreso
Copy link
Author

@Dallas62 any idea what could be wrong? Would appreciate any help thanks!

@Dallas62
Copy link
Collaborator

Dallas62 commented Jan 7, 2021

Hi @robertgreso
Can you share more information ?

  • AndroidManifest
  • Notification Payload
  • Reproduction project ?

I'm a not sure what's the problem, there is many cases listed (notification-only and data-only).
Also on the first case:

I would like to ask, if its possible to somehow get the behaviour that onNotification would be on Android called once push notification is opened, not just once its received (when Android app is in foreground/background)?

The userInteraction props is used to know if the user pressed the notification, so you should be able to skip "on notification received". And if the notification is not received on press, it's probably due to something intercepting the notification / Android Intent.

@robertgreso
Copy link
Author

Hi @Dallas62,

unfortunately I am not sure if I can share with you here AndroidManifest or whole project as this is a project that whole company is based on and already in stores for many years so some info there is confidential but here is part of AndroidManifest that could be relevant to this issue:

<application>
.
.
.
 <meta-data  android:name="com.dieam.reactnativepushnotification.notification_foreground"
        android:value="false"/>
 <meta-data  android:name="com.dieam.reactnativepushnotification.notification_color"
        android:resource=" @android:color/white"/>
 <receiver android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationActions" />
 <receiver android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationPublisher" />
 <receiver android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationBootEventReceiver">
        <intent-filter>
          <action android:name="android.intent.action.BOOT_COMPLETED" />
          <action android:name="android.intent.action.QUICKBOOT_POWERON" />
          <action android:name="com.htc.intent.action.QUICKBOOT_POWERON"/>
        </intent-filter> 
 </receiver>

 <service
        android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationListenerService"
        android:exported="false" >
        <intent-filter>
          <action android:name="com.google.firebase.MESSAGING_EVENT" />
        </intent-filter>
 </service>
.
.
.

</application>

And here is example of the notification payload:

{"data":{"type":"testType","payload":{"id":"testId","name":"Test Name"}},"evt":{"loc":"test location"}}

About that userInteraction props, yes I am getting it, when the Android app is in background onNotification is called immediately once the push notification is received, with userInteraction being false. That works fine.

What I would love to achieve here is to get onNotification called once user clicks on received notification (received while Android app being in the background). This is how it works on iOS for me.
Do you think that should be possible to achieve somehow? Is this intended behaviour for Android while app is being in the background? Or is it possible on iOS but not on Android?

Thank you for all your help!

@Dallas62
Copy link
Collaborator

Dallas62 commented Jan 7, 2021

The problem is that I can't hep without at least a minimal reproduction case.

Did you try with the exemple project ?

If you don't have the notification triggered on user pressed, it's because you have something which intercept the Android Intent. (SplashScreen, SplashActivity, Activity before the React Activity).

There is a full issue about it which is not listed in the current issue:
#1378

Regards,

@cayleyh
Copy link
Contributor

cayleyh commented Jan 13, 2021

@Dallas62 I'm helping @robertgreso look into this. We're using a service called Batch to manage our push campaigns in app. It looks like the push notifications that Batch sends are non-standard or differ from what react-native-push-notification is expecting.

When a push is received, react-native-push-notification, I'm seeing that it gets received correctly in handleRemotePushNotification(), but creating the notification ultimately fails in sendToNotificationCentreWithPicture() because the payload format sent by Batch does not have a message param in the bundle, which causes an early exit without a notification getting created.

Where the confusion was happening was this: we include the Batch SDK in order to send user preferences and subscription settings to Batch so that notifications can be customized using their targeting tools. This was also creating a notification, so it wasn't clear that the standard react-native-push-notification code was failing.

This is also why onNotification() is never called when the notification is tapped -- because it's a Batch notification, not a react-native-push-notification created notification.

Any tips on how to handle this case with a Push notification provider? Is it possible for us to trigger the onNotification() method manually so we can still have a single JS codepath for handling these?

@Dallas62
Copy link
Collaborator

Hi @cayleyh
Just to be sure to fully understand:

  • Does the application display correctly the notification ? I guess it's ok since your are able to press it.
  • Does Batch SDK provide a way to get the "initial notification" ?

For the first one, if it's triggering onNotification, you can ignore the notification with:

onNotification(notification) {
  if(!notification.userInteraction) {
    return;
  }
}

If Batch SDK doesn't provide a way to the "initial notification" you can:

  • Contact their support since it's a paid service and not a cheap one.
  • Debug the application to know what's the content of the Intent. The SDK is not open source and I don't use this service, so I will not be able to help more on my side.

To be able to handle the initial notification, you need to "fix" this function:
https://github.com/zo0r/react-native-push-notification/blob/master/android/src/main/java/com/dieam/reactnativepushnotification/modules/RNPushNotification.java#L74

They should have put data inside the Extra of the Intent, using AndroidStudio with the debugger, you should be able to find the right key, then you can post the result here for a futur fix.

Regards,

@cayleyh
Copy link
Contributor

cayleyh commented Jan 13, 2021

Hey @Dallas62

Yes we get the notification created correctly if I disable the react-native-push-notification intent handling in AndroidManifest.xml and just let the Batch library take care of notification creation.

I went down the same path you've outlined on how to fix this. The solution I came up with was to add some extensible hooks to customize the new intent & bundle parsing behaviours generically: #1819.

Then in our code I have the stuff that is specific to our app / Batch. In our MainActivity onCreate():

RNPushNotification.IntentHandlers.add(new RNPushNotification.RNIntentHandler() {
  @Override
  public void onNewIntent(Intent intent) {
    // Batch includes this method to inspect & prepare Batch-based notification intents
    Batch.onNewIntent(self, intent);
  }

  @Nullable
  @Override
  public Bundle getBundleFromIntent(Intent intent) {
    // Handle the Batch-specific payload format
    if (intent.hasExtra(Batch.Push.PAYLOAD_KEY)) {
      return intent.getBundleExtra(Batch.Push.PAYLOAD_KEY);
    }
    return null;
  }
});

In the PR I added the hooks as class statics because it was the simplest way to show how this approach would work. Happy for any input on what you think of including this type fo customization approach in general, and the details of the implementation.

@cayleyh
Copy link
Contributor

cayleyh commented Jan 14, 2021

I should also add: it might be possible to the intent parsing stuff solely in my activity and then have RNPushNotification work correctly based on that, but I'm not very familiar with how the order of intent handling happens with onNewIntent, and so felt this was a safer way to customize how RNPushNotification handles incoming intents & bundle parsing.

@Dallas62
Copy link
Collaborator

Hi @cayleyh @robertgreso

You can find the fix of #1819 inside the version 7.1.0

Regards,

@cayleyh
Copy link
Contributor

cayleyh commented Jan 26, 2021

Great thanks @Dallas62!

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

3 participants