2016-07-14 6 views
0

Ich mache eine Anwesenheitsüberwachung. Also, ich muss Benachrichtigungen vor Klassenbeginn und nach dem Klassenstart auslösen.Mehrere Benachrichtigungen, nur die erste startet

Hauptmethode ist, wo ich Benachrichtigungen für jeden Kurs einer Datenbank erstellen.

mainMethod() { 
    testAlarmA(s[0], s[2]); 
    testAlarmB(s[0], s[2]); 
} 

Ich habe implementiert setAction() -Methode für jede Absicht, und haben auch unterschiedliche Anforderungscodes für jede anhängige Absicht

private void testAlarmA (String code) { 
    Log.i("attendance", "class-start"); 
    AlarmManager alarmManager = (AlarmManager) getContext().getSystemService(Context.ALARM_SERVICE); 

    Intent notificationIntent = new Intent("android.media.action.DISPLAY_ATTENDANCE_NOTIFICATION"); 
    notificationIntent.addCategory("android.intent.category.DEFAULT"); 
    notificationIntent.putExtra("type", "start"); 
    notificationIntent.putExtra("courseCode", code); 
notificationIntent.setAction(Long.toString(System.currentTimeMillis())); 

    Calendar c = Calendar.getInstance(); 
    c.add(Calendar.MINUTE, 1); 

    PendingIntent broadcast = PendingIntent.getBroadcast(getContext(), 111, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT); 

    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), (2 * 60 * 1000), broadcast); 
} 

private void testAlarmB (String code){ 
    Log.i("attendance", "class-end"); 
    AlarmManager alarmManager = (AlarmManager) getContext().getSystemService(Context.ALARM_SERVICE); 

    Intent notificationIntent = new Intent("android.media.action.DISPLAY_ATTENDANCE_NOTIFICATION"); 
    notificationIntent.addCategory("android.intent.category.DEFAULT"); 
    notificationIntent.putExtra("type", "end"); 
    notificationIntent.putExtra("courseCode", code); 
    notificationIntent.setAction(Long.toString(System.currentTimeMillis())); 

    Calendar c = Calendar.getInstance(); 
    c.add(Calendar.MINUTE, 2); 

    PendingIntent broadcast = PendingIntent.getBroadcast(getContext(), 222, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT); 

    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), (2 * 60 * 1000), broadcast); 

} 

In per Broadcast (ich habe diese in früheren Fragen auf dieser Seite gelesen) Empfängerklasse, ich extrahiere Extras, um zu wissen, welche Benachrichtigung ausgelöst wird, dh Start oder Ende der Klasse.

Aber nur Klassenanfang notif ist Empfänger, Ende der Klasse Log Entry wird nie angezeigt.

@Override 
public void onReceive(Context context, Intent intent) { 
    Log.i("time", "onReceive"); 
    mNotificationManager = 
      (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 
    manager = mNotificationManager; 

     if (intent.getExtras().containsKey("type")) { 
      Log.i("time", "containsKey"); 

      String type = intent.getExtras().getString("type"); 
      if (type.equals("end")) { 
       Log.i("time", "end"); 
       endNotif(context); 

      } else if (type.equals("start")) { 
       Log.i("time", "start"); 
       startNotif(context); 

      } 
    } 

} 

Ich habe auch Benutzer eindeutige ID für Benachrichtigung Builder.

private void startNotif (Context context) { 
    Intent notificationIntent = new Intent(); 
    notificationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); 
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT); 

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context) 
      .setContentTitle("You Have " + courseCode + " at " + roomNo) 
      .setContentIntent(pendingIntent) 
      .setDefaults(Notification.DEFAULT_VIBRATE) 
      .setPriority(Notification.PRIORITY_DEFAULT) 
      .setVisibility(Notification.VISIBILITY_PUBLIC) 
      .setSmallIcon(R.drawable.ic_library_books_white_24dp) 
      .setLights(0xff00ff00, 1000, 1000) 
      .setAutoCancel(true); 

    mNotificationManager.notify(1, mBuilder.build()); 
} 

Meine End-Class-Benachrichtigung hat 3 verschiedene Aktionstasten.

private void endNotif (Context context) { 
    Intent notificationIntent = new Intent(context, AttendanceActivity.class); 
    notificationIntent.putExtra("courseCode", courseCode); 
    notificationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); 

    Intent classAttended = new Intent(context, AttendanceNotificationServiceClass.class); 
    classAttended.setAction("action"); 
    classAttended.putExtra("courseCode", courseCode); 
    classAttended.putExtra("class", "attended"); 
    PendingIntent pClassAttended = PendingIntent.getService(context, 11, classAttended, PendingIntent.FLAG_UPDATE_CURRENT); 

    Intent classBunked = new Intent(context, AttendanceNotificationServiceClass.class); 
    classBunked.setAction("action"); 
    classBunked.putExtra("courseCode", courseCode); 
    classBunked.putExtra("class", "bunked"); 
    PendingIntent pClassBunked = PendingIntent.getService(context, 22, classBunked, PendingIntent.FLAG_UPDATE_CURRENT); 

    Intent classOff = new Intent(context, AttendanceNotificationServiceClass.class); 
    classOff.setAction("action"); 
    classOff.putExtra("courseCode", courseCode); 
    classOff.putExtra("class", "off"); 
    PendingIntent pClassOff = PendingIntent.getService(context, 33, classOff, PendingIntent.FLAG_UPDATE_CURRENT); 

    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT); 

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context) 
      .setVisibility(Notification.VISIBILITY_PUBLIC) 
      .setSmallIcon(R.drawable.ic_library_books_white_24dp) 
      .setContentTitle(courseCode + " Just Ended") 
      .setContentText("Did you attend " + courseCode + " ?") 
      .addAction(R.drawable.ic_thumb_up_white_24dp, "Attended", pClassAttended) 
      .addAction(R.drawable.ic_thumb_down_white_24dp, "Bucked", pClassBunked) 
      .addAction(R.drawable.ic_clear_white_24dp, "Class Off", pClassOff) 
      .setContentIntent(pendingIntent) 
      .setDefaults(Notification.DEFAULT_VIBRATE) 
      .setPriority(Notification.PRIORITY_HIGH) 
      .setLights(0xff00ff00, 1000, 1000) 
      .setAutoCancel(true); 

    mNotificationManager.notify(0, mBuilder.build()); 
} 

