2017-07-04 103 views
0

我app.Whenever其實我已經實現了我的通知點擊一個按鈕,它設定時間通知和呼叫廣播receiver.but 3個通知像一個請求爲下午3點,下午4點的時候下午5點發送它只發送下午5點通知不是下午3點和下午4點,因爲下午5點發生一次最後一次觸發。我認爲它與通知ID有關。我通過id爲0.我應該做什麼,請幫助。與alarmmanager和廣播接收器發送通知

我的廣播接收器

public class NotificationMessage extends BroadcastReceiver { 

    @Override 
    public void onReceive(Context context, Intent arg1) { 
     Log.d("Tag","dd"); 
     showNotification(context); 
    } 

    private void showNotification(Context context) { 
     Log.d("Tag", "visible"); 

     PendingIntent contentIntent = PendingIntent.getActivity(context, 0, 
       new Intent(context, MainActivity.class), 0); 


     NotificationCompat.Builder mBuilder = 
       new NotificationCompat.Builder(context) 
         .setSmallIcon(R.drawable.logo) 
         .setContentTitle("HURRY UP!!") 
         .setContentText("You have a session in 15 minutes."); 
     mBuilder.setContentIntent(contentIntent); 
     mBuilder.setDefaults(Notification.DEFAULT_SOUND); 
     mBuilder.setAutoCancel(true); 
     NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 

     SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context); 
     int notiid = preferences.getInt("Id", -1); 

     int notification_id = notiid; 
     Log.d("ID1", String.valueOf(notification_id)); 

     mNotificationManager.notify(notification_id, mBuilder.build()); 
     Log.d("Tag","fuck u"); 
    } 
} 

這正是我打電話:

holder.accept.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(final View v) { 
       Call<AcceptedDealResponse> call = service.acceptedDeal(acceptDealRequest); 
       call.enqueue(new Callback<AcceptedDealResponse>() { 
        @Override 
        public void onResponse(Call<AcceptedDealResponse> call, Response<AcceptedDealResponse> response) { 
         AcceptedDealResponse response1 = response.body(); 
         if(response.body().getMessage().equals("Successful !! Deal Booked !!")){ 
          Log.d(",,", "" + " deal boooked"); 


          Log.d("jkhdjksahda ","" + newDealResponsess.getResponses().get(position).getTimeFrom()); 


          DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); 
          try { 
           date1 = formatter.parse(String.valueOf(newDealResponsess.getResponses().get(position).getTimeFrom())); 

          } catch (ParseException e) { 
           e.printStackTrace(); 
          } 

          cal = Calendar.getInstance(); 
          cal.setTime(date1); 

          Log.d("Tag", String.valueOf(cal.get(Calendar.DATE))); 


          calendar = Calendar.getInstance(); 

          calendar.set(Calendar.YEAR, cal.get(Calendar.YEAR)); 
          calendar.set(Calendar.MONTH, cal.get(Calendar.MONTH)); 
          calendar.set(Calendar.DATE, cal.get(Calendar.DATE)); 
          calendar.set(Calendar.HOUR_OF_DAY, cal.get(Calendar.HOUR_OF_DAY)); 
          calendar.set(Calendar.MINUTE, cal.get(Calendar.MINUTE)); 
          calendar.set(Calendar.SECOND, cal.get(Calendar.SECOND)); 

          Log.d("hdfbsdhbfs :", "" + calendar.get(Calendar.HOUR_OF_DAY)); 
          Log.d("hdfbsdhbfs :", "" + calendar.get(Calendar.MINUTE)); 
          Log.d("hdfbsdhbfs :", "" + calendar.get(Calendar.SECOND)); 
          Log.d("hdfbsdhbfs :", "" + calendar.get(Calendar.DATE)); 
          Log.d("hdfbsdhbfs :", "" + calendar.get(Calendar.MONTH)); 
          Log.d("hdfbsdhbfs :", "" + calendar.get(Calendar.YEAR)); 

          Intent notificationmassage = new Intent(v.getContext(),NotificationMessage.class); 

          Log.d("Tag","cool"); 

          PendingIntent pi = PendingIntent.getBroadcast(v.getContext(), 0,notificationmassage, PendingIntent.FLAG_UPDATE_CURRENT); 


          Log.d("Tag","cooled"); 
          AlarmManager am = (AlarmManager) v.getContext().getSystemService(ALARM_SERVICE); 
          Log.d("Tag","cooooled"); 

          int id = (int) calendar.getTimeInMillis(); 

          SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(v.getContext()); 
          SharedPreferences.Editor editor = preferences.edit(); 
          editor.putInt("Id", id); 
          Log.d("ID", String.valueOf(id)); 
          editor.apply(); 


          am.set(AlarmManager.RTC_WAKEUP, (calendar.getTimeInMillis() - 900000), pi); 
+0

是添加不同的ID,將工作 – Pavya

+0

'我認爲這與通知id'相關你是對的。 '我傳遞id爲0',然後每次都用這個ID覆蓋通知。 '我應該do'使用不同的ID –

+0

如何使用它們這是一個問題@普拉 –

回答

0

我想這個問題是在你的PendingIntent與同REQUEST_CODE。可能使用AlarmManager設置的alarm當你使用相同的REQUEST_CODEPendingInetent所有alarm

嘗試使用不同REQUEST_CODE針對不同PendingIntent爲 不同Alarm

下面的代碼使用使用AlarmManager設置Alarm

int notiId = UNIQUE_ID; 

    // Intent 
    Intent mIntent = new Intent(getApplicationContext(), NotificationMessage.class); 
    mIntent.putExtra("NOTIFICATION_ID", notiId); 

    // Pending broadcast intent 
    PendingIntent mPI = PendingIntent.getBroadcast(getApplicationContext(), 
         notiId, mIntent, PendingIntent.FLAG_UPDATE_CURRENT); 

    // Alarm manager 
    AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); 
    alarmManager.set(AlarmManager.RTC_WAKEUP, TARGET_TIME_MILLISECONDS, mPI); 

希望這將工作〜