2016-02-25 4 views
-1

Je suis en train de mettre en place une notification période après temps définie à l'aide:Notifications ne montrant Android

scheduleNotification(getNotification("Notification content..") differ); 

et les fonctions suivantes -

private Notification getNotification(String content) { 
     Notification.Builder builder = new Notification.Builder(this); 
     builder.setContentTitle("Scheduled Notification"); 
     builder.setContentText(content); 
     builder.setSmallIcon(R.drawable.icon); 

     return builder.build(); 
} 


private void scheduleNotification(Notification notification, long delay) { 

     Intent notificationIntent = new Intent(this, NotificationPublisher.class); 
      notificationIntent.putExtra(NotificationPublisher.NOTIFICATION_ID, 1); 
     notificationIntent.putExtra(NotificationPublisher.NOTIFICATION, notification); 
     PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);   
     long futureInMillis = SystemClock.elapsedRealtime() + delay; 
     AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE); 
     alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, futureInMillis, pendingIntent); 
} 

Une fois connecté la valeur de retard, je reçois 57580 ce qui est environ 57 secondes, mais même après cette période, je ne reçois aucune notification sur la barre d'état.

Aidez-nous s'il vous plaît.

+0

vous devriez faire l'affaire avec un BroadcastReceiver. Déduire l'alarme et démarrer la notification dans onReceive() ..... – Opiatefuchs

Répondre

1

Vous avez besoin d'un BroadcastReceiver pour recevoir des notifications du système.

public static class AlarmReceiver extends BroadcastReceiver { 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     getNotification(String content); 
    }   
} 

Et rappelez-vous de déclarer la classe dans votre AndroidManifest:

<receiver android:name=".AlarmReceiver" />