@@ -321,13 +321,32 @@ exports.canScheduleExactAlarms = function (callback, scope) {
321
321
* @param {Object } args Optional, can be {skipPermission: true} to skip the permission check
322
322
*/
323
323
exports . schedule = function ( options , callback , scope , args ) {
324
- const optionsList = exports . _toArray ( options ) ;
324
+ let optionsList = exports . _toArray ( options ) ;
325
325
326
326
for ( const options of optionsList ) {
327
327
// Correct renamed properties and set defaults
328
328
exports . _correctOptions ( options , true ) ;
329
329
}
330
330
331
+ // Filter out notifications where the trigger time is in the past
332
+ // On iOS notifications are ignored if the trigger time is in the past, so filter
333
+ // them already here out
334
+ optionsList = optionsList . filter ( ( option ) => {
335
+ // No trigger.at set, don't filter out
336
+ if ( ! option . trigger || ! option . trigger . at ) return true ;
337
+
338
+ // Calculate difference to now
339
+ const triggerAtDiff = option . trigger . at - new Date ( ) . getTime ( ) ;
340
+
341
+ // Trigger time is in the future don't filter out
342
+ if ( triggerAtDiff > 0 ) return true ;
343
+
344
+ // Trigger time is in the past, filter out
345
+ console . warn ( "Notification trigger time is in the past, ignoring it, options=" , JSON . stringify ( option ) ) ;
346
+
347
+ return false ;
348
+ } ) ;
349
+
331
350
// Skip permission check if requested and schedule directly
332
351
if ( args && args . skipPermission ) {
333
352
console . log ( "Skip permission check" ) ;
@@ -919,14 +938,27 @@ exports._prepareTrigger = function (options) {
919
938
920
939
if ( trigger . type == "calendar" ) {
921
940
// Set default trigger time at now if nothing is set
922
- if ( ! trigger . at && ! trigger . in && ! trigger . every ) trigger . at = new Date ( ) ;
941
+ if ( ! trigger . at && ! trigger . in && ! trigger . every ) trigger . at = new Date ( ) . getTime ( ) ;
923
942
924
943
// Convert dates to numbers
925
944
if ( trigger . at ) trigger . at = exports . _dateToNumber ( trigger . at ) ;
926
945
if ( trigger . firstAt ) trigger . firstAt = exports . _dateToNumber ( trigger . firstAt ) ;
927
946
if ( trigger . before ) trigger . before = exports . _dateToNumber ( trigger . before ) ;
928
947
if ( trigger . after ) trigger . after = exports . _dateToNumber ( trigger . after ) ;
929
948
949
+ // On iOS notifications will be ignored if the trigger time is in the past
950
+ // Correct trigger.at if trigger time is maximum 5 seconds in the past
951
+ if ( trigger . at ) {
952
+ // Calculate the difference to now
953
+ const triggerAtDiff = trigger . at - new Date ( ) . getTime ( ) ;
954
+
955
+ // Only correct if maximum 5 seconds in the past
956
+ if ( triggerAtDiff > - 5000 && triggerAtDiff <= 0 ) {
957
+ // Set it a little bit in the future so it will be definitely triggered
958
+ trigger . at = new Date ( ) . getTime ( ) + 5000 ;
959
+ }
960
+ }
961
+
930
962
// Warning that trigger.count is not supported on iOS
931
963
if ( device . platform == 'iOS' && trigger . count ) {
932
964
console . warn ( 'trigger.count is not supported on iOS.' ) ;
0 commit comments