Trying to implement alarm with a notification - java

I am getting "The method getSystemService(String) is undefined for the type AlarmManagerBroadcastReceiver" error. I even tried to put getActivity() before it but it was of no help.
code for AlarmManagerBroadcastReceiver.java
package com.archana.pocketfriendly;
import java.text.Format;
import java.text.SimpleDateFormat;
import android.app.AlarmManager;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.TaskStackBuilder;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.PowerManager;
import android.os.Vibrator;
import android.support.v4.app.NotificationCompat;
import android.widget.Toast;
public class AlarmManagerBroadcastReceiver extends BroadcastReceiver {
final public static String ONE_TIME = "onetime";
MediaPlayer alarm;
#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();
//You can do the processing here update the widget/remote views.
Bundle extras = intent.getExtras();
StringBuilder msgStr = new StringBuilder();
if(extras != null && extras.getBoolean(ONE_TIME, Boolean.FALSE)){
msgStr.append("One time Timer : ");
}
Format formatter = new SimpleDateFormat("hh:mm:ss a");
alarm = MediaPlayer.create(context, R.raw.alarm);
alarm.start();
msgStr.append("Do you want to enter the expenses?\nIgnore if already done!");
Toast.makeText(context, msgStr, Toast.LENGTH_LONG).show();
Vibrator vib=(Vibrator)context.getSystemService(Context.VIBRATOR_SERVICE);
vib.vibrate(2000);
// notification
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.settings)
.setContentTitle("My notification")
.setContentText("Hello World!");
// Creates an explicit intent for an Activity in your app
Intent resultIntent = new Intent(context, MainMenu.class);
// The stack builder object will contain an artificial back stack for the
// started Activity.
// This ensures that navigating backward from the Activity leads out of
// your application to the Home screen.
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
// Adds the back stack for the Intent (but not the Intent itself)
stackBuilder.addParentStack(MainMenu.class);
// Adds the Intent that starts the Activity to the top of the stack
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); //error in this `enter code here`line.
mNotificationManager.notify(0, mBuilder.build());
//Release the lock
wl.release();
}
public void SetAlarm(Context context)
{
AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, AlarmManagerBroadcastReceiver.class);
intent.putExtra(ONE_TIME, Boolean.FALSE);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 0);
//After after 30 seconds
am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1000 * 86400 , pi);
}
public void CancelAlarm(Context context)
{
Intent intent = new Intent(context, AlarmManagerBroadcastReceiver.class);
PendingIntent sender = PendingIntent.getBroadcast(context, 0, intent, 0);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(sender);
}
public void setOnetimeTimer(Context context){
AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, AlarmManagerBroadcastReceiver.class);
intent.putExtra(ONE_TIME, Boolean.TRUE);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 0);
am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), pi);
}
}
Please let me know if any other information is required.

Just like your other calls to getSystemService, you need to perform it on a context object:
NotificationManager mNotificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

Related

Open app from notification “App is running in the background”

I need to make my app open after clicking on the notification “App is running in the background”, but when I clicked it open the App Infos, how can I avoid this and open the app itself.
Here is the code that I using to show this notification:
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
NotificationChannel channel = new NotificationChannel("Player", "Sync Service", NotificationManager.IMPORTANCE_HIGH);
channel.setDescription("Service Name");
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.createNotificationChannel(channel);
Notification.Builder builder = new Notification.Builder(this, "Player")
.setContentTitle("Music Player")
.setContentText("My Music")
.setAutoCancel(true)
.setContentIntent(pendingIntent)
.setOngoing(true);
Notification notification = builder.build();
startForeground(1, notification);
Obs. this app really uses a background service and I don't want to hide this notification.
Try this one.
package developer.eyosiyas.NileSat.Habesha.service;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import androidx.annotation.Nullable;
import androidx.core.app.NotificationCompat;
import developer.eyosiyas.NileSat.Habesha.R;
import developer.eyosiyas.NileSat.Habesha.View.MainActivity;
public class ServiceExample extends Service {
#Nullable
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;
}
#Override
public void onCreate() {
super.onCreate();
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
NotificationManager notificationManager;
notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
NotificationChannel channel = new NotificationChannel("Player", "Sync Service", NotificationManager.IMPORTANCE_HIGH);
channel.enableLights(true);
channel.enableVibration(true);
channel.setLightColor(R.color.colorPrimary);
channel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
channel.setDescription("Service Name");
notificationManager.createNotificationChannel(channel);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "Player")
.setContentTitle("Music Player")
.setContentText("My Music")
.setAutoCancel(false)
.setContentIntent(pendingIntent)
.setOngoing(true);
Notification notification = builder.build();
notificationManager.notify(1, notification);
startForeground(1, notification);
}
}

updating the views of a RemoteView during media playback

