0

Katzer Local notification plugin

I can set and use single notification.

According to the description mutiple notification can be set using

 cordova.plugins.notification.local.schedule([{
    id: 1,
    text: "Multi Notification 1",
    sound: isAndroid ? 'file://sound.mp3' : 'file://beep.caf',
    data: { secret:key }
},{
    id: 2,
    title: "Local Notification Example",
    text: "Multi Notification 2",
    icon: "http://sciactive.com/pnotify/includes/github-icon.png"
}]);

But my notifications are dynamic and it's total number is also dynamic. For example say total is the variable where total number of notification is saved. total can be 1 or 10 or 30 etc.

Now how to build the array for it?? I tried like this

for(i=0;i<total;i++)
{
    // ......... calculate bhhour,vmin,vsec etc. ............. //

    time_for_noti=new Date(year,month-1,parseInt(i),vhour,vmin,vsec);

    arr[i]=' id: app_'+i+' , title: ' +i+' - '+time_for_noti+',text: app alarm.,sound: null,at  :    '+time_for_noti+'  ';

}

And then

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

App hangs for some time say 15-20 sec, then crashes. Then I tried brackets '{}' before and after the strings.

for(i=0;i<total;i++)
{
    // ......... calculate bhhour,vmin,vsec etc. ............. //

    time_for_noti=new Date(year,month-1,parseInt(i),vhour,vmin,vsec);

    arr[i]='{ id: app_'+i+' , title: ' +i+' - '+time_for_noti+',text: app alarm.,sound: null,at  :    '+time_for_noti+'  }';

}

Again same result. App crashes after 10-15 sec from schedule line's execution. I also tried making a huge string manually something like

ex='[{ id:........} , {..........}]';

And then

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

It crashes the app immediately after this line's execution. I know it's a dumb idea, but desperate times.

What I'm doing wrong? How to achive this multiple alarm dynamically for total 20-40 notification? What I'm missing?

AtanuCSE
  • 8,832
  • 14
  • 74
  • 112

3 Answers3

0

I think each element in the array needs to be an object, not a string. Have you tried something like this?

arr[i]={ id: i, text: "Multi Notification " + i };

That is, replace the quote marks around your object with curly brackets.

I can't try it myself because I'm using the Meteor version which doesn't seem to work the same way.

Little Brain
  • 2,647
  • 1
  • 30
  • 54
0

no need to add multiple notification. I've looped the sound of 1 notification until user clicks on it. and to give more alarm like feature I bring the app to foreground when notification is triggered.

have a look at https://github.com/vasani-arpit/cordova-plugin-local-notifications/blob/master/README.md. it is forked from Katzer Local notification plugin so all the syntax is the same.

Arpit Vasani
  • 1,013
  • 8
  • 19
0

Here is my solution and it works well :

var notiflist = [];
for(var i = 0; i < data.length; i++) {
   notiflist[i] = { id: i, title: data[i].Title, text: data[i].TextPush };
} 
cordova.plugins.notification.local.schedule(notiflist);