Schedule Local Notification in Android Studio using Broadcast Receiver and Alarm Manager Step by Step | NEW METHOD
Source Code
#AndroidManifest
<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>
<receiver android:enabled="true" android:exported="true" android:name=".broadcast_receiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<action android:name="android.intent.action.QUICKBOOT_POWERON"/>
</intent-filter>
</receiver>
#BroadcastReceiver.class
public class broadcast_receiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent repeating_intent = new Intent(context,daily_word.class);
repeating_intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, repeating_intent, PendingIntent.FLAG_IMMUTABLE);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, "Notification")
.setContentIntent(pendingIntent)
.setSmallIcon(R.drawable.turkey_flag)
.setColor(ContextCompat.getColor(context, R.color.red))
.setContentTitle("Reminder")
.setContentText("Notification Reminder")
.setAutoCancel(true);
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
notificationManager.notify(200, builder.build());
}
}
///////////////////////////////////////////////////////////
#MainActivity
PendingIntent pending_intent;
AlarmManager alarm_manager;
notification_channel();
pending_intent = PendingIntent.getBroadcast(getApplicationContext(), 0, new Intent(this, broadcast_receiver.class), PendingIntent.FLAG_IMMUTABLE);
alarm_manager = (AlarmManager) getSystemService(ALARM_SERVICE);
private void notification_channel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = "Learn Turkish";
String description = "Learn Turkish Channel";
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel("Notification", name, importance);
channel.setDescription(description);
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
}
public void set_notification_alarm(long interval) {
long triggerAtMillis = System.currentTimeMillis() + interval;
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
if (Build.VERSION.SDK_INT >= 23) {
alarm_manager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), interval, pending_intent);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
alarm_manager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, triggerAtMillis, pending_intent);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
alarm_manager.setExact(AlarmManager.RTC_WAKEUP, triggerAtMillis, pending_intent);
} else {
alarm_manager.set(AlarmManager.RTC_WAKEUP, triggerAtMillis, pending_intent);
}
}
public void cancel_notification_alarm() {
alarm_manager.cancel(pending_intent);
}
Watch This Video
《《《 Links 》》》
To Subscribe to our YouTube Channel
Source Code + Project
Comments
Post a Comment