Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Notifications not fired at the right time. #1190

Closed
koreem opened this issue Jan 17, 2017 · 18 comments
Closed

Notifications not fired at the right time. #1190

koreem opened this issue Jan 17, 2017 · 18 comments

Comments

@koreem
Copy link

koreem commented Jan 17, 2017

My Enviroment
Ionic
Plugin version: de.appplant.cordova.plugin.local-notification 0.8.4
Platform: Android
OS: macOS Sierra
Device manufacturer / model: Samsung Galaxy Tab A
Cordova version (cordova -v): 6.4.0
Cordova platform version (cordova platform ls): Installed platforms: android 6.0.1

Expected Behavior
I have scheduled 3 notifications: 09:10, 09:15, 09:20, they should fire at te right time.

Actual Behavior
They are not fired at the right time, sometimes the first one is fired, the second one isn't and at the last scheduled time it fires two times. It's completely random.

Steps to Reproduce
Scheduling code:

var pushAppointments = [];

pushAppointments.push({
    id: id++,
    title: translator.translate("Reminder appointment"),
    at: at,
    text: appointment.description + ": " + dateToShow,
    icon: "res:///not_icon",
})  

cordova.plugins.notification.local.schedule(pushAppointments);

When I check the pending notifications with:

cordova.plugins.notification.local.getAll(function (notifications) {
    console.log("pending notifications: ");
    console.log(notifications);
});

The 'at' property is correctly set, in my example 1484640600, 1484640900, 1484641200.

@rwillett
Copy link
Collaborator

rwillett commented Jan 17, 2017

Please use the template in #1188 with more information and update your question.

Rob

@koreem
Copy link
Author

koreem commented Jan 17, 2017

@rwillett done.

@rwillett
Copy link
Collaborator

It looks pretty much the same as ours and ours works on Android 6.0.

Our differences:

  1. We have a callback in cordova.plugins.notification.local.schedule(). It doesn't do anything but we have it :)

  2. Exactly how are you creating the at for the at: field? We use

        var d          = new Date();
        var now        = d.getTime();
        var year       = d.getFullYear();
        var month      = d.getMonth();
        var day        = d.getDate();

        var reminder0  = new Date(now + 60 * 1000);
        var reminder1  = new Date(year , month , day , 19 , 30 , 0);
        var reminder2  = new Date(year , month , day , 20 , 30 , 0);
	var reminder3  = new Date(year , month , day , 21 , 0 , 0);

to create four reminders that are scheduled in 60 secs and at 19:30, 20:30 and 21:00 for today.

and we create the array using

	var notifications = [ { id: GetNotificationIdAndIncrement() ,
                                sound: "" , // Turn the sound off
	                        at: reminder0 ,
				data: { type: "CongestionChargeNotification" , original_schedule_time: reminder0.getTime()/1000 ,
                                       	reminder_type: "reminder0_congestion_charge" , order: 1} ,
	                        text: "You've crossed into the Congestion Zone and might need to pay. You'll get more reminders later today."
                             } ,
	                     { id: GetNotificationIdAndIncrement() ,
                               sound: "" , // Turn the sound off
	                       at: reminder1 ,
	                       data: { type: "CongestionChargeNotification" , original_schedule_time: reminder1.getTime()/1000  ,
                                       reminder_type: "reminder1_congestion_charge" , order: 2} ,
                               text: "Reminder 1 - Don't forget to pay the Congestion Charge before midnight."
                             } ,
                             { id: GetNotificationIdAndIncrement() ,
                               sound: "" , // Turn the sound off
                               at: reminder2 ,
                               data: { type: "CongestionChargeNotification" , original_schedule_time: reminder2.getTime()/1000  ,
                                       reminder_type: "reminder2_congestion_charge" , order: 3} ,
                               text: "Reminder 2 - Don't forget to pay the Congestion Charge before midnight."
                             } ,
                             { id:  GetNotificationIdAndIncrement() ,
                               sound: "" , // Turn the sound off
                               at: reminder3 ,
                               data: { type: "CongestionChargeNotification" , original_schedule_time: reminder3.getTime()/1000  ,
                                       reminder_type: "reminder3_congestion_charge" , order: 4} ,
                               text: "Final Reminder - Don't forget to pay the Congestion Charge before midnight."
                             }];

