本地通知每天反复在P​​honeGap的机器人 [英] Local Notification repeated every day in PhoneGap Android

查看:156
本文介绍了本地通知每天反复在P​​honeGap的机器人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通知每天从我的应用程序使用LocalNotification插件,我发现在GitHub上发送。我有以下的code这在应用程序启动,就会发送通知。

I am trying to send notifications everyday from my app using LocalNotification plugin that I found at github. I have the following code which sends a notification as soon as the application is started.

    var notification = cordova.require("cordova/plugin/localNotification");

              document.addEventListener('deviceready', onDeviceReady, false);

              function onDeviceReady() {
                alert('device ready');
               var id = 0;
      id++;
      newDate = new Date();
      newDate.setUTCHours(1,30,1);
          notification.add({
                id : id,
                date : newDate,
                message : "Your message here",
                subtitle: "Your subtitle here",
                ticker : "Ticker text here",
                repeatDaily : true
          });                
}

不过,我希望应用程序自动发送通知没有被打开。设置选项repeatDaily为true,将帮助?

But I want the application to automatically send notification without being opened. Setting the option repeatDaily to true will help ?

我做我的研究并发现其他人能够使用LocalNotification插件来实现它。

I did my research and found out that others were able to achieve it using the LocalNotification plugin.

我不太清楚如何进行测试,因为它要求我保持AVD开机一整天。我们的目标是很简单的。我需要每天发送一个通知到用户,而无需打开应用程序。任何帮助将是非常美联社preciated!谢谢!

I am not quite sure of how to test since it requires me to keep the AVD powered on for one full day. The objective is very simple. I need to send out a single notification everyday to a user without opening the app. Any help will be highly appreciated !! Thanks !!

推荐答案

我从来没有使用这个插件自己,但有一点点到code让我发现是,只要你设置 repeatDaily 的通知将在那里的每一天。

I have never used the plugin myself but a little digging into the code shows me that yes as long as you set repeatDaily to true your notification will be there every day.

如果你拿上一看<一href="https://github.com/phonegap/phonegap-plugins/blob/master/Android/LocalNotification/AlarmHelper.java"相对=nofollow> AlarmHelper 类,你可以看到,如果子句的参数设置重复的每一天。

If you take a look on the AlarmHelper class you can see the if clause for that parameter setting to repeat every day.

final AlarmManager am = getAlarmManager();

...

if (repeatDaily) {
        am.setRepeating(AlarmManager.RTC_WAKEUP, triggerTime, AlarmManager.INTERVAL_DAY, sender);
    } else {
        am.set(AlarmManager.RTC_WAKEUP, triggerTime, sender);
    }

在<一个一个附加的详细解释href="https://github.com/phonegap/phonegap-plugins/blob/master/Android/LocalNotification/AlarmReceiver.java"相对=nofollow> AlarmReceiver 类是,如果你设定的时间为previous时间(如现在是11:00,设置了报警,在08:00至每天重复),它会立即火,然后在在预定时间的第二天。因此,该类有一个if子句prevent的。

One extra detail explained on the AlarmReceiver class is that if you set the time for a previous time, (e.g. now is 11:00 and you set the alarm to repeat every day at 08:00) it will fire immediately, and then in the next day on the scheduled time. So that class has an if clause to prevent that.

if (currentHour != alarmHour && currentMin != alarmMin) {
            /*
             * If you set a repeating alarm at 11:00 in the morning and it
             * should trigger every morning at 08:00 o'clock, it will
             * immediately fire. E.g. Android tries to make up for the
             * 'forgotten' reminder for that day. Therefore we ignore the event
             * if Android tries to 'catch up'.
             */
            Log.d(LocalNotification.PLUGIN_NAME, "AlarmReceiver, ignoring alarm since it is due");
            return;
        }

要设置日期,使用日期参数。在您的样品你使用新的Date()返回默认当前日期时间,和您的通知会在每天的同一时间显示。如果要指定不同的时间,你的报警器,传递一个Date对象,将所需的时间!

To set the date, you use the date param. In your sample you're using new Date() which returns by default the current datetime, and your notification will be displayed daily at the same time. If you want to specify a different time for your alarm, pass in a date object with the desired time!

修改

确保您的code为正在使用的localStorage只运行一次的简单方法。

An easy way of making sure your code is running only once is using localstorage.

function onDeviceReady(){
   ...
   //note that this will return true if there is anything stored on "isAlarmSet"
   var isSet = Boolean(window.localStorage.getItem("isAlarmSet")); 
   if (isSet){
       //Alarm is not set, so we set it here
       window.localStorage.setItem("isAlarmSet",1);
    }
}

和公正,确保清除变量,如果你曾经取消设置你的报警:

And just make sure to clear the variable if you ever unset your alarm:

localStorage.removeItem("isAlarmSet);

这篇关于本地通知每天反复在P​​honeGap的机器人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