Cordova-plugin-local-notifications: Notification based on a condition.

Created on 9 May 2016  Â·  16Comments  Â·  Source: katzer/cordova-plugin-local-notifications

Hello,
It's possible the notification scheduled is trigged only if condition is verified?

Thanks

Awaiting Information

Most helpful comment

+1, also if closed. My need is to create a notification every day, every day the text will change and it has to appear only on specific condition which change day by day.

I retrieve records related to the current day from sqlite, and if there's at least a record, the notification should appear with the content of the record(s).
Any way to achieve this?

All 16 comments

I try to explain myself better, I apologize for my English.

In my app imposed deadlines that I store in a local db. 5 days before the deadline should inform me (that is 5 days). I thought I'd check every day, at a set time, if the condition occurs, and only in this case, trigger the notification, otherwise not.

You can do with this plugin?

I hope I explained.
Thank you.

Yes, this is possible. I had the same issue and found some sort of workaround which is probably not the best way, but i works for me.

First make a function that gets the needed data to verify your if statement, preferably returning a boolean. Do something like:

function getDataToVerifyStatement(){
//your logic here to check the days here
} 

Then after that make a 'fake'-notification that will be 'send' everyday. This notification will never be visible to the user cause it will never be send. It works somewhat like this:

var fakeNotification = {
id: yourId,
firstAt: yourDate,
every: "day",
checkStatement: getDataToVerifyStatement()
}

Do not add this notification with the plugin just declare it like above.

After that you can just say, depending on your expected response, something similar to:

if(fakeNotification.checkStatement === true){
//or maybe something like daysRemaining === 5, what ever you like.

var realNotification = {
//your notification here
}

 $cordovaLocalNotification.add(realNotification);
}

Put all above together in one function and then trigger that function when a deadline is set. From then on the fakeNotification will check everyday if it is 5 or less days remaining.

That was what worked for me, i hope i helped you!

Thank you for responding, I understand the solution you proposed to me. I tried the code but it does not work. I wanted to ask you something, as can be scheduled if the fakeNotification is not added to the plugin?

function onDeviceReady() {
       var fakeNotification = {
            id: 1,
            every: "minute",//Using minute as a test, but it will be day
            checkStatement: getDataToVerifyStatement()
        }

        if (fakeNotification.checkStatement === true) {
            var realNotification = {
                id: 1,
                title: "Prima notifica",
                text: readDataForNotify(),
                data: "{test"
            }

            cordova.plugins.notification.local.add(realNotification);
        }
}

function getDataToVerifyStatement() {
       //logic for  check the days
       return true;
}

what am I doing wrong?
Thanks for your interest.

@blufix79 In the fakeNotification you miss the firstAt attribute that i mentioned in my first post. You can set it to something like:

var now = new Date().getTime();
var _10SecondsFromNow = new Date(now + 10 * 1000);

       var fakeNotification = {
             id: 1,
             firstAt: _10SecondsFromNow,
            every: "minute",//Using minute as a test, but it will be day
            checkStatement: getDataToVerifyStatement()
        }

The firstAt attribute should make it run every minute(or day), even when the app is completely closed. Also you dont have a date attribute in the realNotification, i'm not sure if that is required, but you could set that to the variable: _10SecondsFromNow, you will then get the first notification after 20 seconds and then after that notification you will recieve one every 70 seconds. But you can place interval with anything you want there. I would recommend using different ID's for the notifications as well.

You can try this code:

function onDeviceReady() {

var now = new Date().getTime();
var _10SecondsFromNow = new Date(now + 10 * 1000);

       var fakeNotification = {
            id: 1,
            firstAt: _10SecondsFromNow,
            every: "minute",//Using minute as a test, but it will be day
            checkStatement: getDataToVerifyStatement()
        }

        if (fakeNotification.checkStatement === true) {
            var realNotification = {
                id: 2,
                title: "Prima notifica",
                message: 'hello world',//your function date function here
                date: _10SecondsFromNow,
                autoCancel: true,
                data: "{test"}
            }

             //not sure, but you might want to put: $cordova.plugins.notification.local.add(realNotification);
            cordova.plugins.notification.local.add(realNotification);
        }
}

function getDataToVerifyStatement() {
       //logic for  check the days
       return true;
}

That should do it. I hope that fixes your problem.

@MarkVlamPersie, I am sorry, but it does not work now! Notification is only done the first time, then it is no longer performed.

@blufix79 Ah, i see now. I got the notifications because i sheduled them. But that's still not a fix for you because when you shedule the fake-notification that is mentioned above.

       cordova.plugins.notification.local.addNotification({
            id: 1,
            firstAt: _10SecondsFromNow,
            every: "minute",//Using minute as a test, but it will be day
            checkStatement: getDataToVerifyStatement()
        })

The checkStatement: getDataToVerifyStatement() will always be what it was the first time you sheduled it. So if it's false the first time, it will also be false when the condition is actually true because it doesn't check again, it just takes the value that it was given the first time when sheduling the notification.

I'm sorry i can't help you any further.

Thank you!
It should be "fake Notification" to be scheduled, so check "getDataToVerifyStatement ()", but not dorebbe be visible to the user .. do not know how you could do.

Any solution on this one?

I would suggest implementing additional property like "checkStatement". If function is false, don't trigger notification. This would make things easier, and avoid messing around with trigger event which isn't fired when application is not active.

Can't you pls provide sample for your statement "don't trigger
notification" is there any particular function for this?
On Jun 9, 2016 1:43 PM, "Alan" [email protected] wrote:

I would suggest implementing additional property like "checkStatement". If
function is false, don't trigger notification. This would make things
easier, and avoid messing around with trigger event which isn't fired when
application is not active.

—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
https://github.com/katzer/cordova-plugin-local-notifications/issues/972#issuecomment-224828273,
or mute the thread
https://github.com/notifications/unsubscribe/AICOjelw9FOD3l03WpYderKq1wSBYKr7ks5qJ8sMgaJpZM4IaeeT
.

I am making a http call ($http) on my API to check things in database. I know that probably better way would be to use push notifications but for me it's easier to avoid Android and Apple push services and stay on nice and simple local notifications...

Checked all the other events, they are also not executed.

Ok, switching on push notifications. :(

Is this still an issue?

Closed as no response from original poster.

+1, also if closed. My need is to create a notification every day, every day the text will change and it has to appear only on specific condition which change day by day.

I retrieve records related to the current day from sqlite, and if there's at least a record, the notification should appear with the content of the record(s).
Any way to achieve this?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ThorvaldAagaard picture ThorvaldAagaard  Â·  3Comments

mino922 picture mino922  Â·  3Comments

daniele-sartiano picture daniele-sartiano  Â·  4Comments

makinhs picture makinhs  Â·  5Comments

Shashank2406 picture Shashank2406  Â·  5Comments