简体   繁体   English

在Android 4上重复通知

[英]Repeating notifications on Android 4

Goal: Notification appears everyday once, at 2 PM, if certain condition is met. 目标:如果满足某些条件,通知每天下午2点出现一次。

Example: For simplicity, let's consider that the condition, checked with Internet connection, is met everyday. 示例:为简单起见,我们考虑每天都满足使用Internet连接检查的条件。 If today is already after 2 PM, we we'll start notifications from tomorrow. 如果今天已经在下午2点之后,我们将从明天开始通知。 For example, user starts app at 4 PM on Monday, and he gets notifications on Tuesday 2 PM, Wednesday 2 PM, Thursday 2 PM and so on. 例如,用户在星期一下午4点启动应用程序,并在周二下午2点,周三下午2点,周四下午2点获得通知,依此类推。

Problem: At 2 PM there is a first notification, but then I get the same notification over and over again, at random times. 问题:下午2点有第一个通知,但随后我会一遍又一遍地收到相同的通知。

Problem seems to be only on Android >= 4.0. 问题似乎只在Android> = 4.0上。 It works good on earlier Androids. 它在早期的机器人上运行良好。

This is how I send notification: 这是我发送通知的方式:

public class NotifyService extends Service
{       
static final int NOTIFICATION_ID = 1;
// ...

@Override
public IBinder onBind(Intent intent)
{
    return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
    try
    {
        Symbol biggest = getBiggestMover();
        if (biggest != null)
        {
            String title = getString(R.string.app_name);
            String text = getNotificationText(biggest.symbol, biggest.change);
            sendNotification(title, text);
        }
    }
    catch (Exception e)
    {
        // If there is Internet problem we do nothing, don't want to disturb the user.
        e.printStackTrace();
    }

    return super.onStartCommand(intent, flags, startId);
}

/** @return Symbol which is the biggest mover today. If there is no big mover - null is returned.
 * @throws Exception If there is Internet problem. */
private Symbol getBiggestMover() throws Exception
{
    Symbol biggest = null;
    Symbol[] equities = Network.getTraded(SymbolType.EQUITY);
    for (Symbol equity : equities)
    {
        if (Utilities.isToday(equity.lastTraded) && isBigMove(equity.change) && isBigger(equity, biggest))
        {
            biggest = equity;
        }
    }
    return biggest;
}   

private void sendNotification(String title, String text)
{
    Notification notification = new Notification(R.drawable.ic_launcher, text, System.currentTimeMillis());
    notification.flags = Notification.FLAG_AUTO_CANCEL;

    Intent clickIntent = new Intent(this, MainActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, clickIntent, PendingIntent.FLAG_CANCEL_CURRENT);

    notification.setLatestEventInfo(this, title, text, pendingIntent);

    NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    manager.notify(NOTIFICATION_ID, notification);
}   
// ...  
}

sendNotification() is called at 2 PM, because of the AlarmManager : 因为AlarmManager ,在下午2点调用sendNotification()

public class ServiceStarter extends BroadcastReceiver
{

@Override
public void onReceive(Context context, Intent intent)
{
    setNotificationAlarm(context);
}

/** Set repeating notifications every 24 hours. */
public static void setNotificationAlarm(Context context)
{
    Intent intent = new Intent(context, NotifyService.class);
    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);

    final int oneDay = 24 * 60 * 60 * 1000;
    alarmManager.setRepeating(AlarmManager.RTC, getTriggerTime(), oneDay, pendingIntent);
}

private static long getTriggerTime()
{
    GregorianCalendar calendar = new GregorianCalendar();
    calendar.set(GregorianCalendar.HOUR_OF_DAY, 14);
    calendar.set(GregorianCalendar.MINUTE, 0);
    calendar.set(GregorianCalendar.SECOND, 0);
    calendar.set(GregorianCalendar.MILLISECOND, 0);

    if (calendar.before(new GregorianCalendar()))
    {
        calendar.add(GregorianCalendar.DAY_OF_MONTH, 1);
    }

    return calendar.getTimeInMillis();
}

}

setNotificationAlarm() is called from 2 places. setNotificationAlarm()从2个地方调用。 First, at the start of app. 首先,在应用程序的开头。 Second, from the code above, when the phone reboots ( onReceive() receives BOOT_COMPLETED ). 其次,从上面的代码中,当手机重新启动时( onReceive()收到BOOT_COMPLETED )。 I do that, because when the user turns the phone off, AlarmManager clears its alarms. 我这样做,因为当用户关闭手机时, AlarmManager清除其警报。

So all should work, because alarmManager.setRepeating() overrides previous alarm. 所以都应该工作,因为alarmManager.setRepeating()会覆盖以前的警报。

I have found that somebody had the same issue, but also no answer: 我发现有人有同样的问题,但也没有答案:
https://groups.google.com/forum/?fromgroups=#!topic/android-developers/t_tDU4PwR3g https://groups.google.com/forum/?fromgroups=#!topic/android-developers/t_tDU4PwR3g

Also here I found similar problem: http://comments.gmane.org/gmane.comp.handhelds.android.devel/171471 在这里我也发现了类似的问题: http//comments.gmane.org/gmane.comp.handhelds.android.devel/171471

Some time ago I asked how to create such notifications, so this is related: 前段时间我问过如何创建这样的通知,所以这是相关的:
Everyday notifications at certain time 特定时间的日常通知

Use AlarmManager.RTC_WAKEUP insted of AlarmManager.RTC 使用AlarmManager.RTC中的AlarmManager.RTC_WAKEUP

In AlarmManager.RTC 在AlarmManager.RTC中

Alarm time in System.currentTimeMillis() (wall clock time in UTC). System.currentTimeMillis()中的闹钟时间(以UTC为单位的挂钟时间)。 This alarm does not wake the device up; 此警报不会唤醒设备; if it goes off while the device is asleep, it will not be delivered until the next time the device wakes up. 如果在设备处于睡眠状态时熄灭,则在下次设备唤醒时才会发送。

where as in AlarmManager.RTC_WAKEUP 在AlarmManager.RTC_WAKEUP中的位置

Alarm time in System.currentTimeMillis() (wall clock time in UTC), which will wake up the device when it goes off. System.currentTimeMillis()中的闹钟时间(以UTC为单位的挂钟时间),它将在设备关闭时唤醒设备。

Had the same problem on ICS+ device. 在ICS +设备上遇到同样的问题。 My workaround was very simple-> Put the current time in the shared preferences when the notification is shown. 我的解决方法非常简单 - >在显示通知时将当前时间放在共享首选项中。 Before that always check if the interval is really passed, and if not just abort. 在此之前总是检查间隔是否真的通过,如果不是中止。

            long lastnotification = sharedPrefs.getLong("lnnd", -1);
            Calendar now = Calendar.getInstance();
            if (!namedayalarmEnabled) {
                    return;
            }
            if (lastnotification > 1) {
                    Calendar last = Calendar.getInstance();
                    last.setTimeInMillis(lastnotification);
                    long distance = (now.getTimeInMillis() - last
                                    .getTimeInMillis());
                    if (distance < YOURINTERVAL) {
                            return;
                    } else {
                            SharedPreferences.Editor editor = sharedPrefs.edit();
                            editor.putLong("lnnd", now.getTimeInMillis());
                            editor.commit();
                    }
            }

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

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