I have to show a notification every day at a specific time (for example, 7.00 in the morning) to users from my application, even when my application does not work. (closed)
Since the content of the notification will be user-specific, I cannot use (GCM) push
I went through many tutorials and answers, but I can not display a notification when the application is closed (does not work).
Edit: using the code below, I can create a notification while my application is running, if I close notifications about my applications
Here is my main activity
public class MainActivity extends AppCompatActivity { Button setAlarm; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); setAlarm = (Button) findViewById(R.id.set_alarm); setAlarm.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { NotificationEventReceiver.setupAlarm(getApplicationContext()); } }); } @Override protected void onNewIntent(Intent intent) { super.onNewIntent(intent); setIntent(intent); } }
here, when I click the "Alarm" button, my NotificationEventReciver calls
public class NotificationEventReceiver extends WakefulBroadcastReceiver { private static final String ACTION_START_NOTIFICATION_SERVICE = "ACTION_START_NOTIFICATION_SERVICE"; private static final String ACTION_DELETE_NOTIFICATION = "ACTION_DELETE_NOTIFICATION"; public static void setupAlarm(Context context) { AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); Intent intent = new Intent(context, NotificationEventReceiver.class); PendingIntent alarmIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, getTriggerAt(new Date()), 2 * 60 * 1000, alarmIntent); } private static long getTriggerAt(Date now) { Calendar calendar = Calendar.getInstance(); calendar.setTimeInMillis(System.currentTimeMillis()); calendar.set(Calendar.HOUR_OF_DAY, 17); calendar.set(Calendar.MINUTE, 10); System.err.println(now.getTime()+"----->"+ calendar.getTimeInMillis()); return calendar.getTimeInMillis(); } public static void cancelAlarm(Context context) { AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); PendingIntent alarmIntent = getStartPendingIntent(context); alarmManager.cancel(alarmIntent); } private static PendingIntent getStartPendingIntent(Context context) { Intent intent = new Intent(context, NotificationEventReceiver.class); intent.setAction(ACTION_START_NOTIFICATION_SERVICE); return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); } public static PendingIntent getDeleteIntent(Context context) { Intent intent = new Intent(context, NotificationEventReceiver.class); intent.setAction(ACTION_DELETE_NOTIFICATION); return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); } @Override public void onReceive(Context context, Intent intent) { Intent serviceIntent = NotificationIntentService.createIntentStartNotificationService(context); if (serviceIntent != null) { System.err.println("onReceive inside not null");
My class of service, which will start when you log out.
public class NotificationIntentService extends IntentService { private static final int NOTIFICATION_ID = 1; private static final String ACTION_START = "ACTION_START"; private static final String ACTION_DELETE = "ACTION_DELETE"; public NotificationIntentService() { super(NotificationIntentService.class.getSimpleName()); } public static Intent createIntentStartNotificationService(Context context) { Intent intent = new Intent(context, NotificationIntentService.class); intent.setAction(ACTION_START); return intent; } @Override protected void onHandleIntent(Intent intent) { System.err.println("onHandleIntent, started handling a notification event"); try { String action = intent.getAction(); if (ACTION_START.equals(action)) { System.err.println("enters the loop ACTION_START"); processStartNotification(); } } finally { WakefulBroadcastReceiver.completeWakefulIntent(intent); } } private void processStartNotification() {