简体   繁体   English

使用本地通知插件的多个通知-Phonegap

[英]Multiple notification using Local Notification plugin - Phonegap

Katzer Local notification plugin Katzer本地通知插件

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是保存通知total的变量。 total can be 1 or 10 or 30 etc. total可以是1或10或30等。

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. 应用程序挂起一段时间(例如15到20秒),然后崩溃。 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. 在计划行执行10到15秒后,应用崩溃。 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? 如何动态获得此多个警报,以获取总计20-40的通知? What I'm missing? 我想念的是什么?

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. 我自己无法尝试,因为我使用的Meteor版本似乎工作方式不同。

no need to add multiple notification. 无需添加多个通知。 I've looped the sound of 1 notification until user clicks on it. 我循环播放1条通知的声音,直到用户单击它为止。 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 . 看看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. 它是从Katzer Local通知插件派生的,因此所有语法都是相同的。

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM