0

I'm trying to pop up a notification when my alarm manager fires my onReceive() method. This is what I have done

@Override
public void onReceive(Context context, Intent intent) {
    PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
    PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "YOUR TAG");
    //Acquire the lock
    wl.acquire(10000);

    startNotification(context);

    wl.release();

}


public void setAlarm(Context context){
    AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent(context, AlarmManagerBroadcastReceiver.class);
    intent.putExtra(Activity, "MainActivity.class");
    PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 0);
    assert am != null;
    am.set(AlarmManager.RTC_WAKEUP, 60000, pi);
}


private void startNotification(Context context){
    // Sets an ID for the notification
    int mNotificationId = 001;
    NotificationManager notificationManager;
    NotificationCompat.Builder mBuilder;

            // Build Notification , setOngoing keeps the notification always in status bar
    mBuilder =
            new NotificationCompat.Builder(context)
                    .setSmallIcon(R.drawable.ic_launcher_foreground)
                    .setContentTitle("RandomTitle")
                    .setContentText("RandomText")
                    .setOngoing(true);

    // Create pending intent, mention the Activity which needs to be
    //triggered when user clicks on notification(StopScript.class in this case)

    Intent notificationIntent = new Intent(context, MainActivity.class);
    notificationIntent.putExtra("extra","Extra Notificacion");
    notificationIntent.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT |
            Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent , PendingIntent.FLAG_UPDATE_CURRENT);
   // context.startActivity(notificationIntent);

    mBuilder.setContentIntent(contentIntent);


    // Gets an instance of the NotificationManager service
     notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

    //Android Oreo
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationChannel channel = new NotificationChannel("notify_001",
                "Channel human readable title",
                NotificationManager.IMPORTANCE_DEFAULT);
        notificationManager.createNotificationChannel(channel);
    }

    // Builds the notification and issues it.
    notificationManager.notify(mNotificationId, mBuilder.build());
}

Im really confused why this notification is not showing, I have tested my alarm and it triggers after 1 minute of beign created, but the notification is still not showing.

Any ideas?

thanks

Todd
  • 169
  • 14

2 Answers2

1

As per Android Developers Documentation :

NotificationCompat.Builder NotificationCompat.Builder (Context context) This constructor was deprecated in API level 26.1.0. use NotificationCompat.Builder(Context, String) instead. All posted Notifications must pecify a NotificationChannel Id.

Reference here
Edit: Try the following :

  1. Leave the NotificationCompat.Builder as it is now (with a string representing NotificationChannel) .
  2. Comment out your if block where you create a notification channel

  3. Replace your NotificationManager with NotificationManagerCompat as the following :

    NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
    
                notificationManager.notify(mNotificationId, mBuilder.build());
    
Anas.J
  • 81
  • 1
  • 8
1

From Android developer:

When you target Android 8.0 (API level 26), you must implement one or more notification channels. If your targetSdkVersion is set to 25 or lower, when your app runs on Android 8.0 (API level 26) or higher, it behaves the same as it would on devices running Android 7.1 (API level 25) or lower.

Because your targetSdkVersion is 28, so you must add channelId in the Builder constructor too.

Change your code to:

// Build Notification , setOngoing keeps the notification always in status bar
mBuilder = new NotificationCompat.Builder(context, "notify_001") // Add channel ID to the constructor.
        .setSmallIcon(R.drawable.ic_launcher_foreground)
        .setContentTitle("RandomTitle")
        .setContentText("RandomText")
        .setOngoing(true);
Son Truong
  • 10,947
  • 5
  • 23
  • 50