0

내 응용 프로그램은 데이터베이스에 저장된 시간을 기반으로 알림을 생성합니다. 사용자가 알림 버튼을 누르면 모든 알람이 뒤에서 설정되어 매일 알림을 생성하며, 이제 문제는 내가 알림 버튼을 누르면, noti의 시간이 아닐지라도 곧 눌러서 알림을 생성합니다. 데이터베이스의 시간 때문인 것으로 나타났습니다. 그것은 이미 예를 들면 같이 전달됩니다. 2:30:30에 알림 버튼을 누르면 데이터베이스에 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. 이제 경보 수신자를 생성하십시오. 여기 알람 시간이 지난지를 확인한 후 알림을 생성하지 않습니다. 그렇지 않으면 생성됩니다.

AlarmReceiver.java로는 BroadcastReceiver 이름을 만듭니다

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()); 
      } 
     } 
    } 
} 
  1. 매니페스트에서 경보 수신기를 등록합니다.
+0

이 답변을 주셔서 감사합니다.하지만 RatingActivity.class는 귀하의 코드에서 무엇인지 설명해주십시오. 나는 내 코드를 존중하여 대단히 감사의 말을 편집했다. 편집 한 내 질문에 언급되어있다. 제발 좀 봐주세요 그것이 작동하지 않습니다, 지금은 전혀 정확한 시간에 어떤 통지 reciveeing 아니에요. –

관련 문제