Как показывать уведомления ДАЖЕ, когда телефон находится в режиме ожидания или приложение находится в режиме ожидания приложения? - PullRequest
0 голосов
/ 28 марта 2019

Я создаю приложение, которое должно напоминать пользователю о предстоящих событиях, которые они устанавливают (в основном напоминание).Проблема, с которой я сталкиваюсь, заключается в отправке уведомлений на телефон пользователя (только для API 26+), когда приложение не использовалось некоторое время (около 15 минут или более);уведомления не отображаются вообще.

Я прочитал об этом и понял, что режим ожидания приложения и режим ожидания могут препятствовать тому, чтобы мое приложение выдвигало такие уведомления;мои уведомления принимаются по назначению пользователя на телефонах с API 25 и ниже.Чтобы решить эту проблему, я попытался использовать AlarmManager.setExactAndAllowWhileIdle (), но проблема сохраняется.

class TaskNotifications {
    private AlarmManager alarmManager;
    private Context c;

    TaskNotifications(Context context) {
        this.c = context;
        this.alarmManager = (AlarmManager) c.getSystemService(Context.ALARM_SERVICE);
    }

    void setReminder(Context context, Task task) {
        if (VERSION.SDK_INT < Build.VERSION_CODES.O) {
            long reminderMilliseconds = task.getReminderMilliseconds();
            if (reminderMilliseconds > Calendar.getInstance().getTimeInMillis() && !task.isDone()) {
                Intent intent = new Intent(context, NotificationReceiver.class);
                intent.putExtra("ID", task.getID());
                intent.putExtra("TITLE", task.getTitle());
                intent.putExtra("DETAILS", task.getDetails());

                PendingIntent pendingIntent = PendingIntent.getBroadcast(context, task.getID(), intent, PendingIntent.FLAG_UPDATE_CURRENT);
                if (VERSION.SDK_INT >= 23) {
                    this.alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, reminderMilliseconds, pendingIntent);
                } else
                    this.alarmManager.setExact(AlarmManager.RTC_WAKEUP, reminderMilliseconds, pendingIntent);
            }
        }
    }

    void cancelReminder(Task task) {
        if (VERSION.SDK_INT < Build.VERSION_CODES.O) {
            this.alarmManager.cancel(PendingIntent.getBroadcast(this.c, task.getID(),
                    new Intent(this.c, NotificationReceiver.class), PendingIntent.FLAG_CANCEL_CURRENT));
        }
    }
}
public class NotificationReceiver extends BroadcastReceiver {
    public void onReceive(Context context, Intent intent) {
        Intent startIntent = new Intent(context, NotificationJobIntentService.class);

        startIntent.putExtra("ID", intent.getIntExtra("ID", -1));
        startIntent.putExtra("TITLE", intent.getStringExtra("TITLE"));
        startIntent.putExtra("DETAILS", intent.getStringExtra("DETAILS"));

        JobIntentService.enqueueWork(context, NotificationJobIntentService.class, intent.getIntExtra("ID", -1), startIntent);
    }
}
public class NotificationJobIntentService extends JobIntentService {
    private String CHANNEL_ID = getResources().getString(R.string.channel_name);

    protected void onHandleWork(@NonNull Intent intent) {
        createNotificationChannel(NotificationJobIntentService.this);

        int NOTIFICATION_ID = intent.getIntExtra("ID", -1);
        String GROUP = "NOTIFICATION_GROUP";

        String title = intent.getStringExtra("TITLE");
        if (title.isEmpty())
            title = getResources().getString(R.string.no_title);

        String details = intent.getStringExtra("DETAILS");
        if (details.isEmpty())
            details = getResources().getString(R.string.no_details);

        Intent openAppIntent = new Intent(NotificationJobIntentService.this, MainActivity.class);
        TaskStackBuilder create = TaskStackBuilder.create(this);
        create.addNextIntentWithParentStack(openAppIntent);

        NotificationCompat.Builder builder = new NotificationCompat.Builder(NotificationJobIntentService.this, this.CHANNEL_ID)
                .setContentTitle(title)
                .setContentText(details)
                .setSmallIcon(R.drawable.baseline_alarm_black_18)
                .setPriority(NotificationCompat.PRIORITY_MAX)
                .setContentIntent(create.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT))
                .setCategory(NotificationCompat.CATEGORY_ALARM)
                .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
                .setGroup(GROUP)
                .setAutoCancel(true)
                .setColor(Color.argb(100, 0, 87, 75))
                .setVibrate(new long[]{1000, 1000})
                .setLights(Color.GREEN, PathInterpolatorCompat.MAX_NUM_POINTS, PathInterpolatorCompat.MAX_NUM_POINTS)
                .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM));

        NotificationManagerCompat.from(this).notify(NOTIFICATION_ID, builder.build());
    }

    private void createNotificationChannel(Context context) {
        if (VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            CharSequence string = context.getString(R.string.channel_name);
            String description = context.getString(R.string.channel_description);
            NotificationChannel notificationChannel = new NotificationChannel(this.CHANNEL_ID, string, NotificationManager.IMPORTANCE_HIGH);
            notificationChannel.setDescription(description);
            notificationChannel.enableLights(true);
            notificationChannel.setLightColor(Color.GREEN);
            notificationChannel.enableVibration(true);
            notificationChannel.setVibrationPattern(new long[]{1000, 1000});
            (context.getSystemService(NotificationManager.class)).createNotificationChannel(notificationChannel);
        }
    }
}