We add the icon separately to the above for a reason that now escapes me.

file://pw_notification.png";

Our icon is a slightly different URL.

My feeling is that you are doing something wrong with the at: value but I can't see what. Post your code to create the at: value and see what that does.

Rob

@koreem
Copy link
Author

koreem commented Jan 17, 2017

@rwillett thanks for your answer. For testing purposes I tried your example and I still have the same issue. The first reminder was pushed at the right time, the second and last reminders were pushed at 15:26. See below:

screenshot_20170117-152610

var year = d.getFullYear();
var month = d.getMonth();
var day = d.getDate();
            
var reminder1  = new Date(year , month , day , 15 , 22 , 0);
var reminder2  = new Date(year , month , day , 15 , 24 , 0);
var reminder3  = new Date(year , month , day , 15 , 26 , 0);
                    
var pushAppointments = [{
   id: id++,
   title: translator.translate("Reminder appointment"),
   at: reminder1,
   text: "reminder1",
   icon: "res:///not_icon",
   },
   {
   id: id++,
   title: translator.translate("Reminder appointment"),
   at: reminder2,
   text: "reminder2",
   icon: "res:///not_icon",
   },
   {
   id: id++,
   title: translator.translate("Reminder appointment"),
   at: reminder3,
   text: "reminder3",
   icon: "res:///not_icon",
   }
];
					   
cordova.plugins.notification.local.schedule(pushAppointments);

@rwillett
Copy link
Collaborator

At what time are you running the code?

Are you putting the scheduled time far enough in the future from when you run?

We are also running a later version of the plugin, however I don't think thats the issue as we had this working on Android 6.0 some versions ago.

Getting two alerts at the same time sounds as if something is being overwritten. If this was C I'd start looking for a memory leak :)

You have the code we use in production. I will check its still working on Android 6.0 later today or tomorrow but am busy at the moment.

Can't think of anything else at this time. I'll check its working on our app again and come back.

What other plugins are you running?

Rob

@koreem
Copy link
Author

koreem commented Jan 17, 2017

Specified code runs when the app goes to the background. For testing purposes I'm scheduling the notifications 5-10 min in the future. When I check the pending notifications, the "at" property specifies the right amount of seconds, so that's a bit strange.

I'm running the following plugins:

  • com.appgiraffe.plugins.applicationPreferences 0.1.0 "ApplicationPreferences"
  • com.ionic.keyboard 1.0.4 "Keyboard"
  • cordova-plugin-android-permissions 0.10.0 "Permissions"
  • cordova-plugin-app-event 1.2.0 "Application Events"
  • cordova-plugin-app-preferences 0.7.7 "AppPreferences"
  • cordova-plugin-app-version 0.1.8 "AppVersion"
  • cordova-plugin-ble-central 1.1.0 "BLE"
  • cordova-plugin-compat 1.0.0 "Compat"
  • cordova-plugin-console 1.0.3 "Console"
  • cordova-plugin-crosswalk-webview 2.2.0 "Crosswalk WebView Engine"
  • cordova-plugin-device 1.1.2 "Device"
  • cordova-plugin-file 4.2.0 "File"
  • cordova-plugin-file-opener2 2.0.2 "File Opener2"
  • cordova-plugin-file-transfer 1.5.1 "File Transfer"
  • cordova-plugin-moove 1.0 "Cordova Plugin MOOVE"
  • cordova-plugin-screen-orientation 1.4.2 "Screen Orientation"
  • cordova-plugin-splashscreen 4.0.1 "Splashscreen"
  • cordova-plugin-touchid 0.3.1 "Touch ID"
  • cordova-plugin-transport-security 0.1.2 "App Transport Security"
  • cordova-plugin-whitelist 1.2.2 "Whitelist"
  • de.appplant.cordova.plugin.local-notification 0.8.4 "LocalNotification"
  • ionic-plugin-keyboard 2.2.0 "Keyboard"
  • org.apache.cordova.file 1.3.3 "File"

I will try the same in a new blank Ionic App.

@koreem
Copy link
Author

koreem commented Jan 18, 2017

