0

I am trying to build an offline reminder android application using SQLite database. For notification, I use the alaram manager to call broadcast receiver, but unfortunately, receiver not triggered, So I am not able to get reminder notification in higher version 6.0 and above

My manifest include:

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.VIBRATE" />

And

enter code here
   <receiver
        android:name=".Util.BroadcastManager"
        android:exported="false">
        <intent-filter>
            <action android:name="android.intent.action.NOTIFY" />
            <action android:name="android.intent.action.BOOT_COMPLETED" />
            <action android:name="android.intent.action.INPUT_METHOD_CHANGED" />
            <!--
            <action android:name="ALARM" />
            <action android:name = "android.hardware.usb.action.USB_STATE" />
            <category android:name="android.intent.category.APP_CALENDAR" />
            -->
        </intent-filter>
    </receiver>

Alarm Manager Called as:

           if (alarm) {
        Intent itAlarm = new Intent("ALARM");
        PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, itAlarm, 0);
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        calendar.add(Calendar.SECOND, 3);
        AlarmManager alarme = (AlarmManager) getSystemService(ALARM_SERVICE);
        int interval = 60000;
        alarme.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), interval, pendingIntent);
    }else {
        Intent alarmIntent = new Intent(MainActivity.this, BroadcastManager.class);
        alarmIntent.setAction(Intent.ACTION_MAIN);
        alarmIntent.addCategory(Intent.CATEGORY_LAUNCHER);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, alarmIntent, 0);

        AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        int interval = 60000;

        manager.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), interval, pendingIntent);
    }

    br = new BroadcastManager();
    IntentFilter filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
    filter.addAction(Intent.ACTION_AIRPLANE_MODE_CHANGED);
    this.registerReceiver(br, filter);
Namrata
  • 9
  • 1
  • 3
  • I'm not sure what your `alarm` variable indicates, but you should be using an explicit `Intent` in both cases; i.e., `new Intent(MainActivity.this, BroadcastManager.class)` in the first case, too, instead of `new Intent("ALARM")`. – Mike M. Nov 29 '19 at 02:01
  • this line didn't work, broadcast manager not called although. – Namrata Dec 06 '19 at 06:03
  • I don't know what that means. In any case, nothing changed in Android 6 specifically, with regard to alarms or Receivers. Android 8 introduced restrictions on implicit broadcasts to manifest-registered Receivers but you should've been using explicit broadcasts all along, anyway. The major change in 6 was runtime permissions, so if you have something in your Receiver that requires a dangerous permission, that's possibly the cause of the failure. You'll need to [edit] your question to provide a [mcve] that demonstrates the issue. – Mike M. Dec 06 '19 at 19:32

1 Answers1

0

Try using a notification library to make a custom notification that works on every API.

Alerter Notification

This is a custom library

implementation "com.tapadoo.android:alerter:5.0.0"

Here is the link

I'm using this in my current project and it's working perfectly.

AllwiN
  • 693
  • 2
  • 12
  • 26
  • Alarm Manager not working properly to trigger the notification at particular time – Namrata Nov 28 '19 at 06:22
  • Alarm manager not trigger broadcast receiver when application not running, I gave permission of autorun – Namrata Nov 28 '19 at 06:43
  • I think you need to run the alarm manager in the background thread that's why the alarm manager not invoking while the app is in the background ;)[https://stackoverflow.com/questions/15472383/how-can-i-run-code-on-a-background-thread-on-android] – AllwiN Nov 28 '19 at 06:51
  • i have tried using thread and handler but still not getting expected result – Namrata Dec 06 '19 at 06:04