How Create Auto Notification that Automatically notify on any time in Android Studio

Create Auto Notification that Automatically notify on any time in Android Studio . here we creating a class that create notification channels then auto notify on specification



Step :1 Create a BroadcastReceiver that used to fire notification

public class AutoNotification_Brodcast extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        Intent notificationIntent = new Intent(context, MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(context,
                0, notificationIntent, 0);

        NotificationCompat.Builder builder = new NotificationCompat.Builder(context, CHANNEL_AUTONOTIFICATION);
        builder.setContentTitle(context.getResources().getString(R.string.autonotification_title));
        builder.setSmallIcon(R.drawable.ic_baseline_autonotification_24);

        builder.setStyle(new NotificationCompat.BigTextStyle().bigText(context.getResources().getString(R.string.autonotification_bigdescription))
                .setBigContentTitle(context.getResources().getString(R.string.autonotification_title)));

        builder.setPriority(NotificationCompat.PRIORITY_DEFAULT);
        builder.setContentIntent(pendingIntent);
        NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(context);
        notificationManagerCompat.notify(1, builder.build());


    }


}

Step :2 add this into AndroidManifest.xml file in application tag

 <application>

         <receiver android:name=".Auto_Notification.AutoNotification_Brodcast">

  </receiver></application>

Step 3: create notification channels class that create a notifiction channels then call autonotification method

public class Notification_Channels extends Application {

     public static final String CHANNEL_NOTICATION = "noticationchannels";
  
    @Override
    public void onCreate() {
        super.onCreate();
        creatnotificationchannel();
        AutoNotificaiton();
       
    }

    private void creatnotificationchannel() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            CharSequence name = "Title of notification channel";
            String description = "decription of notification channels";
            int importance = NotificationManager.IMPORTANCE_HIGH;

            NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_NOTICATION, name, importance);
            notificationChannel.setDescription(description);

            NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
            notificationManager.createNotificationChannel(notificationChannel);
        }
    }
    private void AutoNotificaiton() {

        Calendar calendar = Calendar.getInstance();

        calendar.set(Calendar.HOUR_OF_DAY, 8); // set hour of day on wich 
        calendar.set(Calendar.MINUTE, 00); // minutes of day
        calendar.set(Calendar.SECOND, 00); // secounds of day 

        AlarmManager alarmMgr = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
        Intent intent = new Intent(getApplicationContext(), AutoNotification_Brodcast.class);
        PendingIntent pending = PendingIntent.getBroadcast(getApplicationContext(), 0, intent, 0);
        alarmMgr.setRepeating(AlarmManager.RTC, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pending);

// AlarmManager.INTERVAL_DAY  here you can change interval day to any other for one minutes =1*60*1000
        // for one hour = 60*60*1000

    }

  

}

Thank You Keep Learning Keep Connected #AndroidShortCode

Comments