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.

Events triggers multiple times. - Continuation of Issue 50

See original GitHub issue

It is a continuation of issue 50. URL - https://github.com/CrossGeeks/FirebasePushNotificationPlugin/issues/50

My code setup is same as explained in issue 50.

I have register notification events in App class constructor. These events trigger multiple times on successive notification clicks. As these events are register every time I click on notification, it calls App class constructor using LoadApplication method in MainActivity in Xamarin.Android. I have tried removing registered event and then register again but still event called multiple times. 😦

Below code I tried but did not work.

public App()
        {
            InitializeComponent();

            MainPage = new MainPage();

            try
            {
                CrossFirebasePushNotification.Current.OnTokenRefresh -= FCM_OnTokenRefresh;
                CrossFirebasePushNotification.Current.OnTokenRefresh += FCM_OnTokenRefresh;
                CrossFirebasePushNotification.Current.OnNotificationReceived -= FCM_OnNotificationReceived;
                CrossFirebasePushNotification.Current.OnNotificationReceived += FCM_OnNotificationReceived;
                CrossFirebasePushNotification.Current.OnNotificationOpened -= FCM_OnNotificationOpened;
                CrossFirebasePushNotification.Current.OnNotificationOpened += FCM_OnNotificationOpened;
            }
            catch (Exception ex)
            {
                var msg = ex.Message;
            }


        }

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:13 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
abraricommented, Apr 30, 2018

Same here on Android, using the latest version of the plugin and all of the above workarounds.

Steps to reproduce:

  1. Open app (first open)
  2. Press back button, app exited
  3. Reopen the app (second open)
  4. Send a notification
  5. Open the notification, the OnNotificationOpened event will be fired twice

If I exited and reopened the app for the third time, the OnNotificationOpened event will be fired three times. The number of app reopen is the number of the event fired.

3reactions
collaorodrigo7commented, Nov 6, 2017

@ravikanasagra1 I do not know if you noticed they reference this issue on issue 14 In case you have not seen it and to help you is not you taping multiple times 😃 , what happens is that everytime you tap the notification a new instance of App gets created handling the event again, and that will happen everytime you open a new one. If you notice the more notifications you open, the more times you will see your event reaching.
This is the solution:

  1. Is better to handle the events overriding the OnStart() method on App.xaml.cs
   protected override void OnStart()
   {
            // Handle when your app starts
            CrossFirebasePushNotification.Current.OnTokenRefresh += (s, p) =>{ //handle here };

            CrossFirebasePushNotification.Current.OnNotificationReceived += (s, p) =>{//handle here};
            CrossFirebasePushNotification.Current.OnNotificationOpened += async (s, p) =>{//handle here};
  }
  1. As claudiocleberson explains on issue 14 , you should Put the LaunchMode = LaunchMode.SingleTop in the MainActivity class. That should do the trick. That means modify the line on top your main activity class like this:
[Activity(LaunchMode = LaunchMode.SingleTop, Label = "MainAuthApp", Icon = "@drawable/icon", Theme = "@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
    public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity{...}
  1. Add this to your MainActivity.cs
using Android.Content;
  1. Just like claudiocleberson explains later on #14 , Override OnNewIntent method on MainActivity
/// Called when the app is in the background and a notification is clicked on (also called each time the app is minimized and the brought back up), a new Intent is created
        /// and sent out, since we use LaunchMode set to SingleTop this method is called instead of the app being restarted.
        ///
        /// The Intent that was set when the call was made. If started from a notification click, extra string values can be extracted.
        protected override void OnNewIntent(Intent intent)
        {
            base.OnNewIntent(intent);
            FirebasePushNotificationManager.ProcessIntent(intent);
        }

I was having the same issues than you and I was able to fix it by following those steps. Let me know if it fixes the your problem or if you need more help. If you had already figured it out, hope it helps someone else.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Solved: A Event is created multiple times though it is onl...
A Event is created multiple times though it is only supposed to do it once ... The flow is triggered when an event...
Read more >
jQuery click events firing multiple times - javascript
I'm attempting to write a video poker game in Javascript as a way of getting the basics of it down, and I've run...
Read more >
Triggers - AWS Glue
Batch condition that must be met (specified number of events received or batch time window expired) before EventBridge event trigger fires. Predicate structure....
Read more >
Using Trigger Exceptions In Google Tag Manager
Multiple Triggers · A Tag will fire if ANY of the Triggers evaluate to TRUE. · A Tag will only fire ONCE per...
Read more >
How a web session is defined in Universal Analytics
The reason is that Sessions is not incremented in cases where a session only consists of non-interaction events. In contrast, New Users is...
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