About local notification and deep link
Question
Hi, today we tried to show a local notification (PushNotification.localNotification) with deep link attached, but we cannot find an option at https://github.com/zo0r/react-native-push-notification#local-notifications for that. Do we support attaching a link to the local notification? In our case, the notification should appear and open-deep-link-on-click in all cases of app foreground, background and killed. It would be great if somebody can help pointing a way to achieve that.
Thank you and best regards!
Hi @thongncvn,
If I can understand your question correctly, I think you need the user to open a deep link upon clicking on the notification right?
If so, luckily I did this not so while ago. What I did was, I attached the deep link (in my case it is just a website URL) as the message body (you can attach this as anything such as bigText, subText etc. Then what you have to do is, use the onNotification method in PushNotification.configure to catch this and open the deep link using,
const supported = await Linking.canOpenURL(url);
if (supported) {
await Linking.openURL(url);
}
I think by doing this, you can handle all kinds of notifications such as background notifications, foreground notifications, etc.
On second thought, you can specify notification actions to specifically do this deep linking stuff and let the on notification click intent to handle something else. If you need further clarifications, please let me know. 🤞
Thanks @Niweera for very detailed and helpful answer! We managed to make it work, thanks to your guide. However - just sharing here because the problem was deeper than it looks - because in our code MainActivity is not the launch activity :joy:
We didn't understand why onNotification didn't work. It turns out we have an activity SplashActivity that lies before everything; after reviewing the code of react-native-push-notification we saw this method:
public Class getMainActivityClass() {
String packageName = context.getPackageName();
Intent launchIntent = context.getPackageManager().getLaunchIntentForPackage(packageName);
String className = launchIntent.getComponent().getClassName();
try {
return Class.forName(className);
} catch (ClassNotFoundException e) {
e.printStackTrace();
return null;
}
}
So clearly this implies that the launch activity is MainActivity. Then we've decided to temporary patch react-native-push-notification with the following:
diff --git a/node_modules/react-native-push-notification/android/src/main/java/com/dieam/reactnativepushnotification/modules/RNPushNotificationHelper.java b/node_modules/react-native-push-notification/android/src/main/java/com/dieam/reactnativepushnotification/modules/RNPushNotificationHelper.java
index dc6c674..47a425d 100644
--- a/node_modules/react-native-push-notification/android/src/main/java/com/dieam/reactnativepushnotification/modules/RNPushNotificationHelper.java
+++ b/node_modules/react-native-push-notification/android/src/main/java/com/dieam/reactnativepushnotification/modules/RNPushNotificationHelper.java
@@ -406,7 +406,7 @@ public class RNPushNotificationHelper {
notification.setStyle(style);
- Intent intent = new Intent(context, intentClass);
+ Intent intent = new Intent(context, Class.forName("com.ourapp.MainActivity"));
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
bundle.putBoolean("foreground", this.isApplicationInForeground());
bundle.putBoolean("userInteraction", true);
Then onNotification continues to work.
Hi @thongncvn, I'm glad I could be of help. 🙏
Hi @thongncvn There is a way to avoid this change in the library. It's documented in the readme and in issues.
https://github.com/zo0r/react-native-push-notification/issues/1976#issuecomment-835367990
This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 30 days if no further activity occurs. Thank you for your contributions.