0

I've set up Pushbots to work with my PhoneGap app, but since the setup and initiation of Pushbots is happening in the index.js file, i am not able to call any methods inside my main angularjs controller.

Whenever the user clicks on a notification i would want to run a function from my main controller and navigate to a page.

How can I achieve this?

Here's my index.js file with the onDeviceReady event set up:

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

    // TODO: Cordova has been loaded. Perform any initialization that requires Cordova here.
    document.addEventListener("backbutton", onBackKeyDown, 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);
    });

    window.plugins.PushbotsPlugin.on("notification:received", function(data){
        console.log("received:" + JSON.stringify(data));
    });

    // Should be called once the notification is clicked
    window.plugins.PushbotsPlugin.on("notification:clicked", function(data){
        $scope.LoadMyPage(); //this is not getting fired...
        alert("clicked:" + JSON.stringify(data));
        console.log("clicked:" + JSON.stringify(data));
    });
};
Laureant
  • 979
  • 3
  • 18
  • 47

1 Answers1

1

You can use $emit on scope when notification is clicked.

 window.plugins.PushbotsPlugin.on("notification:clicked", function(data){
    console.log("clicked:" + JSON.stringify(data));
    $rootScope.$emit('onNotificationClick');
});

Handle this event in main using

$rootScope.$on('onNotificationClick', function () {
// write your logic here
})

Hope this helps

bvakiti
  • 3,243
  • 4
  • 17
  • 26