0

I am trying to create a notification that I do not need to popup on user screen, but I need it to be displayed in the notification bar. Please tell me what is wrong with the following code, because it does not output any notifications:

    Intent notificationIntent = new Intent(this, ToDoList.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
            notificationIntent, 0);
    NotificationCompat.Builder builder =
            new NotificationCompat.Builder(getApplicationContext())
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setContentTitle("Title")
                    .setContentIntent(pendingIntent)
                    .setContentText("Notification text");

    Notification notification = builder.build();

    NotificationManager notificationManager =
            (NotificationManager) getApplicationContext().getSystemService(NOTIFICATION_SERVICE);
    notificationManager.notify(NOTIF_ID, notification);
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
gaspar
  • 9
  • 3

2 Answers2

0

I'm using below func to create a simple notification(it is written in Kotlin)

private fun showNotification(context: Context, title: String, body: String) {
        val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager

        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            val mChannel = NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_HIGH)
            notificationManager.createNotificationChannel(mChannel)
        }

        val notificationBuilder = NotificationCompat.Builder(context, channelId)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle(title)
                .setContentText(body)
                .setAutoCancel(true)

        val stackBuilder = TaskStackBuilder.create(context)
        val resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT)
        notificationBuilder.setContentIntent(resultPendingIntent)
        notificationManager.notify(notificationId, notificationBuilder.build())
    }
Zafer Celaloglu
  • 1,438
  • 1
  • 17
  • 28
0

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