1

Реализовал вызов пуш-уведомления следующим образом:

scheduleNotification(getNotification(), 5000);//вызов

private void scheduleNotification(Notification notification, int 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);
    }
    private Notification getNotification(){
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher);
        String strtitle = getString(R.string.notificationtitle);
        String strtext = getString(R.string.notificationtext);
        Intent intent = new Intent(this, NotificationPublisher.class);
        intent.putExtra("title", strtitle);
        intent.putExtra("text", strtext);
        PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent,0);
        Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.time)
                .setLargeIcon(bitmap)
                .setContentTitle(getString(R.string.notificationtitle))
                .setContentText(getString(R.string.notificationtext))
                .setContentIntent(pIntent)
                .setAutoCancel(true)
                .setSound(soundUri);
        NotificationManager notificationmanager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
        notificationmanager.notify(0, builder.build());
        return builder.build();
    }

Как реализовать, чтобы пуш приходил не через 5 секунд, как сделано мною, а в определенное время устройства? Буду рад любой помощи!

1 ответ 1

3

long futureInMillis = SystemClock.elapsedRealtime() + delay;

замените на

long futureInMillis = delay;

и передавайте время, в которое должно сработать в scheduleNotification(getNotification(), time);, где time - нужное время в миллисекундах

P.S. Или сразу его подставляйте в alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, delay, pendingIntent);

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.