I'm using the following code to generate a mediaPlayer notification:
package com.app1.notificationtemplate;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Handler;
import android.widget.RemoteViews;
import androidx.core.app.NotificationCompat;
public class NotificationGenerator {
public static final int NOTIFICATION_ID_OPEN_ACTIVITY = 9;
public static final String NOTIFY_PREVIOUS = "com.a.b.previous";
public static final String NOTIFY_DELETE = "com.a.b.delete";
public static final String NOTIFY_PAUSE = "com.a.b.pause";
public static final String NOTIFY_PLAY = "com.a.b.play";
public static final String NOTIFY_NEXT = "com.a.b.next";
public static void openActivityNotificaton(Context context) {
NotificationCompat.Builder nc = new NotificationCompat.Builder(context,
MainActivity.channelID);
NotificationManager nm = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
Intent notifyIntent = new Intent(context, MainActivity.class);
notifyIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT);
nc.setContentIntent(pendingIntent);
nc.setSmallIcon(R.mipmap.ic_launcher);
nc.setAutoCancel(true);
nc.setContentTitle("Notification Demo");
nc.setContentText("Click please");
nm.notify(NOTIFICATION_ID_OPEN_ACTIVITY, nc.build());
}
public static void customBigNotification(Context context){
final RemoteViews expandedView = new RemoteViews(context.getPackageName(), R.layout.big_notification);
NotificationCompat.Builder nc = new NotificationCompat.Builder(context, MainActivity.channelID);
NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Intent notifyIntent = new Intent(context, MainActivity.class);
notifyIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT);
nc.setContentIntent(pendingIntent);
nc.setSmallIcon(R.drawable.play);
nc.setAutoCancel(true);
nc.setCustomBigContentView(expandedView);
nc.setContentTitle("MusicPlayer");
nc.setPriority(NotificationCompat.PRIORITY_MAX);
nc.setContentText("Control AUdio");
//expandedView.setTextViewText(R.id.text_song_name,"Adele");
setListeners(expandedView, context);
Notification notification = nc.build();
notification.flags = NotificationCompat.FLAG_ONGOING_EVENT|NotificationCompat.FLAG_FOREGROUND_SERVICE
| NotificationCompat.FLAG_NO_CLEAR|NotificationCompat.FLAG_BUBBLE ;
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
nm.notify("newNotif", 1, nc.build());
}
public static void setListeners(RemoteViews view, Context context){
Intent previous = new Intent(NOTIFY_PREVIOUS);
Intent delete = new Intent(NOTIFY_DELETE);
Intent pause = new Intent(NOTIFY_PAUSE);
Intent next = new Intent(NOTIFY_NEXT);
Intent play = new Intent(NOTIFY_PLAY);
PendingIntent pPrevious = PendingIntent.getBroadcast(context, 0,
previous, PendingIntent.FLAG_UPDATE_CURRENT);
view.setOnClickPendingIntent(R.id.btnPrevious, pPrevious);
PendingIntent pDelete = PendingIntent.getBroadcast(context, 0,
delete, PendingIntent.FLAG_UPDATE_CURRENT);
view.setOnClickPendingIntent(R.id.btnDelete, pDelete);
PendingIntent pPause = PendingIntent.getBroadcast(context, 0,
pause, PendingIntent.FLAG_UPDATE_CURRENT);
view.setOnClickPendingIntent(R.id.btnPause, pPause);
PendingIntent pPlay = PendingIntent.getBroadcast(context, 0,
play, PendingIntent.FLAG_UPDATE_CURRENT);
view.setOnClickPendingIntent(R.id.btnPlay, pPlay);
PendingIntent pNext = PendingIntent.getBroadcast(context, 0,
next, PendingIntent.FLAG_UPDATE_CURRENT);
view.setOnClickPendingIntent(R.id.btnNext, pNext);
}
}
I have custom notification views (RemoteViews) setup for the notification as well.
but, when I click play, I'm not sure how to update the play view to a pause view. Currently, I'm just setting the visibility of the play button to gone. But, is turning on and off the visibility turnwise the only option? And how do I do that anyways(the tutorial I've followed doesn't really show this). But, I think it'd be better if we could replace the image in the button which would avoid possible blinking or flickering. I've also gotta update the Remote view's artist and song Title name, but have no idea to do so. Please tell me how to update views in the RemoteView according to user interaction
Also, to communicate from the notification to the Service is using Broadcasts the only option? What other options do I have?
You need to create a new notification with the updated data and use NotificationManager to update it. No need to start service again. You have a running foreground service, all you have to do is update your UI via notify.
So in your Broadcast Receiver call your openActivityNotificaton() function with updated data, according to what view was clicked inside the notification. You can have one Receiver with a unique action for each view click, and update notification accordingly. I tested and it does not cause a blink, the notification is updated smoothly.
The code for your Receiver would be something like this:
const val NOTIFICATION_TITLE_INTENT_ACTION = "notification_title_intent_action"
const val NOTIFICATION_ARROW_INTENT_ACTION = "notification_arrow_intent_action"
class NotificationInteractionReceiver : BroadcastReceiver() {
private lateinit var notificationsFactory: NotificationFactory
override fun onReceive(context: Context, intent: Intent) {
notificationsFactory = NotificationFactory(context)
// Update title according to what button was clicked
val newTitle = when (intent.action) {
NOTIFICATION_TITLE_INTENT_ACTION -> "title clicked"
else -> "arrow clicked"
}
// Pass new title to class responsible for showing notification
notificationsFactory.showNotification(context, s)
}
companion object {
fun getIntent(context: Context, action: String): Intent {
val intent = Intent(context, NotificationInteractionReceiver::class.java).apply {
flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
}
intent.action = action
return intent
}
}
}

