Push notifications received multiple times


#1

I’m having a strange issue with push notifications on Android using the nativescript-push-plugin

I can receive notifications fine but the same notification will continue to be received over and over again every few minutes. I can’t seem to find any similar issue so I’m a little stumped as to what could be causing this.

Perhaps I’m initialising the plugin incorrectly?

app.js

application.on(application.launchEvent, function(args) {
    pushViewModel.register();
});

application.on(application.resumeEvent, function (args) {
    // ## Handle push notification
    if (args.android) {
      var act = args.android;
      var intent = act.getIntent();
      var extras = intent.getExtras();
      if (extras) {
          // handle notification           
      }
    }
});

push-view-model.js (simplfied)

const pushSettings = {
    // Android settings
    senderID: "685272138395", // Android: Required setting with the sender/project number
    notificationCallbackAndroid: function callback(stringifiedData, fcmNotification) {
          // handle notification
    },

    // iOS settings
    badge: true, // Enable setting badge through Push Notification
    sound: true, // Enable playing a sound
    alert: true, // Enable creating a alert
    notificationCallbackIOS: function (pushNotification) {
        // handle notification
    }
};

function register() {
    pushPlugin.register(pushSettings, function (token) {
        postPushDeviceToken(token);

        bindCallbacks();
    }, function(error) {
        console.log('pushPlugin.register() error');
        console.log(error);
    });
}

function bindCallbacks() {

    if (pushPlugin.onMessageReceived) {
        pushPlugin.onMessageReceived(pushSettings.notificationCallbackAndroid);
    }

    if (pushPlugin.registerUserNotificationSettings) {
        pushPlugin.registerUserNotificationSettings(() => {
            console.log("Successfully registered for interactive push.");
        }, (err) => {
            console.log("Error registering for interactive push: " + JSON.stringify(err));
        });
    }
}

exports.register = register;
exports.bindCallbacks = bindCallbacks;

This definitely isn’t a problem with my server side code as the same problem exist when I send a single message to the Firebase api from Postman.

Should I be calling register() every time the app launches?
Or should register() only ever be called once?
Is it possible for the same app to be registered more than once with Firebase? And could this be the reason?


#2

Do you see the notifications multiple times in notification tray or it’s just the callback is called multiple times?


#3

Hi,

I have had this problem on multiple occasions and it occurs when you associate an event by code using the “on” method. When you associate an event with a method using this method, the reference remains in memory until the application finishes or you remove the reference manually, so if you re-register the event then you will associate the method again and therefore it will execute multiple times. Solution, the “on” method must be executed only once, per application life cycle.

I hope this will help.


#4

If the app is closed, the notification appears in the notification tray / status bar multiple times.
If the app is open the callback is called multiple times.


#5

I don’t think that is the cause in this case. The callbacks are only registered in the `launchEvent’ so they should only ever be bound once.


#6
  • If you send a notification from firebase console directly still you have the same issue?
  • Are you using the channels feature, like subscribing and sending notification to specific channel?

#7

same issue here i am using firebase for notification i check my callback of firebase notification its not calling multiple time …