My notification is sent only when I start the app on some devices

50
April 06, 2021, at 11:10 AM

I have developed a notification that is sent every time a BroadcastReceiver is woken up by an AlarmMaganer. When the receiver shows the notification it also takes care of activating the next notification. But on some devices the receiver only wakes up when I start the app so it doesn't show the notification at the set time. My onReceive: (the method activeNotification sets AlarmManager)

@Override
public void onReceive(Context context, Intent intent) {
    Intent i = new Intent(context, StartActivity.class);
    PendingIntent pi = PendingIntent.getActivity(context, 0, i, 0);
    NotificationCompat.Builder builder = new NotificationCompat.Builder(context, "notifyValutation")
            .setSmallIcon(R.drawable.ic_notification)
            .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher))
            .setContentTitle("Ehi hai inserito la tua autovalutazione?")
            .setContentText("Che ne pensi di farlo ora? Questo รจ il tuo momento!")
            .setPriority(NotificationCompat.PRIORITY_DEFAULT)
            .setAutoCancel(true)
            .setContentIntent(pi);
    NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
    notificationManager.notify(200, builder.build());
    activeNotification(context);
}

How do I always see the notification, even when the app is not running? Thanks.

UPDATE: (activeNotification() & createNotificationChannel())

private static void createNotificationChannel(Context context) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        CharSequence name = "ValutationReminderChannel";
        String description = "Channel for Valutation Reminder";
        int importance = NotificationManager.IMPORTANCE_DEFAULT;
        NotificationChannel channel = new NotificationChannel("notifyValutation", name, importance);
        channel.setDescription(description);
        NotificationManager notificationManager = context.getSystemService(NotificationManager.class);
        notificationManager.createNotificationChannel(channel);
    }
}
public static void activeNotification(Context context) {
    createNotificationChannel(context);
    Intent intent = new Intent(context, ReminderBroadcast.class);
    alarmManager = (AlarmManager) context.getSystemService(ALARM_SERVICE);
    pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
    Calendar calendar = Calendar.getInstance();
    int hour = calendar.get(Calendar.HOUR_OF_DAY);
    
    if (hour < 9) {
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 9, 0, 0);
        alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
        return;
    }
    if (hour < 12) {
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 12, 0, 0);
        alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
        return;
    }
}
READ ALSO
Calculate orientation angle of minutiae point [closed]

Calculate orientation angle of minutiae point [closed]

Want to improve this question? Update the question so it focuses on one problem only by editing this post

29
Add range in PHP scheduling system slots

Add range in PHP scheduling system slots

Helo! I am a beginner in programming and if anyone can help me change a code snippet I would appreciate itI have a scheduling system with time slots, where the slots are generated from variables with the start date until the end date

55
how to send second result in expressjs

how to send second result in expressjs

i want to make Whatsapp bot and integrated to my websitethe system send the qr and users scan it through my website and after users scan and connected i want to send a message to the users it had been connected in the same page

53