2016-02-25 2 views
-1

Я пытаюсь довести до уведомления после определенного периода времени с помощью:Уведомления не показывая Android

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

и следующие функции -

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); 
} 

После регистрации значения задержки, я получаю 57580 который составляет примерно 57 секунд, но даже после этого периода времени я не получаю уведомления о статусе.

Пожалуйста, помогите.

+0

вы должны сделать трюк с BroadcastReceiver. Расписание будильника и запуск уведомления в onReceive() ..... – Opiatefuchs

ответ

1

Для получения уведомлений от системы вам понадобится BroadcastReceiver.

public static class AlarmReceiver extends BroadcastReceiver { 

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

И помните, чтобы объявить класс в вашем AndroidManifest:

<receiver android:name=".AlarmReceiver" /> 
Смежные вопросы