5

Po uruchomieniu aplikacji wykonuje wywołanie API, a następnie wyświetla powiadomienia na podstawie wyników. To około 10 zaplanowanych powiadomień. Wygląda na to, że problem z sygnaturą czasową wyświetlaną na faktycznym powiadomieniu jest nieprawidłowy.Nieprawidłowy znacznik czasu dla przyszłych powiadomień

Od czasu tworzenia powiadomień, a następnie planowania alarmu za pomocą numeru AlarmManager, domyślnym czasem powiadomienia będzie czas utworzenia powiadomienia (System.currentTimeMillis()).

Próbowałem użyć metody .setWhen() na moim Notification.Builder, aby ustawić ją na czas, którego używam do zaplanowania wcześniej wspomnianego alarmu. Jest to trochę lepiej, ponieważ powiadomienia nie są dostarczane w określonym czasie, często otrzymuję powiadomienia kilka minut temu.

Dodatkowo, próbowałem ręcznie zmienić pole when na powiadomienia w moim BroadcastReceiver, tuż przed .notify() jest w rzeczywistości nazywa się:

public class NotificationPublisher extends BroadcastReceiver { 

    public static String NOTIFICATION_ID = "notification_id"; 
    public static String NOTIFICATION = "notification"; 

    public void onReceive(Context context, Intent intent) { 

     NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE); 

     Notification notification = intent.getParcelableExtra(NOTIFICATION); 
     notification.when = System.currentTimeMillis(); 
     int id = intent.getIntExtra(NOTIFICATION_ID, 0); 
     notificationManager.notify(id, notification); 

    } 
} 

Jednakże w powyższej sytuacji wydaje się, że .when jest ignorowany.

Szczerze mówiąc, po prostu szukam sposobu, aby znacznik czasu wyświetlany w powiadomieniu był czasem, w którym jest rzeczywiście wyświetlany.

Odpowiedz

2

Sugerowałbym przekazanie informacji w powiadomieniu jako dodatki, a następnie utworzenie powiadomienia w BroadcastReceiver. Zbuduje to powiadomienie tuż przed jego wydaniem, więc będzie miał ten sam czas, w którym twój AlarmManager uruchomi BroadcastReceiver.

Od gdziekolwiek jesteś planowania powiadomienie:

private void scheduleNotification(){ 

    // Create an intent to the broadcast receiver you will send the notification from 
    Intent notificationIntent = new Intent(this, SendNotification.class); 

    // Pass your extra information in 
    notificationIntent.putExtra("notification_extra", "any extra information to pass in"); 

    int requestCode = 1; 

    // Create a pending intent to handle the broadcast intent 
    PendingIntent alarmIntent = PendingIntent 
       .getBroadcast(this, requestCode, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT); 

    // Set your notification's trigger time 
    Calendar alarmStart = Calendar.getInstance(); 
    alarmStart.setTimeInMillis(System.currentTimeMillis()); 
    alarmStart.set(Calendar.HOUR_OF_DAY, 6); // This example is set to approximately 6am 

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

    // Set the alarm with the pending intent 
    // be sure to use set, setExact, setRepeating, & setInexactRepeating 
    // as well as RTC_WAKEUP, ELAPSED_REALTIME_WAKEUP, etc. 
    // where appropriate 
    alarmManager.set(AlarmManager.RTC_WAKEUP, alarmStart.getTimeInMillis(), alarmIntent); 
} 

Następnie wewnątrz BroadcastReceiver za onReceive:

String notificationExtra = null; 

// Retrieve your extra data 
if(intent.hasExtra("notification_extra")){ 
    notificationExtra = intent.getStringExtra("notification_extra"); 
} 

//Build the notification 
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context); 
mBuilder.setSmallIcon(notificationIcon) 
     .setContentTitle(notificationTitle) 
     .setContentText(notificationMessage) 
     .setAutoCancel(true); // Use AutoCancel true to dismiss the notification when selected 

// Check if notificationExtra has a value 
if(notificationExtra != null){ 
    // Use the value to build onto the notification 
} 

//Define the notification's action 
Intent resultIntent = new Intent(context, MainActivity.class); // This example opens MainActivity when clicked 

int requestCode = 0; 

PendingIntent resultPendingIntent = 
     PendingIntent.getActivity(
       context, 
       requestCode, 
       resultIntent, 
       PendingIntent.FLAG_UPDATE_CURRENT 
     ); 

//Set notification's click behavior 
mBuilder.setContentIntent(resultPendingIntent); 

// Sets an ID for the notification 
int mNotificationId = 1; 

// Gets an instance of the NotificationManager service 
NotificationManager mNotifyMgr = 
     (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 

// Builds the notification and issues it. 
mNotifyMgr.notify(mNotificationId, mBuilder.build()); 
+0

dzięki @sammy pozwól mi spróbować tego! – xbadal

+0

Thanksyou sammy! pracował idealnie – xbadal

+0

czy możesz mi powiedzieć, jak utworzyć wiele powiadomień.? można to zrobić, dostarczając mNotificationId? to muszę anulować to samo! – xbadal

0

Twojego NotificationPublisher za onReceive() metoda będzie invoked tylko wtedy, gdy planowane alarm wywołuje określone time. Kiedy napiszesz powiadomienie z metody onReceive(), z pewnością wskaże aktualny czas. Nie trzeba wymagać użycia metody .when lub .setWhen().

Spróbuj tego:

public class NotificationPublisher extends BroadcastReceiver { 

    public static String NOTIFICATION_ID = "notification_id"; 
    public static String NOTIFICATION = "notification"; 

    public void onReceive(Context context, Intent intent) { 

     int id = intent.getIntExtra(NOTIFICATION_ID, 0); 

     // Notification 
     Notification notification = new Notification.Builder(context)  
      .setContentTitle("This is notification title") 
      .setContentText("This is notification text") 
      .setSmallIcon(R.mipmap.ic_launcher).build(); 

     notification.flags |= Notification.FLAG_AUTO_CANCEL; 

     // Notification Manager 
     NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 

     notificationManager .notify(id, notification); 
    } 
} 

Jeśli chcesz przekierować do activity gdy kliknięcie Notification, a następnie można użyć PendingIntent i ustawić go do Notification.

public class NotificationPublisher extends BroadcastReceiver { 

    public static String NOTIFICATION_ID = "notification_id"; 
    public static String NOTIFICATION = "notification"; 

    public void onReceive(Context context, Intent intent) { 

     int id = intent.getIntExtra(NOTIFICATION_ID, 0); 

     Intent intent = new Intent(context, YourTargetActivity.class); 
     intent.putExtra("KEY_ID", id); // Pass extra values if needed 

     PendingIntent pI = PendingIntent.getActivity(context, id, intent, PendingIntent.FLAG_UPDATE_CURRENT); 

     // Notification 
     Notification notification = new Notification.Builder(context)  
      .setContentTitle("This is notification title") 
      .setContentText("This is notification text") 
      .setSmallIcon(R.mipmap.ic_launcher) 
      .setContentIntent(pI).build(); 

     notification.flags |= Notification.FLAG_AUTO_CANCEL; 

     // Notification Manager 
     NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 

     notificationManager .notify(id, notification); 
    } 
} 

nadzieję, że pomoże ~

Powiązane problemy