[我正在尝试实现NotificationChannel和WorkManager,但是不知何故,并且看不到任何错误

问题描述 投票:0回答:1

我正在尝试实现一项功能,在其中您可以选择日期和时间,并且通知会在您的手机上弹出。因此,在编写了一些代码之后,它仍然无法正常工作,但是一切似乎都很好

活动代码

FloatingActionButton fab = findViewById(R.id.fab);
            fab.setOnClickListener(new View.OnClickListener() {
                @RequiresApi(api = Build.VERSION_CODES.M)
                @Override
                public void onClick(View view) {

                    Calendar customCalendar = GregorianCalendar.getInstance();
                    DatePicker dp = findViewById(R.id.date_picker);
                    TimePicker picker = findViewById(R.id.time_picker);
                    customCalendar.set(
                            dp.getYear(), dp.getMonth(), dp.getDayOfMonth(), picker.getHour(), picker.getMinute(), 0);

                    long customTime = customCalendar.getTimeInMillis();
                    SimpleDateFormat sdf = new SimpleDateFormat(getString(R.string.notification_schedule_pattern), Locale.getDefault());
                    long currentTime = System.currentTimeMillis();

                    Log.d("time", "cistomTime " + customTime);
                    Log.d("time", "cistomTime " + currentTime);
                    if (customTime > currentTime) {
                        Data data = new Data.Builder().putInt(NOTIFICATION_ID, 0).build();
                        int delay = (int) (customTime - currentTime);

                        scheduleNotification(delay, data);
                        String titleNotificationSchedule = getString(R.string.notification_schedule_title);
                        Snackbar.make(
                                view,
                                titleNotificationSchedule + sdf
                                        .format(customCalendar.getTime()),
                                LENGTH_LONG).show();
    //        Snackbar.make(coordinatorLayout, "Reminder set", LENGTH_LONG)
    //                .setAction("Action", null).show();
                    } else {
                        String errorNotificationSchedule = "Error occured";
                        Snackbar.make(coordinatorLayout, errorNotificationSchedule, LENGTH_LONG).show();
                    }
                }


            });
        }

        private void scheduleNotification(long delay, Data data) {
            OneTimeWorkRequest notificationWork = new OneTimeWorkRequest.Builder(NotifyWork.class)
                    .setInitialDelay(delay, MILLISECONDS).setInputData(data).build();

            WorkManager instanceWorkManager = WorkManager.getInstance(getApplicationContext());
            instanceWorkManager.beginUniqueWork(NOTIFICATION_WORK, REPLACE, notificationWork).enqueue();
        }


    Worker class
    public class NotifyWork extends Worker {
        public static final String NOTIFICATION_ID = "notification_id";
        public static final String NOTIFICATION_NAME = "Remember";
        public static final String NOTIFICATION_CHANNEL = "Reminder_Channel";
        public static final String NOTIFICATION_WORK = "Notification_Work";

        public NotifyWork(@NonNull Context context, @NonNull WorkerParameters workerParams) {
            super(context, workerParams);
        }

        @NonNull
        @Override
        public Result doWork() {
            int id = getInputData().getInt(NOTIFICATION_ID, 0);
            sendNotification(id);
            return Result.success();
        }

        private void sendNotification(int id) {
            NotificationManager notificationManager = (NotificationManager) getApplicationContext()
                    .getSystemService(Context.NOTIFICATION_SERVICE);
            Bitmap bitmap = BitmapFactory.decodeResource(getApplicationContext().getResources(), R.drawable.ic_done_white_24dp);
            Intent intent = new Intent(getApplicationContext(), MainActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
            intent.putExtra(NOTIFICATION_ID, id);
            String titleNotification = "Reminder";
            String subtitleNotification = "Time To WakeUp";
            PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
            NotificationCompat.Builder notification = new NotificationCompat.Builder(getApplicationContext(), NOTIFICATION_CHANNEL)
                    .setLargeIcon(bitmap).setContentTitle(titleNotification)
                    .setContentText(subtitleNotification).setDefaults(IMPORTANCE_DEFAULT).setSound(getDefaultUri(TYPE_NOTIFICATION))
                    .setContentIntent(pendingIntent).setAutoCancel(true);

            notification.setPriority(IMPORTANCE_MAX);
            notificationManager.notify(id, notification.build());

            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {


                Uri ringtoneManager = getDefaultUri(TYPE_NOTIFICATION);
                AudioAttributes audioAttributes = new AudioAttributes.Builder().setUsage(USAGE_NOTIFICATION_RINGTONE)
                        .setContentType(CONTENT_TYPE_SONIFICATION).build();


                NotificationChannel channel = new NotificationChannel(NOTIFICATION_CHANNEL, NOTIFICATION_NAME, NotificationManager.IMPORTANCE_DEFAULT);
                channel.enableLights(true);
                channel.setLightColor(RED);
                channel.enableVibration(true);
                channel.setSound(ringtoneManager, audioAttributes);
                notificationManager.createNotificationChannel(channel);
            }


        }

我有一个DatePickerTimePicker,当您选择日期和时间并单击FAB按钮时,您会在该特定时间收到通知

android-jetpack android-workmanager notificationmanager
1个回答
0
投票

以某种方式将.setLargeIcon更改为.setSmallIcon并直接引用图像,而无需转换为位图,例如.setSmallIcon(R.drawable.ic_done_white_24dp)解决了该问题

© www.soinside.com 2019 - 2024. All rights reserved.