Search code examples
androidnotificationscalendaralarmmanagernotificationmanager

AlarmManager + NotificationManager not working


First of all I would like to thank you for clicking on this thread. Thank you so much for taking your time to read this.

I was planning for the application push a notification on a specific time. I followed some tutorials online but it seems it doesn't work.

There is no error message on the code itself but no notification is coming out.

Here's my "Notification_receiver.java" file:

package com.example.reviewerapplication;

import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Build;

import androidx.annotation.RequiresApi;
import androidx.core.app.NotificationCompat;

public class Notification_receiver extends BroadcastReceiver {



@Override
public void onReceive(Context context, Intent intent) {

    NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);

    Intent repeating_intent = new Intent(context,Chapterchoice.class);
    String daily10 = "daily";
    intent.putExtra("daily",daily10);
    repeating_intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(context,100,repeating_intent,PendingIntent.FLAG_UPDATE_CURRENT);



    NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
            .setContentIntent(pendingIntent)
            .setSmallIcon(R.drawable.ic_launcher)
            .setContentTitle("Sample Title")
            .setContentText("same message")
            .setAutoCancel(true);

    if (intent.getAction().equals("MY_NOTIFICATION_MESSAGE")) {
        notificationManager.notify(100, builder.build());
    }


  }
}

This is what is on my MainActivity:

       //Daily Notification
    val calendar = Calendar.getInstance()

    calendar.set(Calendar.HOUR_OF_DAY,21)
    calendar.set(Calendar.MINUTE,53)
    calendar.set(Calendar.SECOND,0)

    val intent2 = Intent(this, Notification_receiver::class.java)
    intent2.setAction("MY_NOTIFICATION_MESSAGE")
    var pendingIntent = PendingIntent.getBroadcast(this,100,intent2,PendingIntent.FLAG_UPDATE_CURRENT )
    var alarmManager = getSystemService(Context.ALARM_SERVICE) as AlarmManager
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,calendar.timeInMillis,AlarmManager.INTERVAL_DAY,pendingIntent)

Solution

  • I actually got it fixed just now.

    my issue is that I used "activity" in the Manifest instead of using "receiver"