push-plugin icon indicating copy to clipboard operation
push-plugin copied to clipboard

onMessageReceived callback does not triggered on Android

Open noetius opened this issue 8 years ago • 7 comments

Hello, I tried everything to get working the "onMessageReceived" callback:

pushPlugin.areNotificationsEnabled(function(areEnabled) {
    if(areEnabled){
        pushPlugin.register(config.pushPluginConfig, function(token) { 
            onReceivedToken(token);         
      });
      pushPlugin.onTokenRefresh( onReceivedToken );
      pushPlugin.onMessageReceived(function callback(message, data, notification) {
          console.dir(message);
      });            
    }
});

The others callbacks like "areNotificationEnabled", "register" and "onTokenRefresh" work quite good, but I'm not able to understand how to get working the "onMessageReceived" callback, because every time I send a notification from the FireBase console, it simply doesn't trigger, even if I place it inside the "register" callback.

Instead, the "addOnMessageReceivedCallback" from the firebase plugin works smoothly, but I would like to rely on the Push Plugin callback just not to mix too many approaches together.

Hope there is someone with my same issue.

noetius avatar Mar 17 '17 14:03 noetius

@noetius My apologies for the delayed response.

My feeling here is that the case is not enough isolated so I cannot be more specific that suggesting to check the following:

  1. You are utilizing the latest push plugin version (tailored to work with the Firebase SDK )
  2. You have registered the callback, e.g. it is available in the app scope
  3. You are checking an existing parameter of the callback, message is not always available, use a simple console.log to see if the callback is triggered
  4. You are experimenting with background and foreground states - what is the difference, is the notification received at all?
  5. What is the payload - do you have data and notification keys in the payload?
  6. Do you use the firebase plugin simulatenously with this one?

AntonDobrev avatar Mar 29 '17 11:03 AntonDobrev

Hi @AntonDobrev, don't worry for that, and thanks for your reply :)

So I was able to solve my issue simply by removing the firebase plugin, because I didn't know that the plugin cannot working if there's also the firebase plugin installed.

Probably is reported in the docs, but I don't know where, otherwise, I think it would be really useful to read it somewhere, in case of necessity.

noetius avatar Mar 29 '17 12:03 noetius

Hi all,

I prefer to use the same thread rather than creating new issue. The title and description are very close to what I experience.

So here is my problem. When I receive a push notification on my Android emulator and I tap on it, onMessageReceived callback is not executed. This callback is executed only when the application is active (foreground) and I get push notification.

The callback is not executed even if I open the app from the task manager (the square device button).

Problem is not reproduced on iOS. When testing on real iOS device (iOS simulator cannot register for push notifications and that's fabric), the notificationCallbackIOS is always executed when push notification is received and:

  • app is in foreground
  • app is running in background or is killed and I tap on the push notification
  • app is in background and I open it from task manager

My environment:

  • tns version 2.5.3
  • tns core modules 2.5.2
  • nativescript-push-notifications 0.1.2
  • nativescript-local-notifications 1.2.1
  • Android emulator 7.1.1 API 25

ickata avatar Apr 04 '17 20:04 ickata

Just tested on a real device – Asus Nexus 7, Android 5.1.1, and I can confirm that the issue is reproduced.

Please advise.

ickata avatar Apr 04 '17 20:04 ickata

OK, this one is expected and is written in the documentation:

If the app is in background, a notification is put in the tray. When tapped, it launches the app, but does not invoke the onMessageReceived callback.

It looks like I have to use data message instead of notification message.

ickata avatar Apr 05 '17 04:04 ickata

Hi,

I started using data-only messages (no notification payload) and indeed onMessageReceived is always invoked now, both when app is running in foreground or background. At least on my emulator with Android 7.1.1 (API 25).

However, when I deploy and test on real Android 5.1.1, onMessageReceived is NOT invoked when app is running in background and I tap on the notification tray message.

Here is my data payload:

      data  : {
         type        : 'event_name',
         data        : {"foo": "bar"},
         title       : 'Tray notification title',
         message     : 'Tray notification message',
         smallIcon   : 'icon_notify',  // copied in App_Resources
         sound       : 'notify',       // copied in App_Resources
      }

notification payload is NOT set.

ickata avatar Apr 07 '17 20:04 ickata

Hi,

Is there any way where I can get this plugin work together with firebase plugin? The problem I am facing is tough I am able to built my project, I am not receiving the push notifications if I use the nativescript-plugin-firebase plugin installed. Without the plugin the notifications are showing up.

manishbaruah avatar Nov 01 '17 15:11 manishbaruah