0

It's supposed to be a simple to do list app. You can set a list Item with a task and time. It should send you a notification at the given time. Everything works fine when the app is running but when I close it I don't get notifications. I tried on API 23 and API R. I was looking at other posts but I couldn't find any solution.

Receiver class:

public class AlertReceiver extends BroadcastReceiver {
     NotificationManagerCompat notificationManager;
    @Override
    public void onReceive(Context context, Intent intent) {

        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());



        boolean is24HourFormat = android.text.format.DateFormat.is24HourFormat(context);
        CharSequence setTime;
        if (is24HourFormat) {

            setTime = DateFormat.format("HH:mm", calendar);

        } else {

            setTime = DateFormat.format("hh:mm a", calendar);
        }
        String currentTime = setTime.toString();






        DataBaseHelper dataBaseHelper = new DataBaseHelper(context);

        Item itemForNotificationText = dataBaseHelper.GetNotificationContent(currentTime);




        NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
        Notification notification = new NotificationCompat.Builder(context,CHANNEL_1_ID)
                .setSmallIcon(R.drawable.ic_baseline_notifications_active_24)
                .setContentTitle(itemForNotificationText.getTask())
                .setPriority(NotificationCompat.PRIORITY_HIGH)
                .setCategory(NotificationCompat.CATEGORY_MESSAGE)
                .setContentText(itemForNotificationText.getTime())
                .build();
        notificationManager.notify(dataBaseHelper.GetID(),notification);
    }
}

I tired using setExactAndAllowWhileIdle but still doesn't work. Start alarm:

 public void startAlarm (Calendar c){


        AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
        Intent intent = new Intent(this, AlertReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(this, item.getNotification_id(), intent,0);
        if (c.before(Calendar.getInstance())) {
            c.add(Calendar.DATE, 1);
        }

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            alarmManager.setExact(AlarmManager.RTC_WAKEUP,c.getTimeInMillis(),pendingIntent);

        }


        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            
        
     alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP,c.getTimeInMillis(),pendingIntent);
        }
    }

Manifest file:

 <application
        android:name=".App"
        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=".InsertActivity"></activity>
        <activity
            android:name=".ToDoList"
            android:parentActivityName=".MainActivity" />
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <receiver android:name=".AlertReceiver"/>
    </application>


APP Class(Notification channel is created here):

public class App extends Application {
    public static final String CHANNEL_1_ID = "Channel 1";
    @Override
    public void onCreate() {
        super.onCreate();
        CreateNotificationChannels();
    }
    private void CreateNotificationChannels () {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel1 = new NotificationChannel(
                    CHANNEL_1_ID,
                    "Channel 1",
                    NotificationManager.IMPORTANCE_HIGH
            );
            channel1.setDescription("Notifies you when an event has started.");
            channel1.enableVibration(true);
            NotificationManager manager = getSystemService(NotificationManager.class);
            manager.createNotificationChannel(channel1);

        }
    }
}
null_override
  • 467
  • 3
  • 10
  • 30

0 Answers0