Android - Notification at specific time every day

I am trying to add a notification feature to my application. I want it to run a notification or action at the same time, every day. I have this code for my notification right now:
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import android.view.View;
public class MainActivity extends AppCompatActivity {
NotificationCompat.Builder notification;
private static final int uniqueID = 45612;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
notification = new NotificationCompat.Builder(this);
notification.setAutoCancel(true);
// Build the notification
notification.setSmallIcon(R.drawable.icon);
notification.setTicker("Brook Betterment Plan");
notification.setWhen(System.currentTimeMillis());
notification.setContentTitle("Brook Betterment Plan");
notification.setContentText("Don't forget to enter your daily stats! ");
Intent intent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
notification.setContentIntent(pendingIntent);
// Builds notification and issues it
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
nm.notify(uniqueID, notification.build());
}
}
Thanks! Hope someone knows the answer. Also, I was hoping it wouldn't need any additional Activitys or Java class's.
Alarm Manager
public static void startAlarmBroadcastReceiver(Context context) {
Intent _intent = new Intent(context, AlarmBroadcastReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, _intent, 0);
AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(pendingIntent);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 23);
calendar.set(Calendar.MINUTE, 59);
calendar.set(Calendar.SECOND, 0);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
}
Alarm Broadcast Receiver
In AndroidManifest, just define the class as
<receiver android:name=".AlarmBroadcastReceiver" >
</receiver>
And code will be like
public class AlarmBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
showNotification(context);
}
void showNotification(Context context) {
String CHANNEL_ID = "your_name";// The id of the channel.
CharSequence name = context.getResources().getString(R.string.app_name);// The user-visible name of the channel.
NotificationCompat.Builder mBuilder;
Intent notificationIntent = new Intent(context, TestActivity.class);
Bundle bundle = new Bundle();
notificationIntent.putExtras(bundle);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
if (android.os.Build.VERSION.SDK_INT >= 26) {
NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, name, NotificationManager.IMPORTANCE_HIGH);
mNotificationManager.createNotificationChannel(mChannel);
mBuilder = new NotificationCompat.Builder(context)
.setSmallIcon(R.mipmap.ic_launcher)
.setLights(Color.RED, 300, 300)
.setChannelId(CHANNEL_ID)
.setContentTitle("Title");
} else {
mBuilder = new NotificationCompat.Builder(context)
.setSmallIcon(R.mipmap.ic_launcher)
.setPriority(Notification.PRIORITY_HIGH)
.setContentTitle("Title");
}
mBuilder.setContentIntent(contentIntent);
mBuilder.setContentText("Your Text");
mBuilder.setAutoCancel(true);
mNotificationManager.notify(1, mBuilder.build());
}
}
Create a class that will generate notification, like LocalNotification
Add the code you have in on create in that class under a method like showDailyNotification
Now use JobScheduler and schedule a job for every 24 hours, when the job is started call this method in LocalNotification
Make LocalNotification a singleton class.
BTW don't forget to use notification channel because otherwise it will not work on Oreo and above devices.

Notification Error FLAG_ACTIVITY_NEW_TASK

