2

This is my code. It works fine, if I use this in a separate Android studio project. But when I integrate the code to another project it is causing a problem. If app is open I am able to receive the notifications. But on app close, the notifications are not received.

Also I have observed one thing - I set my alarm at 1:10 (let's say) and close, and re-open the app at 1.09. I am receiving the notification at 1.10 !!.

I am not able to identify what's happening. Please tell me even if am doing silly mistake, Thank you.

public class BroadCast {

public void broadcastIntent(int selected, Context context, long userMilli) {
    Intent intent = new Intent(context.getApplicationContext(), ReminderReceiver.class);
    Bundle bundle = new Bundle();
    bundle.putInt("selected", selected);
    intent.putExtras(bundle);
    intent.setAction("android.media.action.DISPLAY_NOTIFICATION");
    intent.addCategory("android.intent.category.DEFAULT");

    Calendar calendar = Calendar.getInstance();
    long curMilli = calendar.getTimeInMillis();

    PendingIntent pendingIntent = PendingIntent.getBroadcast(context.getApplicationContext(), selected, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

    if(Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT)
        alarmManager.setExact(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + (userMilli-curMilli), pendingIntent);
    else
        alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + (userMilli-curMilli), pendingIntent);
}
}

Above code creates an intent and broadcast using alarmManager.

public class ReminderReceiver extends BroadcastReceiver {

public ReminderReceiver() {
}

@Override
public void onReceive(Context context, Intent intent) {
    Bundle bundle = intent.getExtras();
    int selected = bundle.getInt("selected");
    sendNotification(context, selected);
}

private void sendNotification(Context context, int selected) {
    String notificationContentText = "String to display";

    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(context);

    builder.setSmallIcon(R.drawable.alarm)
            .setContentTitle("Title")
            .setPriority(Notification.PRIORITY_MAX)
            .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
            .setContentText(notificationContentText)
            .setColor(Color.rgb(58,95,205));

    notificationManager.notify(selected, builder.build());
}
}

Above code building the notification

This is my AndroidManifest file

    <receiver
        android:name=".ReminderReceiver"
        android:enabled="true"
        android:exported="true">
        <intent-filter>
            <action android:name="android.media.action.DISPLAY_NOTIFICATION"/>
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </receiver>
Nikhil
  • 3,711
  • 8
  • 32
  • 43
  • Check my answer here, works when app it's closed, open and "reopen" when device boot http://stackoverflow.com/a/38474519/3626214 – Aspicas Sep 22 '16 at 08:18
  • Hey, I am trying your way, still not working. Can you tell me where you are calling the MyService in that code. – the_ganesh_hegde Sep 22 '16 at 09:55
  • you can call from activity using `startService` as `Intent serviceIntent = new Intent(this, MyService.class); startService(serviceIntent);` – Aspicas Sep 22 '16 at 10:02
  • Hey, thing is onCreate() of the Service is called, not other methods not even onDestroy(). Google says onDestroy() of service is not always called. So how are you making sure that this service is waking broadcastReceiver? – the_ganesh_hegde Sep 22 '16 at 10:31
  • Yes, i'm sure, but you need modify your AndroidManifest too, remember that. Just... Try by yourself =) – Aspicas Sep 22 '16 at 10:35
  • Thanks for the help. It was not the problem of BroadcastReceiver or the Service. I had to change my notification settings in my mobile. I use Asus, there you have to check power management option. If it is in power saving mode then the notifications are denied on app close. – the_ganesh_hegde Sep 23 '16 at 07:43
  • Ahhh cool, i'm glad to listen your solution. =) – Aspicas Sep 23 '16 at 07:44

1 Answers1

1

It was not the problem of BroadcastReceiver or the Service. I had to change my notification settings in my mobile. I use Asus, there you have to check power management option. If it is in power saving mode then the notifications are denied on app close.