I just isolated the problem into a blank Ionic app. I got the exact same problem. I also build the same app on my Samsung Galaxy S7 (Android 6.0.1) and it surprisingly works...

So for now I think I will build my own timing functionality with $timeout.

@rwillett
Copy link
Collaborator

That's interesting and annoying.

The $timeout won't work on IOS though in the background.

Have you tried scheduling them one at a time.

@rwillett
Copy link
Collaborator

You could try the later version of the plugin as well.

@koreem
Copy link
Author

koreem commented Jan 18, 2017

I'm using 0.8.4, that's the latest right?

Scheduling one at a time results in the same behavior, scheduling one at a time in a forEach loop fires all push notifications at once at the last "at" time, despite the fact that the "at" date seems to be right.

@rwillett
Copy link
Collaborator

There is a 0.8.5 dev branch.

Scheduling one at a time results in the same behavior, scheduling one at a time in a forEach loop fires all push notifications at once at the last "at" time, despite the fact that the "at" date seems to be right.

sounds really odd to me. That sounds like the at: field isn't working or you are overwriting something.

However since it works on a Samsung Galaxy S7 (Android 6.0.1), it does seem to indicate that Samsung have changed something.

I'll test it on an Android 6.0 Nexus

@rwillett
Copy link
Collaborator

We've just rebuilt a test version on Android 6.0.1, Nexus 5.

We schedule four notifications and they all appeared.

Here is the entire production function directly cut from the source, no changes have been made. The const SEND_CONGESTION_ZONE_NOTIFICATIONS_IMMEDIATELY is used to send local notifications out now as opposed to waiting until the evening.

    function SetupCongestionNotificationReminders() {
        // We do three reminders from 18:30 to 21:00.
        var debugSetupCongestionNotificationReminders = debugFalse;
        var whatAmI = WhatAmI("SetupCongestionNotificationReminders");

        if (debugSetupCongestionNotificationReminders)
        {
            ConsoleLog("SetupCongestionNotificationReminders: called");
        }

        var d          = new Date();
        var now        = d.getTime();
        var year       = d.getFullYear();
        var month      = d.getMonth();
        var day        = d.getDate();

        var reminder0  = new Date(now + 60 * 1000);
        var reminder1  = new Date(year , month , day , 19 , 30 , 0);
        var reminder2  = new Date(year , month , day , 20 , 30 , 0);
        var reminder3  = new Date(year , month , day , 21 , 0 , 0);

        if (SEND_CONGESTION_ZONE_NOTIFICATIONS_IMMEDIATELY)
        {
            reminder0 = new Date(now + 5 * 1000);
            reminder1 = new Date(now + 90 * 1000);
            reminder2 = new Date(now + 180 * 1000);
            reminder3 = new Date(now + 300 * 1000);
        }

        if (debugSetupCongestionNotificationReminders)
        {
            ConsoleLog("Year = " + year + " month " + month + " day " + day);
            ConsoleLog("reminder 1 = " + JSON.stringify(reminder1));
            ConsoleLog("reminder 2 = " + JSON.stringify(reminder2));
            ConsoleLog("reminder 3 = " + JSON.stringify(reminder3));
        }

        var notifications = [ { id: GetNotificationIdAndIncrement() ,
                                sound: "" , // Turn the sound off
                                at: reminder0 ,
                                data: { type: "CongestionChargeNotification" , original_schedule_time: reminder0.getTime()/1000 ,
                                        reminder_type: "reminder0_congestion_charge" , order: 1} ,
                                text: "You've crossed into the Congestion Zone and might need to pay. You'll get more reminders later today."
                             } ,
                             { id: GetNotificationIdAndIncrement() ,
                               sound: "" , // Turn the sound off
                               at: reminder1 ,
                               data: { type: "CongestionChargeNotification" , original_schedule_time: reminder1.getTime()/1000  ,
                                       reminder_type: "reminder1_congestion_charge" , order: 2} ,
                               text: "Reminder 1 - Don't forget to pay the Congestion Charge before midnight."
                             } ,
                             { id: GetNotificationIdAndIncrement() ,
                               sound: "" , // Turn the sound off
                               at: reminder2 ,
                               data: { type: "CongestionChargeNotification" , original_schedule_time: reminder2.getTime()/1000  ,
                                       reminder_type: "reminder2_congestion_charge" , order: 3} ,
                               text: "Reminder 2 - Don't forget to pay the Congestion Charge before midnight."
                             } ,
                             { id:  GetNotificationIdAndIncrement() ,
                               sound: "" , // Turn the sound off
                               at: reminder3 ,
                               data: { type: "CongestionChargeNotification" , original_schedule_time: reminder3.getTime()/1000  ,
                                       reminder_type: "reminder3_congestion_charge" , order: 4} ,
                               text: "Final Reminder - Don't forget to pay the Congestion Charge before midnight."
                             }];

        if (whatAmI === "android_app" || whatAmI === "android_simulator")
        {
            for (var i = 0 ; i < notifications.length ; i++)
            {
                notifications[i]["icon"] = "file://pw_notification.png";
            }
        }

        if (debugSetupCongestionNotificationReminders)
            ConsoleLog("SetupCongestionNotificationReminders: " + JSON.stringify(notifications));

        cordova.plugins.notification.local.schedule(notifications , function () {
            if (debugSetupCongestionNotificationReminders)
                ConsoleLog("SetupCongestionNotificationReminders: schedule callback OK");
        });
    }

