代码之家  ›  专栏  ›  技术社区  ›  Dean Hiller

android通知不会延迟60秒,但会在3秒内发出

  •  0
  • Dean Hiller  · 技术社区  · 5 年前

    我正在努力跟上 this post this post

    private void displayNotifiation(Contact contact, byte[] img) {
        int notificationId = contact.hashCode();
        Log.d(TAG, "onClick 2: put info="+contact.getName()+" notifId:"+notificationId);
    
        Bitmap bitmap = BitmapFactory.decodeByteArray(img, 0, img.length);
    
        NotificationCompat.Builder builder = new NotificationCompat.Builder(mContext, MainActivity.CHANNEL_ID)
                .setSmallIcon(R.mipmap.logo2)
                .setContentTitle("It's time to reach out to "+contact.getName())
                .setContentText("For your health")
                .setPriority(NotificationCompat.PRIORITY_DEFAULT)
                .setLargeIcon(bitmap)
                .setAutoCancel(false);
    
        Intent intent = new Intent(mContext, ActivityNotificationLanding.class);
        Uri uri = Uri.parse("http://notexist.mykeepintouch.app/contactid/"+contact.getId());
        intent.setData(uri);
        //intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        gson.putExtra(intent, IntentFlashStore.CONTACT_KEY, contact);
        NotificationInfo info = new NotificationInfo();
        info.setId(notificationId);
        gson.putExtra(intent, IntentFlashStore.NOTIFICATION_INFO, info);
    
        PendingIntent activity = PendingIntent.getActivity(mContext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        builder.setContentIntent(activity);
    
        Notification notification = builder.build();
    
        Intent notificationIntent = new Intent(mContext, MyNotificationPublisher.class);
        notificationIntent.putExtra(MyNotificationPublisher.NOTIFICATION_ID, notificationId);
        notificationIntent.putExtra(MyNotificationPublisher.NOTIFICATION, notification);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, notificationId, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    
        long futureInMillis = SystemClock.elapsedRealtime() + 60000;
        AlarmManager alarmManager = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
        alarmManager.cancel(pendingIntent);
        alarmManager.set(AlarmManager.RTC_WAKEUP, futureInMillis, pendingIntent);
    }
    

    嗯,,

    但是,在重新启动手机时,这是否有效?我读了一些关于引导接收器的文章,不确定是否需要这样做。安卓24+

    0 回复  |  直到 5 年前