【问题标题】:How to show notifications EVEN when phone is in Doze or app is in App Standby mode?即使手机处于打盹或应用处于应用待机模式,如何显示通知?
【发布时间】:2019-03-28 20:06:13
【问题描述】:

我正在构建一个应用程序,它应该提醒用户他们设置的即将发生的事件(基本上是一个提醒)。我遇到的问题是当应用程序有一段时间没有使用(大约 15 分钟或更长时间)时,将通知推送到用户的手机(仅在 API 26+ 上);通知根本不显示。

我阅读了这篇文章并意识到应用待机和打盹模式可能会阻止我的应用推送此类通知;用户在运行 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+ 的用户的手机发送准确/稍微准确的通知?或者我的代码中是否有我没有注意到的错误?

【问题讨论】:

    标签: java android notifications android-8.0-oreo


    【解决方案1】:

    我无法让通知系统在 API 26+ 设备上运行,但是,我使用 Android Calendar Provider Reminders 将事件添加到用户日历,然后通过默认日历设置提醒...不是我本来想要的,但它是一个创可贴的解决方案。

    如果有人仍然可以按预期解决问题,请告诉我。

    代码如下:

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

    【讨论】:

      猜你喜欢
      • 2019-12-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多