Home > database >  Create and android Alarm and get Notification
Create and android Alarm and get Notification

Time:12-01

I can set dates, times, and type whatever ask I won't successfully and reminders. I do not have receive any error, but I don't have receive any notification, when the task is set. These are my code snippet below:

this is my set alarm class:

private void setAlarm( String text, String date, String time){ AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);

//create an intent to show notification

Intent intent = new Intent(CreateTask.this, TaskNotificationAlarm.class); intent.putExtra("event", text); intent.putExtra("time", date); intent.putExtra("date", time);

PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, intent, PendingIntent.FLAG_IMMUTABLE); String dateandtime = date " " timeTonotify; DateFormat formatter = new SimpleDateFormat("d-M-yyyy hh:mm"); try { Date date1 = formatter.parse(dateandtime); alarmManager.set(AlarmManager.RTC_WAKEUP, date1.getTime(), pendingIntent); Toast.makeText(getApplicationContext(), "Alarm", Toast.LENGTH_SHORT).show(); catch (ParseException e) { e.printStackTrace(); }

Intent intentBack = new Intent(getApplicationContext(), TaskActivity.class); intentBack.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); startActivity(intentBack); }


this is my notification class:


public class TaskNotificationAlarm extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Bundle bundle = intent.getExtras(); String text = bundle.getString("event"); String description = bundle.getString("event description"); String date = bundle.getString("date") "" bundle.getString("time");

Intent intent1 = new Intent(context, AlertDetails.class); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); intent1.putExtra("message", text);

PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent1, PendingIntent.FLAG_ONE_SHOT); NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); NotificationCompat.Builder builder = new NotificationCompat.Builder(context, "notify_001");

RemoteViews contentView = new RemoteViews(context.getPackageName(), R.layout.activity_notification); PendingIntent pendingSwitchIntent = PendingIntent.getBroadcast(context, 0, intent, 0); contentView.setOnClickPendingIntent(R.id.flashButton, pendingSwitchIntent); contentView.setTextViewText(R.id.message, text); contentView.setTextViewText(R.id.date, date); builder.setSmallIcon(R.drawable.ic_baseline_calendar); builder.setAutoCancel(true); builder.setOngoing(true); builder.setAutoCancel(true); builder.setPriority(Notification.PRIORITY_HIGH); builder.setOnlyAlertOnce(true); builder.build().flags = Notification.FLAG_NO_CLEAR | Notification.PRIORITY_HIGH; builder.setContent(contentView); builder.setContentIntent(pendingIntent);

if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { String channelId = "channel_id"; NotificationChannel channel = new NotificationChannel(channelId, "channel name", NotificationManager.IMPORTANCE_HIGH); channel.enableVibration(true); notificationManager.createNotificationChannel(channel); builder.setChannelId(channelId); } Notification notification = builder.build(); notificationManager.notify(1, notification); } }

CodePudding user response:

fellow coder.

I think the issue is that the notification channel is not able to identify the builder.

I would suggest you to create notification channel first then create the builder,declare channel's variable globally in the file.

Also you should separate your notification channel creation code and notification builder code into two separate methods and call it at ones in your onReceive()(TaskNotificationAlarm) in broadcast receiver such as

private void createNotificationChannel() {
  // Write your channel creation code here
  Log.d("TAG","Notification Channel Running")
}
private void createNotification() {
// Write your notification builder code here
Log.d("TAG","Notification Builder Running")
}

Don't Forget to mention your broadcast receiver class in your manifest file like this :

<receiver
        android:name=".TaskNotificationAlarm"
        android:enabled="true"
        android:exported="false" />

Make sure you mention this receiver inside <application></application> tag.

  • Try to fire logs as shown in the code section to verify that the the method is getting called or not.
  • Start a debugging point at the position where the BroadcastReceiver's code is fired.

Try using setAndAllowWhileIdle() method of Alarm Manager to trigger alarm, this method will allow the OS to trigger alarm even if the device is on battery saver or low power mode.

private AlarmManager alarmManager;
alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.setAndAllowWhileIdle (int type, 
            long triggerAtMillis, 
            PendingIntent operation)

And to cancel the alarm you just have to use the predefined alarmManager.cancel() method

For more detailed understanding in Alarm Manager Click Me

CodePudding user response:

The alarmManager.set will fire the alarm at an approximate time and not at the exact time. In order to show the exact alarm you need to use

alarmManager.setAlarmClock()

For more details check this out Schedule alarms

  • Related