Skip to content
This repository was archived by the owner on Jan 14, 2025. It is now read-only.

Commit 359aeb0

Browse files
author
Boris Tacyniak
authored
Merge pull request #1819 from cayleyh/intent-handling-hooks
[Android] Add hooks to intent handling and bundle parsing
2 parents 2682658 + c052494 commit 359aeb0

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

README.md

+43
Original file line numberDiff line numberDiff line change
@@ -702,6 +702,49 @@ Uses the [ShortcutBadger](https://github.com/leolin310148/ShortcutBadger) on And
702702

703703
`PushNotification.unsubscribeFromTopic(topic: string)` Unsubscribe from a topic (works only with Firebase)
704704

705+
## Android Custom Notification Handling
706+
707+
Unlike iOS, Android apps handle the creation of their own notifications. React Native Push Notifications does a "best guess" to create and handle incoming notifications. However, when using 3rd party notification platforms and tools, the initial notification creation process may need to be customized.
708+
709+
### Customizing Notification Creation
710+
711+
If your notification service uses a custom data payload format, React Native Push Notifications will not be able to parse the data correctly to create an initial notification.
712+
713+
For these cases, you should:
714+
715+
1. Remove the intent handler configuration for React Native Push Notifications from your `android/app/src/main/AndroidManifest.xml`.
716+
2. Implement initial notification creation as per the instructions from your Provider.
717+
718+
### Handling Custom Payloads
719+
720+
Data payloads of notifications from 3rd party services may not match the format expected by React Native Push Notification. When tapped, these notifications will not pass the details and data to the `onNotification()` event handler. Custom `IntentHandlers` allow you to fix this so that correct `notification` objects are sent to your `onNotification()` method.
721+
722+
Custom handlers are added in Application init or `MainActivity.onCreate()` methods:
723+
724+
```
725+
RNPushNotification.IntentHandlers.add(new RNPushNotification.RNIntentHandler() {
726+
@Override
727+
public void onNewIntent(Intent intent) {
728+
// If your provider requires some parsing on the intent before the data can be
729+
// used, add that code here. Otherwise leave empty.
730+
}
731+
732+
@Nullable
733+
@Override
734+
public Bundle getBundleFromIntent(Intent intent) {
735+
// This should return the bundle data that will be serialized to the `notification.data`
736+
// property sent to the `onNotification()` handler. Return `null` if there is no data
737+
// or this is not an intent from your provider.
738+
739+
// Example:
740+
if (intent.hasExtra("MY_NOTIFICATION_PROVIDER_DATA_KEY")) {
741+
return intent.getBundleExtra("MY_NOTIFICATION_PROVIDER_DATA_KEY");
742+
}
743+
return null;
744+
}
745+
});
746+
```
747+
705748
## Checking Notification Permissions
706749

707750
`PushNotification.checkPermissions(callback: Function)` Check permissions

android/src/main/java/com/dieam/reactnativepushnotification/modules/RNPushNotification.java

+21
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import android.content.IntentFilter;
1010
import android.os.Bundle;
1111
import androidx.annotation.NonNull;
12+
import androidx.annotation.Nullable;
1213
import androidx.core.app.NotificationManagerCompat;
1314

1415
import com.dieam.reactnativepushnotification.helpers.ApplicationBadgeHelper;
@@ -27,6 +28,7 @@
2728

2829
import java.io.IOException;
2930
import java.security.SecureRandom;
31+
import java.util.ArrayList;
3032
import java.util.HashMap;
3133
import java.util.Map;
3234

@@ -42,6 +44,15 @@ public class RNPushNotification extends ReactContextBaseJavaModule implements Ac
4244
public static final String LOG_TAG = "RNPushNotification";// all logging should use this tag
4345
public static final String KEY_TEXT_REPLY = "key_text_reply";
4446

47+
public interface RNIntentHandler {
48+
void onNewIntent(Intent intent);
49+
50+
@Nullable
51+
Bundle getBundleFromIntent(Intent intent);
52+
}
53+
54+
public static ArrayList<RNIntentHandler> IntentHandlers = new ArrayList();
55+
4556
private RNPushNotificationHelper mRNPushNotificationHelper;
4657
private final SecureRandom mRandomNumberGenerator = new SecureRandom();
4758
private RNPushNotificationJsDelivery mJsDelivery;
@@ -81,6 +92,12 @@ private Bundle getBundleFromIntent(Intent intent) {
8192
bundle.putBundle("data", intent.getExtras());
8293
}
8394

95+
if (bundle == null) {
96+
for (RNIntentHandler handler : IntentHandlers) {
97+
bundle = handler.getBundleFromIntent(intent);
98+
}
99+
}
100+
84101
if(null != bundle && !bundle.getBoolean("foreground", false) && !bundle.containsKey("userInteraction")) {
85102
bundle.putBoolean("userInteraction", true);
86103
}
@@ -90,6 +107,10 @@ private Bundle getBundleFromIntent(Intent intent) {
90107

91108
@Override
92109
public void onNewIntent(Intent intent) {
110+
for (RNIntentHandler handler : IntentHandlers) {
111+
handler.onNewIntent(intent);
112+
}
113+
93114
Bundle bundle = this.getBundleFromIntent(intent);
94115
if (bundle != null) {
95116
mJsDelivery.notifyNotification(bundle);

0 commit comments

Comments
 (0)