question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Notifications only works if being triggered at the current time, not a time in future!

See original GitHub issue

WARNING: IF YOU IGNORE THIS TEMPLATE, WE’LL IGNORE YOUR ISSUE. YOU MUST FILL THIS IN!

Last year, I have used this plugin for an Ionic v3 application (which I no longer have the access to the source code so no idea which version it was) and it worked just great. However, now when I tried to used it in another new one, it doesn’t operate as it should. In short, I cannot make local notification comes at a specific time in the future, only at the current moment, there is no error log shown when calling the plugin.

Any help would be much appreciated.

Your Environment

Plugin: cordova-plugin-local-notification 0.9.0-beta.2 “LocalNotification”

Ionic: Ionic CLI : 5.2.5 (C:\Users\hoangN\AppData\Roaming\npm\node_modules\ionic) Ionic Framework : ionic-angular 3.9.2 @ionic/app-scripts : 3.1.8

Cordova: Cordova CLI : 8.1.2 (cordova-lib@8.1.1) Cordova Platforms : android 8.1.0 Cordova Plugins : cordova-plugin-ionic-keyboard 2.0.5, cordova-plugin-ionic-webview 1.1.1, (and 13 other plugins)

Utility: cordova-res : 0.8.0 (update available: 0.8.1) native-run : 0.2.9

System: NodeJS : v10.16.3 (C:\Program Files\nodejs\node.exe) npm : 6.9.0 OS : Windows 10

Expected Behavior

Notifications should come at the time specified in the trigger param.

Actual Behavior

Notifications only come at current moment, no future triggering.

Steps to Reproduce

This is the simplified piece of code that I’m using in order to demonstarte this issue, I’ve created 2 button to fire notifications, one is for current moment, one is for 2 minutes later from that.

 setLocalNotiNow() {
    let promise = new Promise(() => {
      let currdate = moment().format('YYYY-MM-DD HH:mm:ss');  
      let currentMoment= moment(currdate,'YYYY-MM-DD HH:mm:ss' )
      console.log(currentMoment.toDate())
      this.localNotifications.schedule({
        text: 'Xin vui lòng đến đúng lịch để chúng tôi có thể phục vụ bạn tốt hơn.',
        trigger: { at: currentMoment.toDate() },
        led: 'FF0000',
        sound: null,
        priority:1,
        foreground:true
      });

    });
    return promise;
  }
  setLocalNotiLater() {
    let promise = new Promise(() => {
      let currdate = moment().format('YYYY-MM-DD HH:mm:ss');  
      let currentMoment= moment(currdate,'YYYY-MM-DD HH:mm:ss' )
      let time = moment.duration("00:02:00");
      currentMoment.add(time)
      console.log(currentMoment.toDate())
      this.localNotifications.schedule({
        text: 'Xin vui lòng đến đúng lịch để chúng tôi có thể phục vụ bạn tốt hơn.',
        trigger: { at: currentMoment.toDate() },
        led: 'FF0000',
        sound: null,
        priority:1,
        foreground:true
      });

    });
    return promise;
  }

Context

What were you trying to do?

Debug logs

Include iOS / Android logs

  • ios XCode logs
  • Android: $ adb logcat

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:1
  • Comments:19

github_iconTop GitHub Comments

4reactions
m0dch3ncommented, Feb 27, 2020

I finally found some time to investigate into this, and it seems that my fix is solving this issue. Here is an explanation why it’s not working.

https://github.com/katzer/cordova-plugin-local-notifications/blob/caff55ec758fdf298029ae98aff7f6a8a097feac/src/android/notification/Notification.java#L217-L243

The first line decides if the Notification Intent is triggered immediately or if its a PendingIntent added to the AlarmManager.

The AlarmManager then did not wake up, and fired the notification, because the following Receivers were not added to the AndroidManifest.xml

https://github.com/katzer/cordova-plugin-local-notifications/blob/caff55ec758fdf298029ae98aff7f6a8a097feac/plugin.xml#L103-L135

I fixed this in my fork:

https://github.com/katzer/cordova-plugin-local-notifications/compare/master...m0dch3n:master

So the solution which worked for me is:

cordova plugin remove cordova-plugin-local-notification
cordova plugin add https://github.com/m0dch3n/cordova-plugin-local-notifications.git

It seems this plugin is also no longer maintained, and JFYI, I will neither maintain my fork, unless I find specific problems to my apps.

BTW: I think it has nothing todo with the smallIcon, or array solution, but you can correct me… I suppose it only worked during your debug session, because you stopped long enough in your Breakpoint, so that

!date.after(new Date())

became true…

2reactions
TheNotorius0commented, May 19, 2020

I have had the same issue. Actually it worked for me few weeks ago and then it stopped working randomly. The notifications worked only when triggered instantly.

But I’ve found a solution! At least for me. The code below was missing on my AndroidManifest.xml. I have no idea why, maybe another plugin messed it up (probably firebasex with his push notifications?)

Anyway just add it to your manifest and it will start working again.

<activity android:exported="false" android:launchMode="singleInstance" android:name="de.appplant.cordova.plugin.localnotification.ClickReceiver" android:theme="@android:style/Theme.Translucent"/>
<provider android:authorities="${applicationId}.provider" android:exported="false" android:grantUriPermissions="true" android:name="de.appplant.cordova.plugin.notification.util.AssetProvider">
  <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/localnotification_provider_paths"/>
</provider>
<receiver android:exported="false" android:name="de.appplant.cordova.plugin.localnotification.TriggerReceiver"/>
<receiver android:exported="false" android:name="de.appplant.cordova.plugin.localnotification.ClearReceiver"/>
Read more comments on GitHub >

github_iconTop Results From Across the Web

Why isn't my Android notification triggering in the future, only ...
I am trying to implement a notification to trigger at a pre-defined time when the user selects a date from the DatePicker (e.g....
Read more >
Resolved - Mobile Notifications - Not working ? - Unity Forum
Hello everybody, "Good news" : the notifications will only work when the project is builded with the status "Development Build". But how to...
Read more >
Email Notification Trigger not going out correctly - ServiceNow
Solved: Hello, this is my first time asking a question here so please let me know if you need any additional information. We...
Read more >
Notification Triggers - Chrome Developers
The problem with the Push API is that it's not reliable for triggering notifications which must be shown when a particular condition, like...
Read more >
Time Operators - OneSignal Documentation
Use time-based triggers to send push notifications to users based on the time. ... 1600968090 is the past, present or future date of...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found