Laureant
Laureant

Reputation: 1019

Pushbots handle notification while Phonegap app is open

I have a PhoneGap app, that I've set up Pushbots with (in the onDeviceReady event).

The notification:clicked event is working correctly if the application was not running in the background, but if the app is running and it's in use, and the user brings down the notification tab and clicks on a notification, nothing happens.

How can I fire the notification:clicked event when the app is in the foreground?

I want to navigate to a page whenever the user clicks the notification, whether or not the application is running or not.

My index.js file

myApp.run(['$rootScope', function($rootScope) {
    document.addEventListener('deviceready', function() {

        // Handle the Cordova pause and resume events
        document.addEventListener( 'pause', onPause.bind( this ), false );
        document.addEventListener( 'resume', onResume.bind( this ), false );

        window.plugins.PushbotsPlugin.initialize(...);
        window.plugins.PushbotsPlugin.on("registered", function(token){
            console.log("Registration Id:" + token);
        });

        window.plugins.PushbotsPlugin.getRegistrationId(function(token){
            console.log("Registration Id:" + token);
        });

        // Should be called once app receive the notification
        window.plugins.PushbotsPlugin.on("notification:received", function(data){
            alert("received:" + JSON.stringify(data));
            console.log("received:" + JSON.stringify(data));
        });

        // Should be called once the notification is clicked
        window.plugins.PushbotsPlugin.on("notification:clicked", function(data){
            alert("Notification clicked"); only fires when the app is not running
            $rootScope.$emit('onNotificationClick', data);
            console.log("clicked:" + JSON.stringify(data));
        });

    }, false);
}]);

I have a $rootScope.onNotificationClick event in my MainAppController. Should I instantiate / pass the Pushbotsplugin to that controller somehow?

Upvotes: 0

Views: 332

Answers (1)

bvakiti
bvakiti

Reputation: 3611

you are emitting the event on notification click. So you can handle $on in controller using below code

$rootScoope.$on('onNotificationClick', function (data) {
 // handle here.
 //need not pass pushBotPlugin
});

Upvotes: 0

Related Questions