如何在特定条件下停止通知

时间:2017-01-03 09:55:13

标签: android sqlite notifications alarmmanager android-broadcastreceiver

我的应用程序根据存储在数据库中的时间生成通知,就像当用户点击通知按钮时,所有警报都将在后面设置,并且每天都会生成通知,现在问题出现在我的时候按下通知按钮,即使不是noti的时间,也会在按下时立即生成通知,我发现这是由于数据库中的时间。已经过去了,例如。我在2:30:00按通知按钮但在我的数据库中存储了2:28:00的时间,因此它会生成该时间的通知。因为我已经测试了设置时间,然后是当前的,并且工作正常(就像点击时没有生成通知一样)。 那么如何停止获取前一次的通知?

  for (int i = 0; i < C.getCount(); i++) {


        h[i] = Integer.valueOf(DealTimes[i].split(":")[0]);
        m[i] = Integer.valueOf(DealTimes[i].split(":")[1]);
        //   Toast.makeText(getBaseContext(), "Time at 16th index " + h + "min " + m, Toast.LENGTH_SHORT).show();

        Calendar calendar = Calendar.getInstance();

        calendar.set(Calendar.HOUR_OF_DAY, h[i]);
        calendar.set(Calendar.MINUTE, m[i]);
        calendar.set(calendar.SECOND, 0);
        calendar.set(calendar.MILLISECOND, 0);
        A[i] = calendar.getTimeInMillis();


        //     Toast.makeText(getBaseContext(), "Final calnder Time in mili sec " + A[i] , Toast.LENGTH_SHORT).show();
        //   Toast.makeText(getBaseContext(), "Final calnder Only Time  " + calendar.getTime() + " Time zone " + calendar.getTimeZone(), Toast.LENGTH_SHORT).show();

        Toast.makeText(getBaseContext(), "Setting Alarm", Toast.LENGTH_SHORT).show();


        Intent alertIntent = new Intent(this, AlertReceiver.class);

        final int _id = (int) System.currentTimeMillis();

        AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

        alarmManager.set(AlarmManager.RTC_WAKEUP, A[i],
                PendingIntent.getBroadcast(this, _id, alertIntent, PendingIntent.FLAG_UPDATE_CURRENT));

        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,Allah[i],AlarmManager.INTERVAL_DAY, PendingIntent.getBroadcast(this, _id, alertIntent, PendingIntent.FLAG_UPDATE_CURRENT));


    }

这是我的广播接收者代码

         private static int NOTIFICATION_ID = 1;
    NotificationManager notificationManager;
    private PendingIntent pendingIntent;

    SQLiteDB handler;

    @Override
    public void onReceive(Context context, Intent intent) {
        String myUserid = "1"; // = new SavedPreferences(context).getUserId();

        long[] blah = intent.getLongArrayExtra("id");
        handler = new SQLiteDB(context);
        handler.open();
        Cursor C = handler.returnData();

        String notificationUserId = intent.getStringExtra("NotificationUserId");
        if (!TextUtils.isEmpty(myUserid) && myUserid.equals(notificationUserId)) {
            String bookingId = intent.getStringExtra("bookingId");
            System.out.println("SP ID IN RECEIVER"+intent.getStringExtra("spId"));
            String spId = intent.getStringExtra("spId");
// change before using for real
            for (int i = 0; i <C.getCount(); i++) {
                if (new Date().getTime() > blah[i]) {//dbTime) {// Here time is past so need not to play notification.
                } else {// Notification time is in future. So can play the notification.
                    notificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
                    Intent mIntent = new Intent(context, MainActivity.class);
                    NOTIFICATION_ID = Integer.parseInt(intent.getStringExtra("Id"));
                    mIntent.putExtra("message", intent.getStringExtra("message"));
                    mIntent.putExtra("Id", NOTIFICATION_ID);
                    mIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    pendingIntent = PendingIntent.getActivity(context, 111, mIntent, PendingIntent.FLAG_UPDATE_CURRENT);
                    NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
                    Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
                    builder.setSound(uri);
                    builder.setAutoCancel(true);
                    builder.setContentTitle("Test");
                    builder.setContentText(intent.getStringExtra("message"));
                    builder.setSmallIcon(R.drawable.cast_ic_notification_forward);
                    builder.setContentIntent(pendingIntent);
                    //  builder.setStyle(new NotificationCompat.BigTextStyle().bigText(context.getString(R.string.service_completed_text)));
                    notificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
                    notificationManager.notify(NOTIFICATION_ID, builder.build());
                }
            }
        }

2 个答案:

答案 0 :(得分:0)

如果要生成完全时间匹配的通知,则可以将时间(当前时间和先前存储的时间)转换为字符串,然后您可以轻松使用String.Equals()功能。但是在当前场景中,如果用户在存储的时间过后按下按钮,那么他将错过该通知。据我所知,您应该使用service根据时间生成通知。这只是我的建议。

答案 1 :(得分:0)

在这里,我正在为您发布一个示例。请一步一步地进行。这真的有帮助。

  1. 在设置闹钟的地方,只需使用自定义广播接收器即可。我将在下面提到接收器代码。

    AlarmManager beforeAlarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    Intent alarmIntentBefore = new Intent(NotificationRequestActivity.this, AlarmReceiver.class);
                        alarmIntentBefore.putExtra("id", bookingId);
                        alarmIntentBefore.putExtra("message", "");
    
    PendingIntent pendingIntentBefore = PendingIntent.getBroadcast(NotificationRequestActivity.this,
                       111, alarmIntentBefore, 0);
    beforeAlarmManager.set(AlarmManager.RTC_WAKEUP, new Date().getTime(), pendingIntentBefore);
    
  2. 现在创建一个警报接收器。 此处我们将检查闹钟时间是否已过,然后我们将不会生成通知。否则会生成。

  3. 创建名为BroadcastReceiver的{​​{1}}:

    AlarmReceiver.java
    1. 在清单中注册闹钟接收器。