I am currently recieving an eror i have not experienced before. I want a basic notification to show when AlarmReciever.java is executed. The error is for FLAG_ACTIVITY_NEW_TASK.
Can anyone help with a solution?
Thank you!
AlarmReceiver:
package servicealarmdemo.test2;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.v4.app.NotificationCompat;
import android.widget.Toast;
public class AlarmReceiver extends BroadcastReceiver {
private static final int MY_NOTIFICATION_ID=1;
NotificationManager notificationManager;
Notification myNotification;
private final String myBlog = "http://android-er.blogspot.com/";
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Alarm received!", Toast.LENGTH_LONG).show();
Intent myIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(myBlog));
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, myIntent,Intent.FLAG_ACTIVITY_NEW_TASK);
myNotification = new NotificationCompat.Builder(context)
.setContentTitle("Exercise of Notification!")
.setContentText("http://android-er.blogspot.com/")
.setTicker("Notification!")
.setWhen(System.currentTimeMillis())
.setContentIntent(pendingIntent)
.setDefaults(Notification.DEFAULT_SOUND)
.setAutoCancel(true)
.build();
notificationManager =
(NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(MY_NOTIFICATION_ID, myNotification);
}
}
public static PendingIntent getActivity (Context context, int requestCode, Intent intent, int flags){
...
}
The last parameter May be FLAG_ONE_SHOT, FLAG_NO_CREATE, FLAG_CANCEL_CURRENT, FLAG_UPDATE_CURRENT, or any of the flags as supported byIntent.fillIn() to control which unspecified parts of the intent that can be supplied when the actual send happens.
So your code could be like this:
Intent myIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(myBlog));
myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, myIntent,PendingIntent.FLAG_ONE_SHOT);

Notification at certain time using a Service

I am developing an Android prayer app for Muslims. I have already developed the code to obtain the prayer times with the help of praytimes.org. Now I want to make it so that the user is notified at that time to pray. But for now, I just want to be able to send a notification at a certain time. Looking at the code and online resources, it seems to me as if it should work but for some reason, the notification is not displaying. I know the code for the notification part is correct because I created a button to create a notification using that code and it works. So I believe the problem is with the service, receiver, or alarmManager.
My guess is the problem has to do with the onStartCommand() method in MyNotificationService.java. I am not too familiar with services and their life-cycles so please go easy on me.
Thanks in advance.
In my MainActivity.java, here is the code in my onCreate() method.
/*========================Begin Notification Code==============================*/
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.MONTH, 1);
calendar.set(Calendar.YEAR, 2015);
calendar.set(Calendar.DAY_OF_MONTH, 18);
calendar.set(Calendar.HOUR_OF_DAY, 4);
calendar.set(Calendar.MINUTE, 45);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.AM_PM,Calendar.PM);
Intent intent = new Intent(MainActivity.this, MyReceiver.class);
pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, intent,0);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
/*========================End Notification Code==============================*/
Here is MyReceiver.class
package com.ahmed.omar.tawheed;
/**
* Created by Omar on 1/18/15.
*/
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class MyReceiver extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent intent)
{
Intent service = new Intent(context, MyNotificationService.class);
context.startService(service);
}
}
Here is MyNotificationService.java
package com.ahmed.omar.tawheed;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.support.v4.app.NotificationCompat;
public class MyNotificationService extends Service {
private NotificationManager notificationManager;
private int prayerNotificationID = 420; //blazeIt
#Override
public IBinder onBind(Intent arg0)
{
// TODO Auto-generated method stub
return null;
}
#Override
public void onCreate()
{
// TODO Auto-generated method stub
super.onCreate();
}
#Override
public int onStartCommand(Intent intent1, int flag, int startId) {
Intent intent = new Intent(this, MainActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);
Notification n = new NotificationCompat.Builder(this)
.setContentTitle("Time to Pray")
.setContentText("It is now time to pray salah")
.setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(pIntent)
.setAutoCancel(true)
.build();
notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
n.flags = Notification.FLAG_AUTO_CANCEL;
// Sending the notification itself
notificationManager.notify(prayerNotificationID, n);
return START_STICKY;
}
#Override
public void onDestroy()
{
// TODO Auto-generated method stub
super.onDestroy();
}
}
Here are the lines from my AndroidManifest.xml
<service android:name=".MyNotificationService"
android:enabled="true" />
<receiver android:name=".MyReceiver"/>
See the documentation for Calendar.MONTH. The valid values start at 0 (for January), not 1. So, your code is trying to set an alarm on February 18, not January 18. Try changing the Calendar.MONTH value to 0 instead of 1.
This is what i do for send notification.
fun showNotification(context: Context,title: String, message: String) {
Log.e("notification", "visible")
val mNotificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
val channel = NotificationChannel("1",
"NAMAJ_CHANNEL",
NotificationManager.IMPORTANCE_DEFAULT)
channel.description = "namaj alarm channel"
mNotificationManager.createNotificationChannel(channel)
}
val mBuilder = NotificationCompat.Builder(context, "1")
.setSmallIcon(com.waltonbd.muslimcalender.R.mipmap.ic_launcher) // notification icon
.setContentTitle(title) // title for notification
.setContentText(message)// message for notification
.setAutoCancel(true) // clear notification after click
val intent = Intent(context, MainActivity::class.java)
val pi = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)
mBuilder.setContentIntent(pi)
mNotificationManager.notify(0, mBuilder.build())
}

Categories