Giter Site home page Giter Site logo

Comments (7)

re7eal avatar re7eal commented on July 24, 2024 16

I have the same issue. After digging around, I found that every notifications are reusing the same PendingIntent. That is why they pass the same data stored with intent.putExtra().

This solution solves the problem: https://stackoverflow.com/questions/7370324/notification-passes-old-intent-extras

For a quick fix, edit NotificationIntentAdapter.java from this

public static PendingIntent createPendingNotificationIntent(Context appContext, Intent intent, PushNotificationProps notification) {
    intent.putExtra(PUSH_NOTIFICATION_EXTRA_NAME, notification.asBundle());
    return PendingIntent.getService(appContext, PENDING_INTENT_CODE, intent, PendingIntent.FLAG_ONE_SHOT);
}

to this

public static PendingIntent createPendingNotificationIntent(Context appContext, Intent intent, PushNotificationProps notification) {
    intent.putExtra(PUSH_NOTIFICATION_EXTRA_NAME, notification.asBundle());
    int uniqueInt = (int) (System.currentTimeMillis() & 0xfffffff);
    return PendingIntent.getService(appContext, uniqueInt, intent, PendingIntent.FLAG_ONE_SHOT);
}

The concept is to pass a different "requestCode" for each notification.

With this, when having multiple notifications, PendingNotifications promise result will give the data for the notification that you pressed.

from react-native-notifications.

re7eal avatar re7eal commented on July 24, 2024 2

@swellingt0n You cannot fix the issue within JS code. Fortunately, you can override the PushNotification java class.

Follow these steps: https://github.com/wix/react-native-notifications/wiki/Android:-Layout-Customization

In MyPushNotification class, override getCTAPendingIntent() method.

public class MyPushNotification extends PushNotification {

    public MyPushNotification(Context context, Bundle bundle, AppLifecycleFacade appLifecycleFacade, AppLaunchHelper appLaunchHelper, JsIOHelper jsIoHelper) {
        super(context, bundle, appLifecycleFacade, appLaunchHelper, jsIoHelper);
    }

    @Override
    protected PendingIntent getCTAPendingIntent() {
        final Intent cta = new Intent(mContext, ProxyService.class);
        return CustomNotificationIntentAdapter.createPendingNotificationIntent(mContext, cta, mNotificationProps);
    }
}

CustomNotificationIntentAdapter class (copy over from the original NotificationIntentAdapter.java and modify createPendingNotificationIntent())

public class CustomNotificationIntentAdapter {
    private static final String PUSH_NOTIFICATION_EXTRA_NAME = "pushNotification";

    public static PendingIntent createPendingNotificationIntent(Context appContext, Intent intent, PushNotificationProps notification) {
        intent.putExtra(PUSH_NOTIFICATION_EXTRA_NAME, notification.asBundle());
        int uniqueInt = (int) (System.currentTimeMillis() & 0xfffffff);
        return PendingIntent.getService(appContext, uniqueInt, intent, PendingIntent.FLAG_ONE_SHOT);
    }
}

from react-native-notifications.

cordiosd avatar cordiosd commented on July 24, 2024 1

You may want to read the first paragraphs of the following page before implementing this.

https://developer.android.com/reference/android/app/PendingIntent

From this page "A common mistake people make is to create multiple PendingIntent objects with Intents that only vary in their "extra" contents, expecting to get a different PendingIntent each time. This does not happen. The parts of the Intent that are used for matching are the same ones defined by Intent#filterEquals(Intent). If you use two Intent objects that are equivalent as per Intent#filterEquals(Intent), then you will get the same PendingIntent for both of them."

Also "A PendingIntent itself is simply a reference to a token maintained by the system describing the original data used to retrieve it. This means that, even if its owning application's process is killed, the PendingIntent itself will remain usable from other processes that have been given it. If the creating application later re-retrieves the same kind of PendingIntent (same operation, same Intent action, data, categories, and components, and same flags), it will receive a PendingIntent representing the same token if that is still valid, and can thus call cancel() to remove it."

My interpretation of this is that your android application is holding onto every "unique" pending intent combination you ever created without cancelling it. This is true even if your applications process is killed and restarted. It may be the case that android has some way of killing the older pending intents after some period of time. I do not know if it does or does not.

from react-native-notifications.

jeffmwells avatar jeffmwells commented on July 24, 2024

@re7eal Thanks for the insightful response! Is there any way to achieve this within the JS code, or is the only alternative to override the class within the android/ folder of the project, to avoid issues with updates to the node_modules content?

from react-native-notifications.

jeffmwells avatar jeffmwells commented on July 24, 2024

@re7eal Thank you, closing this for now.

from react-native-notifications.

jeffmwells avatar jeffmwells commented on July 24, 2024

Wanted to leave an update for anyone with this issue, finally got around to implementing @re7eal 's solution and it worked perfectly.

from react-native-notifications.

mehtaketan7 avatar mehtaketan7 commented on July 24, 2024

Thanks, @re7eal perfect solution!

from react-native-notifications.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.