2016-06-06 3 views
0

Я пытаюсь установить ежедневное уведомление в android. С помощью будильника следующий код.Android: Local Notification пропуски

public class DrawerActivity extends AppCompatActivity { 
@Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_drawer_menu); 
     Intent alarmIntent = new Intent(DrawerActivity.this, LocalNotificationReceiver.class); 
    dailyNotificationPendingIntent = PendingIntent.getBroadcast(DrawerActivity.this, 0, alarmIntent, 0); 

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



    Calendar calendar = Calendar.getInstance(); 
    calendar.set(Calendar.HOUR_OF_DAY, 22); 
    calendar.set(Calendar.MINUTE, 0); 
    calendar.set(Calendar.SECOND, 0); 
    calendar.set(Calendar.MILLISECOND, 0); 
    dailyNotificationManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, dailyNotificationPendingIntent); 

    } 
} 

Это мой сигнал тревоги приемник

public class LocalNotificationReceiver extends WakefulBroadcastReceiver { 
    private final static String TAG = "LocalNotificationReceiv"; 
    public LocalNotificationReceiver(){} 
    Realm realm; 
    private NotificationUtils notificationUtils; 
    String currentStartDate; 
    SimpleDateFormat dateFormat; 
    public NumberFormat numberFormat = NumberFormat.getInstance(Locale.US); 
    @Override 
    public void onReceive(Context context, Intent intent) { 

     realm = Realm.getDefaultInstance(); 
     Calendar calStartDate = Calendar.getInstance(); 

     dateFormat = new SimpleDateFormat("yyyy-MM-dd"); 
     currentStartDate = dateFormat.format(calStartDate.getTime()); 

     SweatPointFitnessDB sweatPointFitnessDB = realm.where(SweatPointFitnessDB.class) 
                .equalTo("forday",currentStartDate) 
                .findFirst(); 

     String title; 
     String alert; 

     try{ 
      if (sweatPointFitnessDB.getStepCount() != 0) { 
       Log.i(TAG, "Yout step count today notification: " + sweatPointFitnessDB.getStepCount()); 
       title = "Hey you did " + numberFormat.format(sweatPointFitnessDB.getStepCount()) + " steps today!!"; 
       alert = "Kudos"; 

      } else { 
       title = "You were inactive today!!"; 
       alert = "Buckle up"; 
      } 



      Log.i(TAG, "inside local notification receiver"); 
      Intent resultIntent = new Intent(context, MainActivity.class); 
      showNotificationMessage(context, title, alert, resultIntent); 

     }catch (Exception e){ 

     } 




    } 


    private void showNotificationMessage(Context context, String title, String message, Intent intent) { 

     notificationUtils = new NotificationUtils(context); 

     // intent.putExtras("title",title); 


     intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); 

     notificationUtils.showNotificationMessageNew(title, message, intent); 
    } 

} 

Наконец это мое уведомление деятельность.

public void showNotificationMessageNew(String title, String message, Intent intent){ 
      if(TextUtils.isEmpty(message)){ 
       return; 
      } 


       int icon = R.drawable.push_icon; 

       int mNotificationId = 100; 

       PendingIntent resultPendingIntent = 
         PendingIntent.getActivity(
           mContext, 
           0, 
           intent, 
           PendingIntent.FLAG_CANCEL_CURRENT 
         ); 

       NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle(); 

       NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(mContext); 
       Notification notification = mBuilder.setSmallIcon(icon).setTicker(title).setWhen(0) 
         .setAutoCancel(true) 
         .setContentTitle(title) 
         .setStyle(inboxStyle) 
         .setContentIntent(resultPendingIntent) 
         .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)) 
         .setLargeIcon(BitmapFactory.decodeResource(mContext.getResources(), icon)) 
         .setContentText(message) 
         .build(); 

       NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE); 
       notificationManager.notify(mNotificationId, notification); 



     } 

Теперь проблема в том, что я получаю уведомление каждую ночь в 10 вечера, что хорошо. Но после нажатия на нее и ввода приложения уведомление отображается снова и снова.

ответ

2

Вы вызываете локальное уведомление каждый раз из DrawerActivity, сохраняете предпочтение, что локальные уведомления являются расписанием и проверяют его перед планированием в следующий раз.

В настоящее время каждый раз, когда DrawerActivity onCreate называется вызванным уведомлением.