Aber wenn ich die App fire, wird nur Class-Start-Benachrichtigung nach 1 Minute angezeigt. Klassenende notif sollte 1 min nach Klassenstart anzeigen.

Start notif wird angezeigt, wie es sein sollte, d. H. Nach jeder Minute. Aber Ende notif wird nie gezeigt, nicht einmal.

Wenn in den Methoden testAlarmA() und testAlarmB(), ich Änderungen vornehmen, so dass testAlarmB() vor testAlarmA() ausgelöst wird, wird nur B/end angezeigt. Der A/Start wird nie angezeigt.

Mein Log

07-14 22:04:49.258 30137-30137/in.ac.iitd.bsw.iitdapp I/time: onReceive 
07-14 22:04:49.286 30137-30137/in.ac.iitd.bsw.iitdapp I/time: containsKey 
07-14 22:04:49.287 30137-30137/in.ac.iitd.bsw.iitdapp I/time: start 
07-14 22:06:29.165 30137-30137/in.ac.iitd.bsw.iitdapp I/time: onReceive 
07-14 22:06:29.173 30137-30137/in.ac.iitd.bsw.iitdapp I/time: containsKey 
07-14 22:06:29.173 30137-30137/in.ac.iitd.bsw.iitdapp I/time: start 
07-14 22:07:59.689 30137-30137/in.ac.iitd.bsw.iitdapp I/time: onReceive 
07-14 22:07:59.690 30137-30137/in.ac.iitd.bsw.iitdapp I/time: containsKey 
07-14 22:07:59.690 30137-30137/in.ac.iitd.bsw.iitdapp I/time: start 
07-14 22:09:52.756 30137-30143/in.ac.iitd.bsw.iitdapp W/art: Suspending all threads took: 5.590ms 
07-14 22:10:29.623 30137-30137/in.ac.iitd.bsw.iitdapp I/time: onReceive 
07-14 22:10:29.624 30137-30137/in.ac.iitd.bsw.iitdapp I/time: containsKey 
07-14 22:10:29.624 30137-30137/in.ac.iitd.bsw.iitdapp I/time: start 
07-14 22:11:59.696 30137-30137/in.ac.iitd.bsw.iitdapp I/time: onReceive 
07-14 22:11:59.696 30137-30137/in.ac.iitd.bsw.iitdapp I/time: containsKey 
07-14 22:11:59.696 30137-30137/in.ac.iitd.bsw.iitdapp I/time: start 
07-14 22:13:23.618 30137-30137/in.ac.iitd.bsw.iitdapp I/time: onReceive 
07-14 22:13:23.618 30137-30137/in.ac.iitd.bsw.iitdapp I/time: containsKey 
07-14 22:13:23.657 30137-30137/in.ac.iitd.bsw.iitdapp I/time: start 
07-14 22:15:43.136 30137-30137/in.ac.iitd.bsw.iitdapp I/time: onReceive 
07-14 22:15:43.136 30137-30137/in.ac.iitd.bsw.iitdapp I/time: containsKey 
07-14 22:15:43.136 30137-30137/in.ac.iitd.bsw.iitdapp I/time: start 

Antwort

0

In testAlarmA und testAlarmB Methode Sie Intent Konstruktor verwenden, die Aktion setzt, gibt es keine Notwendigkeit, wieder setAction aufrufen. Und in der setAction-Methode übergeben Sie Long.toString (System.currentTimeMillis()), das Sie fälschlicherweise eingefügt haben, versuchen Sie, das zu entfernen.

sehen diese Absicht Konstruktor: https://developer.android.com/reference/android/content/Intent.html#Intent(java.lang.String)

und setAction Methode: https://developer.android.com/reference/android/content/Intent.html#setAction(java.lang.String)

+0

Ich habe 'Long.toString (System.currentTimeMillis()) 'in' setAction() '' verwendet, weil ich in stackOverflow gelesen habe, dass jede Absicht unterschiedliche und eindeutige setAction - ID haben sollte und die Systemzeit immer eindeutig sein wird jede Aktion, also habe ich das benutzt. –

+0

Ich habe auch ohne 'setAction()' versucht, immer noch das gleiche Ergebnis. Vielen Dank für die Antwort –

0

Hallo Wenn PendingIntent Erstellen Objekt stellen Sie sicher, jede Absicht verschiedene Benachrichtigungs ID hat, wie unten

PendingIntent deletePendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), notificationId, deleteIntent, 0); 

wo gezeigt notificationId Wenn die App geöffnet ist, müssen Sie eine übergeben und nach dem Start müssen Sie eine weitere

haben
+0

Ja, ich bin mir der Tatsache bewusst, und wie Sie sehen können, habe ich unterschiedliche ID für jede ausstehende Absicht, d. H. 111 und 222 für erste und zweite jeweils. 'PendingIntent Broadcast = PendingIntent.getBroadcast (getContext(), 111, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);' ' PendingIntent Broadcast = PendingIntent.getBroadcast (getContext(), 222, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);' Danke für deine Antwort. –

Verwandte Themen