This is an example of a custom notification I created in a devotional app that notifies the user every day at a set time(8:00AM). For recent versions of Android the channel id will be required as shown in the code.
Your NotificationReceiver class should look like this
public class NotificationReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Intent intent1 = new Intent(context, MainActivity.class);
intent1.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
//if we want ring on notifcation then uncomment below line//
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 100, intent1, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder notification = new NotificationCompat.Builder(context, CHANNEL_1_ID)
.setSmallIcon(R.drawable.ic_mobile_app_icon)
.setContentIntent(pendingIntent)
.setContentText("Have you read your devotional today?")
.setContentTitle("Daily Devotional")
.setSound(alarmSound)
.setColor(Color.BLUE)
.setSound(alarmSound)
.setVibrate(new long[] { 1000, 1000, 1000, 1000, 1000 })
.setLights(Color.BLUE, 3000, 3000)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setCategory(NotificationCompat.CATEGORY_MESSAGE)
.setAutoCancel(true);
notificationManager.notify(100, notification.build());
}
}
You can call it maybe in your splashscreen or anywhere
public static void devotionalNotification(Context context) {
calendar.set(Calendar.HOUR_OF_DAY, 8);
calendar.set(Calendar.MINUTE, 00);
calendar.set(Calendar.SECOND, 00);
Intent intent = new Intent(context.getApplicationContext(),
NotificationReceiver.class);
PendingIntent pendingIntent =
PendingIntent.getBroadcast(context.getApplicationContext(), 100,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager)
context.getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
}
The Notification reciver should be added to your manifest like so
<receiver android:name=".util.NotificationReceiver" />
I hope the above guide help you create push notification for your app. For more on android push notifications you can check here