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() );
}
}