Skip to content

Android Java Notification Not Displaying

This is my first time working with Notifications and I can’t seem to figure out the issue.

What I am Trying To do:

In short, I have a service that checks for a variable in a firebase document, if it’s true, show a notification. And before you say it, I can’t use Cloud Functions in this scenario. I’ll explain, everything in detail, so bear with me, thanks.

What’s Happening

Based on my logs, I have an idea of what’s happening, let me summarize it in a few short points.

  1. The service is running every 30 minutes, By Job Scheduler. That is the “Job started” log.
  2. It calls the check bookings function that checks for the above mentioned variable, that is the “Checkbookings”, “true”, and “false” logs.
  3. The final log is “Add notification” which is in the addNotifcation function where I build the notification. This log appears after the “true” log.

The issue

Add notification is called but the notification is not shown. Logs And Emulator

I will paste the Logs, relevant code below any input or suggestion will be appreciated, thanks

Logs

D/Notification Service: Job started
D/Notification Service: Check Bookings
D/Notification Service: Job finished
D/Notification Service: true
D/Notification Service: Check Bookings On Success
D/Notification Service: Add Notification
D/Notification Service: Job started
D/Notification Service: Check Bookings
D/Notification Service: false
D/Notification Service: Job finished
D/Notification Service: Job started
D/Notification Service: Check Bookings
D/Notification Service: true
    Check Bookings On Success
    Add Notification
D/Notification Service: Job finished
W/ample.rehnseha: Accessing hidden method Ldalvik/system/CloseGuard;->close()V (greylist,core-platform-api, linking, allowed)
D/Notification Service: Job started
D/Notification Service: Check Bookings
D/Notification Service: false
D/Notification Service: Job finished

Code

private void doBackgroundWork(final JobParameters params) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                checkBookings();
                if (jobCancelled) {
                    return;
                }
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                Log.d(TAG, "Job finished");
                jobFinished(params, true);
            }
        }).start();
    }

    public void checkBookings(){
        String userId = FirebaseAuth.getInstance().getCurrentUser().getUid();
        Log.d(TAG,"Check Bookings");
        FirebaseFirestore.getInstance().collection("users").document(userId).get()
                .addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
                    @Override
                    public void onSuccess(DocumentSnapshot documentSnapshot) {
                        Log.d(TAG,""+documentSnapshot.get("notification"));
                        if ((boolean) documentSnapshot.get("notification")){
                            Log.d(TAG,"Check Bookings On Success");
                            addNotification();
                        }
                    }
                });
    }

    private void addNotification() {
        Log.d(TAG,"Add Notification");
        NotificationCompat.Builder builder =
                new NotificationCompat.Builder(this, NotificationChannel.DEFAULT_CHANNEL_ID)
                        .setSmallIcon(R.drawable.ic_launcher_foreground)
                        .setContentTitle("New Booking!")
                        .setAutoCancel(true)
                        .setContentText("One of your properties was booked recently, Respond Now!");

        Intent notificationIntent = new Intent(this, AddProperty.class);
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent,
                PendingIntent.FLAG_UPDATE_CURRENT);
        builder.setContentIntent(contentIntent);

        // Add as notification
        NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        manager.notify(0, builder.build());
        FirebaseFirestore.getInstance().collection("users").document(FirebaseAuth.getInstance().getCurrentUser().getUid())
                .update("notification",false);
    }

If you need any more details, let me know

Answer

When you want to create a notification, you need to first create a channel for it. Here is an example:

 private void createNotificationChannel() {
    // Create the NotificationChannel, but only on API 26+ because
    // the NotificationChannel class is new and not in the support library
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        CharSequence name = CHANNEL_NAME;
        String description = CHANNEL_DESC;
        int importance = NotificationManager.IMPORTANCE_DEFAULT;
        NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
        channel.setDescription(description);
        // Register the channel with the system; you can't change the importance
        // or other notification behaviors after this
        NotificationManager notificationManager = getSystemService(NotificationManager.class);
        notificationManager.createNotificationChannel(channel);
    }
}

After this you can call add notification:

  private void addNotification() {
    Log.d(TAG,"Add Notification");
    NotificationCompat.Builder builder =
            new NotificationCompat.Builder(this, CHANNEL_ID)
                    .setSmallIcon(R.drawable.ic_launcher_foreground)
                    .setContentTitle("New Booking!")
                    .setAutoCancel(true)
                    .setContentText("One of your properties was booked recently, Respond Now!");

    Intent notificationIntent = new Intent(this, AddProperty.class);
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);
    builder.setContentIntent(contentIntent);

    // Add as notification
    NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    manager.notify(0, builder.build());
    FirebaseFirestore.getInstance().collection("users").document(FirebaseAuth.getInstance().getCurrentUser().getUid())
            .update("notification",false);
}

This should work