0

I'm trying to do App which remind user everymorning with push notification.

I have been using AlarmManager, BroadcastReceiver and IntentService.

In code below, I test my AlarmManager with 60 seconds interval.

Problem: Everything works fine until I shut down my app, alarms not firing anymore.

Any ideas where to go next?

Manifest:

<service
    android:name=".IntentMentor"
    android:exported="false" >
</service>

<receiver android:name=".AlertReceiver"
    android:process=":remote">
</receiver>

MainActivity:

Intent intent = new Intent(getApplicationContext(), AlertReceiver.class);

final PendingIntent pIntent = PendingIntent.getBroadcast(this, 0,
    intent, PendingIntent.FLAG_UPDATE_CURRENT);

long firstMillis = System.currentTimeMillis();
AlarmManager alarm = (AlarmManager)this.getSystemService(Context.ALARM_SERVICE);
// 1s is only for testing
alarm.setInexactRepeating(AlarmManager.RTC_WAKEUP, firstMillis, 1000*60, pIntent);

Receiver:

public class AlertReceiver extends BroadcastReceiver {

    private static final String TAG = "MyReceiver";

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d(TAG, "MyReceiver on receive");
        Intent i = new Intent(context, IntentMentor.class);
        context.startService(i);
    }
}

IntentService:

public class IntentMentor extends IntentService {

    NotificationManager notificationManager;
    int notifID = 33;

    private static final String TAG = "MonitorService";

    public IntentMentor() {
        super(TAG);
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        Log.d("TAG", "Service method was fired.");
        pushNotification();
    }

    public void pushNotification(){
        NotificationCompat.Builder notificBuilder = new NotificationCompat.Builder(this);
        notificBuilder.setContentTitle("test");
        notificBuilder.setContentText("this is text");
        notificBuilder.setTicker("this is Ticker?");
        notificBuilder.setSmallIcon(R.drawable.ic_info_black_24dp);
        notificBuilder.setDefaults(NotificationCompat.DEFAULT_SOUND);

        Intent intent = new Intent(this, Card.class);

        TaskStackBuilder tStackBuilder = TaskStackBuilder.create(this);
        tStackBuilder.addParentStack(Card.class);
        tStackBuilder.addNextIntent(intent);

        PendingIntent pendingIntent = tStackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT);

        notificBuilder.setContentIntent(pendingIntent);

        notificationManager = (NotificationManager) getSystemService((Context.NOTIFICATION_SERVICE));
        notificationManager.notify(notifID, notificBuilder.build() );
    }
}
Lukas Körfer
  • 13,515
  • 7
  • 46
  • 62
Wiltson
  • 65
  • 2
  • 8
  • [Try this may be this help you](http://stackoverflow.com/questions/35121191/i-want-show-notification-at-800-am-everyday/35127736#35127736) – Andy Developer Apr 21 '17 at 08:34
  • I was trying solution behind your link. Thanks.. but I still have same problem. When app is not open, alarms will not fire. – Wiltson Apr 21 '17 at 09:11
  • Make sure you give the permission for WAKE_LOCK – Andy Developer Apr 21 '17 at 09:12
  • [Try this set of code with your.](http://stackoverflow.com/questions/35121191/i-want-show-notification-at-800-am-everyday/35124436#35124436) You can change this line "alarm.setInexactRepeating(AlarmManager.RTC_WAKEUP, firstMillis, 1000*60, pIntent);" – Andy Developer Apr 21 '17 at 09:23
  • Sorry to be a such a noob.. But what I should use in "intentLR". I don't get meaning of that. "piLR = PendingIntent.getBroadcast(context, 0, intentLR, PendingIntent.FLAG_UPDATE_CURRENT);" – Wiltson Apr 21 '17 at 09:35
  • Sorry. Of course. Thanks for patience. Too much dissapointments with my coding today :) – Wiltson Apr 21 '17 at 09:53
  • Ok. With that code "Andy Developer" linked. With my current emulator "IF ELSE" goes to: amLR.setAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME_WAKEUP, interval, pIntent); And this way notification fires only one time. At least interval is something else than 60 seconds. So far only one notification has been fired. – Wiltson Apr 21 '17 at 10:12

2 Answers2

0

Try this I update your Main Activity code. Now try this.It works fine for me.

Intent intent = new Intent(getApplicationContext(), AlertReceiver.class);

final PendingIntent pIntent = PendingIntent.getBroadcast(this, 0,
        intent, PendingIntent.FLAG_UPDATE_CURRENT);

long firstMillis = System.currentTimeMillis();
AlarmManager alarm = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);

if (android.os.Build.VERSION.SDK_INT >= 23)
{
   alarm.setAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME_WAKEUP,
            firstMillis, 1000*60, pIntent);
}
else if (android.os.Build.VERSION.SDK_INT >= 19
        && android.os.Build.VERSION.SDK_INT < 23)
{
    alarm.setInexactRepeating(AlarmManager.RTC_WAKEUP,
            firstMillis, 1000*60, pIntent);
}
else
{
    alarm.setRepeating(AlarmManager.RTC_WAKEUP,
            firstMillis, 1000*60, pIntent);
}
Andy Developer
  • 3,071
  • 1
  • 19
  • 39
  • For some reason alarm.setAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME_WAKEUP, firstMillis, 1000*60, pIntent); won't work on me. setAndAllowWhileIdle takes only 3 params? I'm I just so lost... – Wiltson Apr 21 '17 at 10:25
  • I use: alarm.setExactAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME_WAKEUP, interval, pIntent); And it fires notification only one time. – Wiltson Apr 21 '17 at 10:31
  • You can remove the firstmillis for setAndAllow something like this: alarm.setAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME_WAKEUP,1000*60, pIntent); – Andy Developer Apr 21 '17 at 10:59
  • @Wiltson try [this one](https://github.com/commonsguy/cw-omnibus/tree/master/AlarmManager/Wakeful) – Andy Developer Apr 21 '17 at 11:03
  • Yes, I realised that. I can't figure out how this setAndAllow... function works. When I start my app, first notification fires right away. Is it wrong? Is it should fire after 60 seconds? Another problem is that alarm is not repeating. Only one notification fires. – Wiltson Apr 21 '17 at 11:04
  • Ok. Thanks Andy. I try that link next. – Wiltson Apr 21 '17 at 11:08
  • Sorry to say. That solution behind link https://github.com/commonsguy/cw-omnibus/tree/master/AlarmManager/Wakeful doesn't work correctly. It fires pushnotification repeatly. But it is not working if app is closed. – Wiltson Apr 21 '17 at 12:38
0

Finally. After ten of hours testing, failing, testing, failing.. I wound answer.

Protected Apps in Huawei Phones

So as you see, problem was with my Huawei P8 phone. App needs to be in protected apps list. Now, at lest couple of solutions I tested works fine.

Thanks for everybody to help. I think Andy and others solutions works fine. But Huawei phones have this special issue.

Community
  • 1
  • 1
Wiltson
  • 65
  • 2
  • 8