fechanique / cordova-plugin-fcm

Google FCM Push Notifications Cordova Plugin
623 stars 997 forks source link

Can't figure out how to handle onNotification in ionic 1. Please help #302

Open kurtnovice opened 7 years ago

kurtnovice commented 7 years ago

Hi folks,

I am quite new to this plugin. I get to configure my FCM android app and got to the point where I can send notif from a laravel server. The notification array is like this {title, body, sound}

I got to receive the push notification on my cellphone when I tested the app (yay!), however I have no clue how to handle it. When I tap on the notification, it goes back to my app and does nothing. Doesn't matter whether it's on the foreground or background, still it does nothing.

I tried to do ...

  1. in module run, I tried to initiate an on resume
    $ionicPlatform.$on('resume', function(){
    FCMPlugin.onNotification(function(data){
                if(data.wasTapped){
                  //Notification was received on device tray and tapped by the user.
                  alert( JSON.stringify(data) );
                }else{
                  //Notification was received in foreground. Maybe the user needs to be notified.
                  alert( JSON.stringify(data) );
                }
            });
    })

    .. didn't work

  2. create a service that would be called on every page controller, assuming when the notification is tapped, it would call this service function.
    service('FCM', function(){
    return{
    checkPush: function(){
            FCMPlugin.onNotification(function(data){
                if(data.wasTapped){
                  //Notification was received on device tray and tapped by the user.
                  alert( JSON.stringify(data) );
                }else{
                  //Notification was received in foreground. Maybe the user needs to be notified.
                  alert( JSON.stringify(data) );
                }
            });
        }
    }
    };

The readme doesn't offer a clue, it simply says... If the user taps the notification, the application comes to foreground and the notification data is received in the JavaScript callback.

Am I missing something? Please help.

yosranagati commented 7 years ago

What I did and worked for me is as following ;

window.FCMPlugin.onNotification(function (data) {
        if (data.wasTapped) {
            //Notification was received on device tray and tapped by the user.
           console.log(JSON.stringify(data));
        } else {
            // Notification was received in foreground. Maybe the user needs to be notified.
           console.log(data); 

        }