Существует ли надежный способ отправки точных / несколько точных уведомлений на телефоны моих пользователейработает API 26+?Или в моем коде есть ошибка, которую я не заметил?

1 Ответ

0 голосов
/ 11 мая 2019

Мне не удалось заставить работать систему уведомлений на устройствах API 26+, однако я использовал напоминания поставщика календаря Android, чтобы добавить событие в календарь пользователей, а затем установить напоминание через календарь по умолчанию ... не то, что яизначально хотел, но это пластырь.

Если кто-то еще может решить проблему, как задумано, пожалуйста, дайте мне знать.

Код ниже:

if (task.getEventID() > 0) {
                //Remove existing events for this task
                ContentResolver cr = c.getContentResolver();
                int iNumRowsDeleted;
                Uri eventUri = ContentUris.withAppendedId(CalendarContract.Events.CONTENT_URI, task.getEventID());
                iNumRowsDeleted = cr.delete(eventUri, null, null);
                Log.i("removeEvent()", "Deleted " + iNumRowsDeleted + " calendar entry.");
            }
            try {
                //Add an event
                ContentResolver cr = context.getContentResolver();
                ContentValues values = new ContentValues();
                values.put(CalendarContract.Events.DTSTART, task.getCal().getTimeInMillis());
                values.put(CalendarContract.Events.DTEND, task.getCal().getTimeInMillis()+60*60*1000);//Each task a duration of 60 minutes
                values.put(CalendarContract.Events.TITLE, task.getTitle() + " - " + task.getDetails());
                values.put(CalendarContract.Events.CALENDAR_ID, getPrimaryCalendar());
                values.put(CalendarContract.Events.EVENT_TIMEZONE, Calendar.getInstance().getTimeZone().getID());

                Uri uri = cr.insert(CalendarContract.Events.CONTENT_URI, values);

                // Save the eventId into the Task object for possible future delete.
                task.setEventID(Integer.parseInt(uri.getLastPathSegment()));
                Log.i("addEvent()","The event id is " + task.getEventID());

                // Add a reminder
                ContentValues valuesR = new ContentValues();
                valuesR.put(CalendarContract.Reminders.MINUTES, (task.getCal().getTimeInMillis() - reminderMilliseconds)/(1000*60));
                valuesR.put(CalendarContract.Reminders.EVENT_ID, task.getEventID());
                valuesR.put(CalendarContract.Reminders.METHOD, CalendarContract.Reminders.METHOD_ALERT); /*The alarm method, as set on the server. METHOD_DEFAULT, METHOD_ALERT, METHOD_EMAIL, METHOD_SMS and METHOD_ALARM are possible values; the device will only process METHOD_DEFAULT and METHOD_ALERT reminders (the other types are simply stored so we can send the same reminder info back to the server when we make changes).*/
                Uri uriR = cr.insert(CalendarContract.Reminders.CONTENT_URI, valuesR);
                Cursor c = CalendarContract.Reminders.query(cr, task.getEventID(), new String[]{CalendarContract.Reminders.MINUTES});
                if (c.moveToFirst()) {
                    Log.i("setReminder()",task.toString());
                    Log.i("setReminder()","calendar has reminder at " + c.getInt(c.getColumnIndex(CalendarContract.Reminders.MINUTES)));
                }
                c.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
...