We've just simulated a trip into London's congestion zone and got the notifications appear on the notification shade

screenshot_20170118-113912

@koreem
Copy link
Author

koreem commented Jan 18, 2017

Where can I find the 0.8.5 dev branch?

@rwillett
Copy link
Collaborator

@koreem
Copy link
Author

koreem commented Jan 18, 2017

Tested the 0.8.5 version. Same results. Working as it should be on my Samsung Galaxy S7, but not on the Samsung Galaxy Tab A.

$rootScope.scheduleNotifications = function() {
      
      var d          = new Date();
      var year       = d.getFullYear();
      var month      = d.getMonth();
      var day        = d.getDate();

      var reminder1  = new Date(year , month , day , 15 , 40 , 0);
      var reminder2  = new Date(year , month , day , 15 , 41 , 0);
      var reminder3  = new Date(year , month , day , 15 , 42 , 0);
      
      var pushAppointments = [{
          id: 1,
          title: "Reminder appointment",
          at: reminder1,
          text: "reminder1",
          },
          {
          id: 2,
          title: "Reminder appointment",
          at: reminder2,
          text: "reminder2",
          },
          {
          id: 3,
          title: "Reminder appointment",
          at: reminder3,
          text: "reminder3",
      }];
      
      cordova.plugins.notification.local.schedule(pushAppointments , function () {
        console.log("notifications set!")    
      });
		
      $timeout(function() {
        cordova.plugins.notification.local.getAll(function (notifications) {
          console.log("pending notifications: ");
          console.log(notifications);
        });
      }, 5000);
}

@rwillett
Copy link
Collaborator

This is a Samsung issue then.

Also we used to put stuff in $timeout functions. We found that it wasn't needed AND it caused race conditions when rescheduling and reworking the local notifications.

Rob

@koreem
Copy link
Author

koreem commented Jan 18, 2017

Ok, I will use my own timing functionality then. Thanks anyway! :)

@nadirzia
Copy link

nadirzia commented Apr 22, 2019

Ionic Local Notification does not trigger sometimes or does not trigger other times. Sometimes it triggers before time set time.

I am facing a different scenario

When a schedule notification is set for 5 minute and app in background. Then sometime notification trigger before the finish time randomly e.g(4 minute or sometime 4.5 minute)

When a schedule notification set for 5 minute and the app is left running in background. Then sometimes the notification is triggered but sometimes it is not.

below is my code
this.myDate = new Date();
this.triggerDate = new Date(this.myDate.setSeconds(this.myDate.getSeconds() + 300));

this.localNotifications.schedule({
  id: 1,
  title: 'mylimit',
  text: 'Please provide eat enjoyment and guilt experience' + new Date(this.triggerDate),
  smallIcon: 'resources/icon.png',
  data: { "mydata": this.stateType, "createdTime": this.myDate },
  trigger: {at: new Date(this.triggerDate)},
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants