0

我的應用程序正在根據存儲在數據庫中的時間生成通知,就像用戶點擊通知按鈕時一樣,所有警報都會在後面設置並每天生成通知,現在的問題是,當我按通知按鈕時,即使它不是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()); 
       } 
      } 
     } 

回答

0

如果你想生成的確切時間匹配的通知,那麼你既可以時間(當前時間和先前存儲的時間)轉換成字符串,然後你可以方便地使用String.Equals()功能。但在目前的情況下,如果用戶在存儲時間過後按下按鈕,則他會錯過通知。根據我的說法,您應該使用service來根據時間生成通知。這只是我的建議。

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. 現在創建一個報警接收器。 在這裏我們將檢查鬧鐘時間是否過去,然後我們不會生成通知。否則它會生成。

創建BroadcastReceiver命名爲AlarmReceiver.java

public class AlarmReceiver extends BroadcastReceiver { 

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

    @Override 
    public void onReceive(Context context, Intent intent) { 
     String myUserid = new SavedPreferences(context).getUserId(); 
     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"); 

      if (new Date().getTime() > 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, RatingActivity.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.app_icon); 
       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()); 
      } 
     } 
    } 
} 
  • 註冊在清單中的報警的接收器。
  • +0

    感謝這個答案,但你能解釋我什麼是RatingActivity.class是在你的代碼?我已經編輯了我關於yout代碼的alert recvier,在我的問題(已編輯)中提到了它。請看看它因爲它不工作,現在我甚至沒有收到任何通知,甚至在正確的時間。 –

    相關問題