手机长时间睡眠时,Android通知不会被触发

时间:2016-12-31 17:54:16

标签: android notifications alarmmanager wakelock

我有一种奇怪的问题。我知道我错过了一些东西,但我不知道是什么。

我每天增加大约5个通知,每两到三个小时创建一次。

如果我们有以下情况:

  1. 通知1在8:00创建,应在12:00开始。
  2. 收到通知1后,将对其进行处理并创建通知2,并且需要在14:00触发。
  3. 收到通知2后,将对其进行处理并创建通知3,并应在17:00触发
  4. 收到通知3后,将对其进行处理并创建通知4,并应在第二天的8:00解雇。
  5. 在手动解锁/取消睡眠之前,我们才会收到通知4。即使我在9:00解锁了手机,通知的时间也是8:00。
  6. 所以我的问题是当手机长时间进入睡眠状态时,直到我手动按下Samsung S7上的唤醒按钮才会处理通知。此外,每次通过服务收到通知时,我都在播放媒体。对于通知4,在我手动唤醒手机之前,媒体不会播放。此外,我有一些应用于内容的样式,它正确显示通知1,2和3,但没有在通知4。

    我想也许当我第二天创建通知时,注册它们时出现问题,所以我尝试手动播放我的日期,一切正常。

    这是我创建通知的方式:

    public Notification getNotification(String notificationName, long notificationTime) {
    
        Intent intent = new Intent( context, NotificationPlayerService.class );
        intent.setAction( NotificationPlayerService.ACTION_STOP );
        PendingIntent pendingIntent = PendingIntent.getService(context, 1, intent, 0);
    
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
        builder.setContentTitle("Notification");
        builder.setContentText(notificationName);
        builder.setSmallIcon(R.drawable.icon);
        builder.setColor(ContextCompat.getColor(context, R.color.teal_500));
        builder.setLights(Color.GREEN, 3000, 3000);        
        builder.setVibrate(new long[]{0,750});
        builder.setWhen(notificationTime);
        builder.setShowWhen(true);     
        Uri sound= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        builder.setSound(sound);
        builder.setDeleteIntent(pendingIntent);
        return builder.build();
    }
    

    这就是我安排他们的方式:

    @SuppressLint("NewApi")
    public void scheduleNotification(Notification notification, long notificationTime, String action, int id) {
    
        Intent notificationIntent = new Intent(context, NotificationPublisher.class);
        notificationIntent.setAction(action);       
        if(id!=-1){         
            notificationIntent.putExtra(NotificationPublisher.NOTIFICATION, notification);
            notificationIntent.putExtra(NotificationPublisher.NOTIFICATION_ID, id);
        }
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);        
        AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);        
    
        if (Build.VERSION.SDK_INT > 19) {
            alarmManager.setExact(AlarmManager.RTC_WAKEUP, notificationTime, pendingIntent);        
        }
    
    }
    

    这就是我收到它们的方式:

    public void onReceive(Context context, Intent intent) {
    
        this.context = context;
    
        NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
    
        String action = intent.getAction();
        int id = intent.getIntExtra(NOTIFICATION_ID, -1);
        Log.e("NOTIFICATION_RECEIVER","Notification received " + action +"-"+ id);
    
        if(id != -1){
    
            Notification notification = intent.getParcelableExtra(NOTIFICATION);                        
            notificationManager.notify(id, notification);                       
    
        }
    
        Intent serviceIntent = NotificationService.startNotificationService(context);
        if (serviceIntent != null) {
            startWakefulService(context, serviceIntent);
        }
    
    }
    

    最后我使用IntenService创建通知:

    @Override
    protected void onHandleIntent(Intent intent) {
    
        try{
            startNotifications();
        }
        finally {
            WakefulBroadcastReceiver.completeWakefulIntent(intent);
        }
    }
    

    startNotification使用上面显示的方法getNotification和scheduelNotification。基本上,它从SharedPreferences获取一些设置,并计算何时应该触发通知。

    谢谢

0 个答案:

没有答案