【问题标题】:Wake device on triggering the notification触发通知时唤醒设备
【发布时间】:2020-05-01 02:45:08
【问题描述】:

我在我的应用程序中设置了提醒,但是当通知出现时屏幕没有打开,也没有弹出。我只是收到通知栏的通知。 设备应唤醒(如果锁定)或显示通知弹出(如果解锁)

AlarmReceiver 类:

public class AlarmReceiver extends BroadcastReceiver {
    private final String CHANNEL_ID="Reminder";
    @Override
    public void onReceive(Context context, Intent intent) {
        //wake
        WakeLocker.acquire(context);

        int notificationId = intent.getIntExtra("notificationId", 0);
        Intent mainIntent = new Intent(context, Reminder.class);
        PendingIntent contentIntent = PendingIntent.getActivity(context, 0, mainIntent, 0);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            CharSequence name = "Reminder";
            String description = "Reminder for Workout";

            int importance = NotificationManager.IMPORTANCE_DEFAULT;
            NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ID, name, importance);
            notificationChannel.setDescription(description);
            NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
            notificationManager.createNotificationChannel(notificationChannel);

            //FOR ANDROID OLDER THAN VERSION OREO (8.0)
            NotificationManager mynotificationManager =
                    (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);


            NotificationCompat.Builder builder = new NotificationCompat.Builder(context, CHANNEL_ID);
            builder.setSmallIcon(android.R.drawable.ic_lock_idle_alarm)
                    .setContentTitle("It's Time")
                    .setContentText("Let's Workout")
                    .setWhen(System.currentTimeMillis())
                    .setAutoCancel(true)
                    .setContentIntent(contentIntent)
                    .setPriority(NotificationCompat.PRIORITY_DEFAULT)
                    .setDefaults(NotificationCompat.DEFAULT_ALL)
                    .setVibrate(new long[]{1000, 1000, 1000, 1000, 1000});
            mynotificationManager.notify(notificationId, builder.build());
            //wake
            WakeLocker.release();
        }

    }
}

WakeLocker 类:

public abstract class WakeLocker {
    private static PowerManager.WakeLock wakeLock;

    public static void acquire(Context context) {


        PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
        wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK |
                PowerManager.ACQUIRE_CAUSES_WAKEUP |
                PowerManager.ON_AFTER_RELEASE, "HomeFitness:WAKE_LOCK_TAG");
        wakeLock.acquire(1000);
    }

    public static void release() {
        if (wakeLock != null) wakeLock.release(); wakeLock = null;
    }
}

提醒类:

public void onClick(View v)
    {
        TimePicker t=findViewById(R.id.timepicker);
        Intent intent=new Intent(Reminder.this,AlarmReceiver.class);
        intent.putExtra("notificationId",notificationId);
        PendingIntent alarmIntent=PendingIntent.getBroadcast(Reminder.this, 0 , intent , PendingIntent.FLAG_CANCEL_CURRENT);
        AlarmManager alarm=(AlarmManager) getSystemService(ALARM_SERVICE);


        switch(v.getId())
        {
            case R.id.set:
                int hr= t.getHour();
                int min=t.getMinute();

                Calendar startTime = Calendar.getInstance();
                startTime.set(Calendar.HOUR_OF_DAY,hr);
                startTime.set(Calendar.MINUTE,min);
                startTime.set(Calendar.SECOND,0);
                startTime.set(Calendar.MILLISECOND,0);


                long alarmStartTime=startTime.getTimeInMillis();
                alarm.set(AlarmManager.RTC_WAKEUP,alarmStartTime,alarmIntent);

                alarm.setRepeating(AlarmManager.RTC_WAKEUP, startTime.getTimeInMillis(),
                        AlarmManager.INTERVAL_DAY, alarmIntent);
                //added later for higher android
                alarm.setExact(AlarmManager.RTC_WAKEUP,startTime.getTimeInMillis(),alarmIntent);
                alarm.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP,startTime.getTimeInMillis(),alarmIntent);
                //new close
                Toast.makeText(Reminder.this,"Reminder Set", Toast.LENGTH_SHORT).show();
                break;

            case R.id.cancelb:
                alarm.cancel(alarmIntent);
                Toast.makeText(Reminder.this, "Reminder Canceled", Toast.LENGTH_SHORT).show();
                break;
        }
    }

【问题讨论】:

    标签: android broadcastreceiver alarmmanager powermanager


    【解决方案1】:

    ACQUIRE_CAUSES_WAKEUP 不能与PARTIAL_WAKE_LOCK 一起使用。尝试删除后者。 尝试.setPriority(NotificationCompat.PRIORITY_MAX) 获取通知(并为频道更改它)。

    /**
     * Wake lock flag: Turn the screen on when the wake lock is acquired.
     * <p>
     * Normally wake locks don't actually wake the device, they just cause
     * the screen to remain on once it's already on.  Think of the video player
     * application as the normal behavior.  Notifications that pop up and want
     * the device to be on are the exception; use this flag to be like them.
     * </p><p>
     * Cannot be used with {@link #PARTIAL_WAKE_LOCK}.
     * </p>
     */
    public static final int ACQUIRE_CAUSES_WAKEUP = 0x10000000;
    

    【讨论】:

    • 通知渠道有什么变化?
    • 使用 NotificationManager.IMPORTANCE_HIGH 作为通知通道。
    • 我刚刚在我的应用程序中发现了一个新错误。我试图设置明天早上 8 点的提醒,但由于今天早上 8 点已经过去,它会立即触发通知。对此也有任何帮助吗?
    • 有一个方法calendar.add(Calendar.DATE, 1);这将为任何日历对象添加 24 小时。我还注意到您基本上将相同的警报设置了三次,这也可能导致问题。希望对你有所帮助。
    • 好的,谢谢,我会检查的。尽管@einUsername 弹出通知仍然不起作用
    【解决方案2】:

    唤醒屏幕的唯一方法是: PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP

    但是,这将(可能)在较新的 Android 版本中停止工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-12-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多