2016-07-27 52 views
0

Ich möchte 2 verschiedene oder mehr notification mit verschiedenen id 's aber die zweite ersetzt die erste. Ich habe eine reciver Klasse broadcastReciever erstreckt:zwei Benachrichtigung mit zwei verschiedenen IDs aber 2. ersetzen die 1.

public class NotificationPublisher extends BroadcastReceiver { 

public static String NOTIFICATION_ID = "notification-id"; 
public static String NOTIFICATION = "notification"; 


@Override 
public void onReceive(Context context, Intent intent) { 
    NotificationManager notificationManager = 
      (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 
    Notification notification = intent.getParcelableExtra(NOTIFICATION); 
    int id = intent.getIntExtra(NOTIFICATION_ID, 0); 
    Log.i("ShowNotifyID:", "" + id); 
    notificationManager.notify(id, notification); 

    } 

} 

Und in meiner Tätigkeit i schaffen notifications mit einzigartigen id und geben sie noteID ist einzigartig für reciver.Each und i retrive es von DataBase .when der Benutzer Klick auf den Button einstellen Benachrichtigung wird die Methode onClickSet() aufgerufen.

public class ReminderActivity extends Activity { 

int    noteID; 
String   noteText; 
DatePicker  datePicker; 
TimePicker  timePicker; 
NoteManagerHelper db; 
int    isReminder; 
NoteItem   item; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.reminder_activity); 
    datePicker = (DatePicker) findViewById(R.id.datePicker1); 
    timePicker = (TimePicker) findViewById(R.id.timePicker1); 

    Intent reminderIntent = getIntent(); 
    noteText = reminderIntent.getStringExtra("noteText"); 
    noteID = reminderIntent.getIntExtra("noteID", 0); 
    db = new NoteManagerHelper(this); 
    isReminder = db.getIsReminder(noteID); 
    if (isReminder == 1) { 
     setShowingTimeInDatePicker(); 
    } 

} 


private void setShowingTimeInDatePicker() { 
    NoteItem item = db.getAnItem(noteID); 
    datePicker.updateDate(item.reminderYear, item.reminderMonth, item.reminderday); 
    Log.i("DATEPICKER:" + item.reminderYear, "" + item.reminderMonth + "-" + item.reminderday); 
    timePicker.setCurrentHour(item.reminderHour); 
    timePicker.setCurrentMinute(item.reminderMinute); 
} 


public void onClickSet(View v) {//onClick of the set button 
    //if isreminder is not 0 that means this note already have a notifaction in receiver, we need to cancel it first and start a new one 
    if (isReminder == 1) { 
     removeReminderFromService(); 
    } 
    Calendar calendar = Calendar.getInstance(); 
    calendar.set(Calendar.MONTH, datePicker.getMonth()); 
    calendar.set(Calendar.YEAR, datePicker.getYear()); 
    calendar.set(Calendar.DAY_OF_MONTH, datePicker.getDayOfMonth()); 
    calendar.set(Calendar.HOUR_OF_DAY, timePicker.getCurrentHour()); 
    calendar.set(Calendar.MINUTE, timePicker.getCurrentMinute()); 
    calendar.set(Calendar.SECOND, 0); 
    long delay = calendar.getTimeInMillis() - Calendar.getInstance().getTimeInMillis(); 

    item = new NoteItem(noteID, noteText); 
    item.isReminder = 1; 
    item.reminderYear = datePicker.getYear(); 
    item.reminderMonth = datePicker.getMonth(); 
    item.reminderday = datePicker.getDayOfMonth(); 
    item.reminderHour = timePicker.getCurrentHour(); 
    item.reminderMinute = timePicker.getCurrentMinute(); 

    db.setReminder(item); 

    scheduleNotification(getMyNotification(noteText), delay); 

    finish(); 

} 


public void onClickRemoveReminder(View v) { 
    if (isReminder == 1) { 
     removeReminderFromService(); 
     db.setIsReminderToDB(noteID, 0); 
     isReminder = 0; 
     Toast.makeText(ReminderActivity.this, "Reminder removed successfully!", Toast.LENGTH_SHORT).show(); 
    } 
    else { 
     Toast.makeText(ReminderActivity.this, "No reminder set yet!", Toast.LENGTH_SHORT).show(); 
    } 

} 


public void removeReminderFromService() { 
    NotificationManager notificationManager = 
      (NotificationManager) (ReminderActivity.this).getSystemService(Context.NOTIFICATION_SERVICE); 
    notificationManager.cancel(noteID); 
    Log.i("RemoveNotifyID:", "" + noteID); 

} 


public void scheduleNotification(Notification notification, long delay) { 
    Intent notificationIntent = new Intent(this, NotificationPublisher.class); 
    notificationIntent.putExtra(NotificationPublisher.NOTIFICATION_ID, noteID); 
    notificationIntent.putExtra(NotificationPublisher.NOTIFICATION, notification); 
    PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT); 
    long futureInMillis = SystemClock.elapsedRealtime() + delay; 
    Toast.makeText(this, "" + futureInMillis, Toast.LENGTH_SHORT).show(); 
    AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); 
    alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, futureInMillis, pendingIntent); 

} 


private Notification getMyNotification(String content) { 
    Notification.Builder builder = new Notification.Builder(this); 
    builder.setContentTitle("Rnote"); 
    builder.setAutoCancel(false); 
    builder.setContentText(content); 
    builder.setSmallIcon(R.drawable.ic_launcher); 
    return builder.getNotification(); 
    } 

} 

Ich habe ähnliche Fragen gelesen. aber die Lösung war meist über verschiedene id's. Kann jemand mir helfen? Jede Hilfe wird

Antwort

0

geschätzt werden Sie haben auch die notifiaction_id in der anhängigen Absicht passieren,

Pass die notification_id

PendingIntent pendingIntent = PendingIntent.getBroadcast(this, notification_id, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT); 

statt 0 in der anhängigen Absicht

PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);