2

I am trying to show notification at specific date I am using alarm manager and broadcast receiver to show notifications but the problem is notification works only when app is open and when app is closed notification does not show. Below is my code:

Reminder.java

public class Reminder extends AppCompatActivity {

long reminderDateTimeInMilliseconds = 000;
Button but;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_reminder);

    but = findViewById(R.id.but);

    but.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            createNotifyChannel();

            Intent intent = new Intent(Reminder.this,ReminderBroadcast.class);
            PendingIntent pendingIntent = PendingIntent.getBroadcast(Reminder.this,0,intent,0);


            AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);

            Calendar calendarToSchedule = Calendar.getInstance();
            calendarToSchedule.setTimeInMillis(System.currentTimeMillis());
            calendarToSchedule.clear();

            //.Set(Year, Month, Day, Hour, Minutes, Seconds);
            calendarToSchedule.set(2020, 8, 20, 19, 12, 0);
            reminderDateTimeInMilliseconds = calendarToSchedule.getTimeInMillis();

            alarmManager.setExact(AlarmManager.RTC_WAKEUP,reminderDateTimeInMilliseconds,pendingIntent);

        }
    });
}

private void createNotifyChannel(){

      if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){

          CharSequence name = "ReminChannel";
          String desc = "This is my channel";
          int importance = NotificationManager.IMPORTANCE_DEFAULT;
          NotificationChannel channel = new NotificationChannel("mynotif",name,importance);
          channel.setDescription(desc);

          NotificationManager notificationManager = getSystemService(NotificationManager.class);
          notificationManager.createNotificationChannel(channel);
      }
   }
}

ReminderBroadcast.java

public class ReminderBroadcast extends BroadcastReceiver {

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

NotificationCompat.Builder notif = new NotificationCompat.Builder(context,"mynotif")
                                   .setContentTitle("Appointment reminder")
                                   .setContentText("Hello there")
                                   .setPriority(NotificationCompat.PRIORITY_DEFAULT);

NotificationManagerCompat manager = NotificationManagerCompat.from(context);
manager.notify(200,notif.build());

  }
}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.app.firstapp">

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".Reminder">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>
<activity android:name=".ThirdActivity" />
<activity android:name=".SecondActivity" />
<activity android:name=".MainActivity">

</activity>

<receiver android:name=".ReminderBroadcast"/>

</application>

</manifest>

What am I doing wrong?

halfer
  • 19,824
  • 17
  • 99
  • 186
Digvijay
  • 2,887
  • 3
  • 36
  • 86
  • This question of mine will help you: https://stackoverflow.com/questions/63303559/how-to-show-daily-offline-notifications-in-android-10 – null_override Sep 20 '20 at 15:49

0 Answers0