0

I created an application with AlarmManager and Notification but it works only on an emulator. When I run the app on my physical device the notifications are not shown despite the fact that in app info there is information about the notifications.

EDIT: I've found the solution in this thread

Remi
  • 99
  • 1
  • 3
  • 13
  • Please include a snippet of your notification code. – dazza5000 Nov 13 '20 at 02:29
  • Have you inserted proper logs to get notified about how the code is behaving? – Nadeem Shaikh Nov 13 '20 at 07:36
  • @dazza5000 The code is almost the same as it is here: [my previous post](https://stackoverflow.com/questions/64805288/alarmmanager-with-notification-in-android-does-not-show-any-notifications/) – Remi Nov 13 '20 at 09:31
  • @NadeemShaikh I inserted logs into the class which extends BroadcastReceiver and it looks like the application does not execute the overriden onReceive method which it should execute in order to show notifications to the user – Remi Nov 13 '20 at 09:34
  • @Remi any specific event responsible to trigger the `onReceive` command? – Nadeem Shaikh Nov 13 '20 at 10:45
  • @Remi In order to reach `onReceive` method you must have to invoke `sendBroadcast(Intent intent)`. Make sure where you have this function written gets invoked. – Nadeem Shaikh Nov 13 '20 at 10:51
  • @NadeemShaikh `AlarmManager` should trigger the `onReceive` method every minute since `AlarmManager`'s delay is set to 1 minute (1000L * 60L). User clicks a button and the `alarmManager.setRepeating(...)` is executed – Remi Nov 13 '20 at 10:51
  • @NadeemShaikh I will try to implement it – Remi Nov 13 '20 at 10:52
  • @Remi check out the answer that I have posted! – Nadeem Shaikh Nov 13 '20 at 11:01

1 Answers1

0

As your Alarm completes 60 secs or whenever you need to trigger the notification, invoke the following function:-

triggerNotification(){
Intent intent = new Intent(getContext(),DailyNotificationReceiver.class);
sendBroadcast(intent);
}
//after every min lapsed invoke this function

This is in java, convert it into kotlin. This sendBroadcast function will internally call your onReceive method

Nadeem Shaikh
  • 1,160
  • 9
  • 17
  • It worked! It showed a notification only once. It seems like this code does not work: `alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, triggerTime, repeatInterval, notifyPendingIntent) ` – Remi Nov 13 '20 at 13:28
  • done, thank you! I'll investigate further the problem – Remi Nov 13 '20 at 14:19
  • 1
    I've found the solution here: https://stackoverflow.com/questions/34729966/alarmmanager-not-working-in-several-devices/42468905 – Remi Nov 13 '20 at 15:04
  • @Remi I personally also have experienced such problems in OnePLus devices as well. Some app dont show notifications until opened once. To prevent such problem you need to exempsuch apps in battery optimization. In Oneplus phones there is a setting called "Dont optimize" in Battery settings. – Nadeem Shaikh Nov 13 '20 at 16:00
  • I've found the guide for my phone here: https://dontkillmyapp.com/xiaomi – Remi Nov 14 '20 at 08:08