Can't get Notification to pop up on Oreo (Android 8.0) and higher. What exactly am I doing wrong?

Viewed 50

I tried almost every answer I saw on StackOverflow and CodinginFlow.com but even after everything. My code doesn't still work. Months ago I saw a developer warning; in form of a Toast, that I can't post notifications to the channel, and after searching and searching StackOverflow yesterday, I still haven't gotten it to work. But the Toast message doesn't pop up. I don't know what I missed. Didn't I do it right?

Code below...

AndroidManifest.xml

...

  <application
      android:allowBackup="true"
      android:name=".main.App"
      android:fullBackupContent="true"
      android:icon="@mipmap/timely_app_icon"
      android:label="@string/app_name"
      android:requestLegacyExternalStorage="true"
      android:roundIcon="@mipmap/timely_app_icon_round"
      android:theme="@style/AppTheme">

...

App.java

package com.noah.timely.main;

import android.app.Application;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.os.Build;

import androidx.annotation.RequiresApi;
import androidx.core.content.ContextCompat;

import com.noah.timely.R;

public class App extends Application {
   public static final String ASSIGNMENT_CHANNEL = "TimeLY's assignments";
   public static final String ASIGNMENT_CHANNEL_ID = "com.noah.timely.assignments";
   public static final String ALARMS_CHANNEL = "TimeLY's study alarm";
   public static final String ALARMS_CHANNEL_ID = "com.noah.timely.alarms";
   public static final String SCHEDULED_TIMETABLE_CHANNEL = "TimeLY's Scheduled Classes";
   public static final String SCHEDULED_TIMETABLE_CHANNEL_ID = "com.noah.timely.scheduled";
   public static final String TIMETABLE_CHANNEL = "TimeLY's Timetable";
   public static final String TIMETABLE_CHANNEL_ID = "com.noah.timely.timetable";
   public static final String GENERAL_CHANNEL = "Miscellaneous";
   public static final String GENERAL_CHANNEL_ID = "com.noah.timely";

   @Override
   public void onCreate() {
      super.onCreate();
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
         createNotificationChannels();
      }
   }

   @RequiresApi(api = Build.VERSION_CODES.O)
   private void createNotificationChannels() {
      NotificationManager mgr = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
      NotificationChannel channel1 = new NotificationChannel(ASIGNMENT_CHANNEL_ID, ASSIGNMENT_CHANNEL,
                                                             NotificationManager.IMPORTANCE_DEFAULT);
      channel1.setLightColor(ContextCompat.getColor(this, android.R.color.holo_purple));
      mgr.createNotificationChannel(channel1);

      NotificationChannel channel2 = new NotificationChannel(ALARMS_CHANNEL_ID, ALARMS_CHANNEL,
                                                             NotificationManager.IMPORTANCE_HIGH);
      channel1.setLightColor(ContextCompat.getColor(this, android.R.color.holo_orange_light));
      mgr.createNotificationChannel(channel2);

      NotificationChannel channel3 = new NotificationChannel(SCHEDULED_TIMETABLE_CHANNEL_ID, SCHEDULED_TIMETABLE_CHANNEL,
                                                             NotificationManager.IMPORTANCE_DEFAULT);
      channel1.setLightColor(ContextCompat.getColor(this, android.R.color.holo_green_light));
      mgr.createNotificationChannel(channel3);

      NotificationChannel channel4 = new NotificationChannel(TIMETABLE_CHANNEL_ID, TIMETABLE_CHANNEL,
                                                             NotificationManager.IMPORTANCE_DEFAULT);
      channel1.setLightColor(ContextCompat.getColor(this, android.R.color.holo_red_light));
      mgr.createNotificationChannel(channel4);

      NotificationChannel channel5 = new NotificationChannel(GENERAL_CHANNEL_ID, GENERAL_CHANNEL,
                                                             NotificationManager.IMPORTANCE_DEFAULT);
      channel5.setDescription(getString(R.string.miscellaneous_channel_desc));
      mgr.createNotificationChannel(channel5);
   }

}

And this was how I posted the notification. The code was executed inside a BroadcastReceiver

NotificationManagerCompat manager = NotificationManagerCompat.from(context);

  NotificationCompat.Builder builder = new NotificationCompat.Builder(context, App.ALARMS_CHANNEL_ID);
  builder.setContentTitle("Alarm for: " + (is24 ? _24H : _12H) + (isSnoozeAction ? " (Snoozed)" : ""))
         .setContentText(alarmLabel)
         .setSmallIcon(R.drawable.ic_n_alarm)
         .setColor(ContextCompat.getColor(context, R.color.colorPrimaryDark))
         .setContentIntent(pi)
         .setPriority(NotificationCompat.PRIORITY_HIGH)
         .setSilent(true)
         .setFullScreenIntent(pi, true)
         .addAction(new NotificationCompat.Action(R.drawable.ic_n_snooze, "Snooze", actionSnooze))
         .addAction(new NotificationCompat.Action(R.drawable.ic_n_cancel, "Dismiss", actionDismiss));

  manager.notify(NOTIFICATION_ID, builder.build());

The code above should have displayed a heads-up notification, but only post regular notification. The code below doesn't even post a notification.

  NotificationManagerCompat manager = NotificationManagerCompat.from(context);

  NotificationCompat.Builder builder = new NotificationCompat.Builder(context, App.SCHEDULED_TIMETABLE_CHANNEL_ID);
  builder.setStyle(new NotificationCompat.BigTextStyle().bigText(spannedMessage))
         .setContentTitle("Scheduled class reminder")
         .setContentText(spannedMessage)
         .setAutoCancel(true)
         .setPriority(NotificationCompat.PRIORITY_DEFAULT)
         .setSmallIcon(R.drawable.ic_n_schedule)
         .setColor(ContextCompat.getColor(context, R.color.colorPrimaryDark))
         .setSound(DEFAULT_URI)
         .setContentIntent(pi);

  manager.notify(-299, builder.build());
0 Answers
Related