如何使用AlarmManager和NotificationBuilder设置通知?

时间:2019-05-17 11:19:59

标签: java android

我正在尝试使用警报和通知管理器设置通知。我遇到的问题是通知根本不会发出。 我在AlarmReceiver的onReceive函数中设置了一个简单的Toast消息,它可以工作,但是通知本身根本不显示。

这是启动接收器的添加按钮:

            @Override
            public void onClick(View v) {

                if (dateStr == null) {
                    dateStr = simpleDateFormat.format(Calendar.getInstance().getTime());
                }

                /*Toast toast = Toast.makeText(getApplicationContext(),
                        taskNameInput.getText().toString() + taskDescInput.getText().toString() + "\n" +
                         dateStr + "\n" +
                        hours + ":" + mins,
                        Toast.LENGTH_LONG);
                toast.show();*/

                ReminderDateTime reminderDateTime1 = new ReminderDateTime(taskNameInput.getText().toString(),
                        taskDescInput.getText().toString(),
                        hours + ":" + mins,
                        dateStr);

                MyDatabase.getDatabase(getApplicationContext()).myDao().addToDoTask(reminderDateTime1);


                Date dateToConvert = null;
                try {
                    dateToConvert = new SimpleDateFormat("yyyy-MM-dd").parse(dateStr);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                long timeInMillis = dateToConvert.getTime() + (hours*3600 + mins*60)*1000;

                AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
                Intent alarmIntent = new Intent(getApplicationContext(), AlarmReceiver.class);

                PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, alarmIntent, 0);

                alarmManager.set(AlarmManager.RTC_WAKEUP, timeInMillis, pendingIntent);


                /*Toast.makeText(getApplicationContext(),
                        new Date(timeInMillis).toString(),
                        Toast.LENGTH_LONG).show();*/

                finish();
                Intent intent1 = new Intent(getApplicationContext(), MainActivity.class);
                startActivity(intent1);
            }
        });

这是AlarmReceiver类:

    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context, "NOTIF", Toast.LENGTH_LONG).show();

        final Intent emptyIntent = new Intent();
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, emptyIntent, PendingIntent.FLAG_UPDATE_CURRENT);


        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(context)
                        .setSmallIcon(R.drawable.ic_access_time_black_24dp)
                        .setContentTitle("My notification")
                        .setContentText("Hello World!")
                        .setContentIntent(pendingIntent); //Required on Gingerbread and below

        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(101, mBuilder.build());
    }
}

由于我尝试了多个版本,因此此通知构建器可能不是100%正确。

接收器中的Toast消息工作正常,所以我认为我的问题是通知生成器本身,因为该应用程序会调用接收器。

0 个答案:

没有答案