question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

About local notification and deep link

See original GitHub issue

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!

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:1
  • Comments:6

github_iconTop GitHub Comments

2reactions
thongncvncommented, Jul 28, 2021

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 😂

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.

1reaction
Dallas62commented, Sep 3, 2021

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

How do I implement deep links in local notification
You can use DeepLinkKit for navigation to different screens. I'm using this library in my app, I have about 30-37 deeplinks for every...
Read more >
URLs, Links and Deep Links - OneSignal Documentation
This guide explains how to open to a specific page of your app or website using push notifications. For options using Email or...
Read more >
Organizing your app to handle deeplinks and push notifications
This article presents a way to organize in the same module the code for handling deeplinks and the code for handling push notifications....
Read more >
Scheduling and Handling Local Notifications - Apple Developer
Local notifications give you a way to alert the user at times when your app might not be running. You schedule local notifications...
Read more >
Deep Links in Push Notifications - Iterable Support Center
A deep link is a URI that links to a specific location within your mobile app. The following sections describe how to work...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found