Cordova-plugin-local-notifications: Local notification onclick issue

Created on 4 Feb 2015  Â·  21Comments  Â·  Source: katzer/cordova-plugin-local-notifications

Hi thanks for this great plugin... i have installed the local notification by command line and all work perfectly i received the notification but i cant make a redirection to a specific page when i clicked on the notification it redirect me to the current page instead of my specific page this my code

$.ajax({


type: "POST",
url: "http://******/~my/path/send_filter.php",

data:   
        "&classes="+all_classes+
        "&states="+all_states+
        "&titres="+all_titres+
        "&autheurs="+all_autheur+
        "&id="+id,

dataType: "json",

cache: false,


beforeSend: function(){ $("#send_filter").val('Enregistrement...');},

success: function(data) {

    if(data)
    {
        if (data.msg=="ok") {

            $("#send_filter").val('ok');

         var id        = window.localStorage.getItem('id');
         var url       = "http://*****/~mypath/my path/get_notify_alerts.php?id="+id;

        document.addEventListener('deviceready', function () {


        $.getJSON(url,function(json){

                id_notify   = json.notify.id;
                updater     = json.notify.date;
                msg_alert   = "Resultat d'Alert de filtre";

            window.plugin.notification.local.add({
                id:      "1",
                title:   "TM",
                message: msg_alert,
                });

              });


            window.plugin.notification.local.onclick = function (id, state, json) 
                    {
                         location.href="index.html";
                    };

            });


        };

Most helpful comment

Not working for me also when click on notification. Is there any solution?

All 21 comments

seems like onclick is not working on 0.8 but is on stable

hmm, now I see that 0.8 is already on "rc" but onclick is still not working (android 4.4.2). I bet its working for the developers but not for some like me. Maybe the fact that I had 0.7x installed is affecting it?
Ive tried a minimal example of .add a basic notification (works) but clicking does not cause onclick to get called (app running, not cold start or suspended).
Note that on 0.7x I got the onclick to work even under cold start. see issue #410

I agree, the onclick function never fires whether the app is running or not. This is a fairly significant bug

@zmandel, the way of how to schedule notifications or listen for events has been changed! I am working on the wiki buts its not yet complete.

cordova.plugins.notification.local.on("click", function(notification) {
    alert("clicked: " + notification.id);
});

Please see https://github.com/katzer/cordova-plugin-local-notifications/wiki/Schedule-Notifications.

You can also take a look into the example branch

Even with my callback registered as you have above on the lastest version of the source, the event is never triggered.

Would you please show me the options object you pass to schedule. Does it contain json: or data: value?
Thanks

cordova.plugins.notification.local.schedule({
    id: app.getRandomID(),
    date: new Date(),
    title: e.payload.title,
    text: e.payload.message,
    badge: app.badgeCount,
    data: { state: e.payload.navTo }
});

I am passing in an object. I tried using alert and console.log as well as the action I am trying to run as proof that the event handler is even getting called, but nothing is triggered.

cordova.plugins.notification.local.schedule({
    id: rrApp.getRandomID(),
date: new Date(),
title: e.payload.title,
text: e.payload.message,
badge: rrApp.badgeCount,
data: { state: e.payload.navTo }
});

I tried both json and data, neither works. Here is my click registration also, nothing logs or alerts or navigates (inside $ionic.ready):

cordova.plugins.notification.local.on("click", function (notification) {
    app.badgeCount = 0;
    console.log("Local notification clicked, id= " + notification.id + ", state = " + notification.state + ", json = " + notification.json);
    alert("clicked");
    $state.go(JSON.parse(notification.json).state);
});

Cannot find a issue on plugin side.
You are sure that app.badgeCount is defined? Because before you use rrApp.badgeCount.

Otherwise does it get fired with a test notification without _data:_?

@danda1man @katzer I just found this issue because I was having similar problems and I can confirm that after following the instructions in the wiki and using the new API it works fine in iOS 8 simulator, passing data and all:

// in controller scope method
$ionicPlatform.ready(function () {
  if (window.plugin && window.plugin.notification) {
    window.plugin.notification.local.schedule({
      id: id
      date: alarmTime,
      title: title,
      text: message,
      data: data,
      badge: badgeCount
    });
  }
});
// in app.run()
$ionicPlatform.ready(function () {
  // ...

  if (window.plugin && window.plugin.notification) {
    window.plugin.notification.local.setDefaults({
      autoCancel: true
    });

    if (window.device && window.device.platform === 'iOS') {
      window.plugin.notification.local.registerPermission();
    }

    window.plugin.notification.local.on('click', function (notification) {
      $timeout(function () {
        $rootScope.$broadcast('cordovaLocalNotification:click', notification);
      });
    });

    window.plugin.notification.local.on('trigger', function (notification) {
      $timeout(function () {
        $rootScope.$broadcast('cordovaLocalNotification:trigger', notification);
      });
    });
  }
});

And finally, in any controller:

$scope.$on('cordovaLocalNotification:trigger', function(notification) {
  alert(notification.id);
});
$scope.$on('cordovaLocalNotification:click', function(notification) {
  alert(notification.id); 
});

This uses the technique mentioned here: http://devdactic.com/local-notifications-ionic/ and has been working as of right now! :)

Great!

Yes those values are defined, I see the badge number in the notification.
Same result if I remove data from the schedule call. I tried with the
timeout pattern above (same thing ngCordova uses, still no click action).
I am testing on a real device (Galaxy Note 4), not an emulator.

On Tue, Feb 17, 2015 at 11:07 AM, Sebastián Katzer <[email protected]

wrote:

Great!

—
Reply to this email directly or view it on GitHub
https://github.com/katzer/cordova-plugin-local-notifications/issues/406#issuecomment-74705576
.

Dan Johnson

The new 0.8.x release is a complete rewrite. If the issue still exists please reopen it.

You may also take a look into the new wiki.

Thanks!

Notifications data are parsed in string format. You should use:

JSON.parse(notification.data)

Do someone knows how to implement the click event using typescript?

@Jatapiaro working example with Ionic and Typescript (plugin v0.8.5):

constructor(private localNotifications: LocalNotifications){
//.... 
this.localNotifications.on('click', (notification, state) => {
            this.onClickTest(notification, state);
      });
}

@OctoberCat I tested in ios and click is not calling the callback, check my code:

this.localNotifications.on('click', (notification, state) => {
      console.log("clickeeddddd: ", notification, state);
      alert("clickkkedddd: ")
    });

I get the notification but nothing happens when I click.

This is the on click in the constructor

this.plt.ready().then((readySource) => {
  this.localNotifications.on('click', (notification, state) => {
    let json = JSON.parse(notification.data);

    let alert = alertCtrl.create({
      title: notification.title,
      subTitle: json.mydata
    });
    alert.present();
  })
});

Any help?

My onclick function is not working. Please check my below code and provide me the solution.

cordova.plugins.notification.local.on("click", (notification) => {
});

Notification scheduling works for me but on click and trigger functions not getting called.

Thanks in advance.

Not working for me also when click on notification. Is there any solution?

Was this page helpful?
0 / 5 - 0 ratings