【问题标题】:Getting all notifications at same time同时获取所有通知
【发布时间】:2016-03-09 18:38:31
【问题描述】:

我已使用警报管理器为事件设置通知。如果我在不同时间设置多个通知,那么最后一个通知也会在最后一个通知中出现。

如果开关关闭,我还想取消特定通知。我也有同样的意图取消通知。但我认为所有通知都被取消了。

我还创建了时间表,事件是使用时间表的 ID 创建的。现在,如果我删除一个时间表,我想删除所有为属于已删除时间表的事件设置的通知。

设置闹钟:

public void setNotificationTime(Calendar c)
{

    Date dateFrom = new Date();
    df = new SimpleDateFormat("E MMM dd HH:mm:ss zzzz yyyy");
    try {
        dateFrom = df.parse(startTime);
    }
    catch (ParseException ex) {

    }

    dateFrom.getTime();
    c.setTime(dateFrom);

    hour = c.get(Calendar.HOUR_OF_DAY);
    minute = c.get(Calendar.MINUTE);

    if(notificationTime !=null  &&  !notificationTime.isEmpty()) {


        if (notificationTime.equals("10 Minutes Before")) {

            FLAG = 1;

            c.set(Calendar.HOUR_OF_DAY, hour);
            c.set(Calendar.MINUTE, minute - 10);
            c.set(Calendar.SECOND, 0);
            c.set(Calendar.MILLISECOND, 0);
            c.set(Calendar.DATE, day);
            // c.set(Calendar.DAY_OF_WEEK,);

            SetDay(c);

            notification = c.getTime();
            notificationTime = df.format(notification);

           // setAlarm(c, FLAG);
            Intent intent = new Intent(getBaseContext(), NotificationReceiver.class);
            PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), RQS_1, intent, 0);
            AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
            alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 7, pendingIntent);

            Toast.makeText(getApplicationContext(), notificationTime, Toast.LENGTH_SHORT).show();


        } else if (notificationTime.equals("30 Minutes Before")) {

            FLAG = 2;

            c.set(Calendar.HOUR_OF_DAY, hour);
            c.set(Calendar.MINUTE, minute - 30);
            c.set(Calendar.SECOND, 0);
            c.set(Calendar.MILLISECOND, 0);
            c.set(Calendar.DATE, day);
            // c.set(Calendar.DAY_OF_WEEK,);

            SetDay(c);

            notification = c.getTime();
            notificationTime = df.format(notification);

            Intent intent = new Intent(getBaseContext(), NotificationReceiver.class);
            PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), RQS_1, intent, 0);
            AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
            alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 7, pendingIntent);

            Toast.makeText(getApplicationContext(), notificationTime, Toast.LENGTH_SHORT).show();

          //  setAlarm(c,FLAG);

        }

通知接收器

public class NotificationReceiver  extends BroadcastReceiver {


public static int MY_NOTIFICATION_ID = 0;
NotificationManager notificationManager;
Notification myNotification;

EventTableHelper db;

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


    Toast.makeText(context, "Time is set", Toast.LENGTH_LONG).show();

    db = new EventTableHelper(context);

    List<EventData> testSavings = db.getAllEvents();

    for (EventData ts : testSavings) {
        String log = "from date:" + ts.getFromDate()
                + " ,to date: " + ts.getToDate()
                + " ,location: " + ts.getLocation()
                + " ,title " + ts.getTitle();

        Calendar c = Calendar.getInstance();
        Date date = new Date();
        Date date1 = new Date();
        Log.d("Result: ", log);

        SimpleDateFormat df = new SimpleDateFormat("E MMM dd hh:mm:ss zzzz yyyy");
        SimpleDateFormat df2 = new SimpleDateFormat("hh:mm a");

        try {
            date = df.parse(ts.getFromDate());
            date1 = df.parse(ts.getToDate());
        } catch (ParseException ex) {

        }
        String timeFrom = df2.format(date);
     //   String startTime = String.valueOf(timeFrom);

        String timeTo = df2.format(date1);
       // String endTime = String.valueOf(timeTo);


        String location = ts.getLocation();
        String title = ts.getTitle();


        Intent myIntent = new Intent(context, MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(
                context,
                0,
                myIntent,
                PendingIntent.FLAG_UPDATE_CURRENT);

        if(location.equals(""))
        {
            String msg = "From : " + timeFrom + "\nTo : " + timeTo;

            myNotification = new NotificationCompat.Builder(context)
                    .setContentTitle("Event : " + title)
                    .setContentText(msg)
                    .setWhen(System.currentTimeMillis())
                    .setContentIntent(pendingIntent)
                    .setAutoCancel(true)
                    .setSmallIcon(R.drawable.eventicon)
                    .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
                    .setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
                    .setDefaults(Notification.DEFAULT_SOUND)
                    .build();

        }

        else
        {
            String msg = "From : " + timeFrom + "\nTo : " + timeTo + "\nAt : " + location;
            myNotification = new NotificationCompat.Builder(context)
                    .setContentTitle("Event : " + title)
                    .setContentText(msg)
                    .setWhen(System.currentTimeMillis())
                    .setContentIntent(pendingIntent)
                    .setAutoCancel(true)
                    .setSmallIcon(R.drawable.eventicon)
                    .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
                    .setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
                    .setDefaults(Notification.DEFAULT_SOUND)
                    .build();

        }

        Log.i("Notify", "Notification");
        notificationManager =
                (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(MY_NOTIFICATION_ID, myNotification);

        myNotification.flags=Notification.FLAG_AUTO_CANCEL;

        Intent i = new Intent();
        i.putExtra("notificationId",MY_NOTIFICATION_ID);

        MY_NOTIFICATION_ID ++;

    }


 }
}

【问题讨论】:

  • Calendar 类对于操作日期和时间来说是可怕的。我建议你看看joda-time-android
  • 为什么是 db.getAllEvents() ??
  • 从数据库中获取事件详情。@Zoedia

标签: java android alarmmanager android-notifications


【解决方案1】:

每次调用 broadcastReceiver 时都会重置 MY_NOTIFICATION_ID,因此所有通知都具有相同的 id,即 0。 我为避免它所做的就是使用:

Random rand = new Random();
int id = rand.nextInt(1000000) + 1;  

所以每个通知肯定会得到一个不同的 ID 号(百万分之一的机会它会重复自己)。

【讨论】:

  • 我试过这个,但我希望每周重复一次通知。它没有重复。 @Morza
  • 我可以想到 3 种方法来处理它: 1. 设置一个每周发送通知的重复警报(我不确定它是如何工作的,因为我从未尝试过)。 2.每次弹出通知时再次设置为下周,并带有一个新的id号。 3.将id号保存在SharedPreferences中,然后从那里获取。
猜你喜欢
  • 1970-01-01
  • 2013-03-12
  • 1970-01-01
  • 2011-03-03
  • 2010-11-06
  • 2023-02-04
  • 2011-04-18
  • 2015-04-16
  • 1970-01-01
相关资源
最近更新 更多