Mert Serimer
Mert Serimer

Reputation: 1245

android - how to push notification (reminder) on a specific time?

I checked almost all questions in stack overflow and also in google documentation and still couldn't understand what to use exactly for my cases.

What i want is, user will select reminder date in the app and in that time app will send a notification even app is closed or phone restarted between remind set time and remind time.

So what do i need to use? Which classes do i need? Broadcast Receiver, AlarmManager these two is enough or what? a sample of code 20-30 lines would be nice =)

Upvotes: 1

Views: 2218

Answers (3)

Abdul Qayyum
Abdul Qayyum

Reputation: 23

  1. Use AlertManager.set to send notification on particular time.
  2. For re-setting Alarm Notification, use

    <receiver android:name=".AfterBootReceiver" android:label="AfterBootReceiver">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED"></action>
    </intent-filter>
    

and

@Override public void onReceive(Context context,Intent intent){

    ...
    }

to reset AlarmManager.set after device re-booted.

Upvotes: 0

KISHORE_ZE
KISHORE_ZE

Reputation: 1466

Check Android Developers website for calender intents so you can use them to determine the time etc. On your broadcast receiver pass the details or timing to the broadcast receiver. Once you do that use the alarm manager etc to check if the time is perfect and simply send a push notification when the time is correct.

I say this assuming that you know already about the calendar, alarm manager, and broadcast receivers intents. If you don't I recommend googling the following with Android Developers appended to it.

Upvotes: 1

BoredAndroidDeveloper
BoredAndroidDeveloper

Reputation: 1359

You will need both a few things.

Setup: You need to set your app to listen for the phone starting by registering for this intent in your manifest.

Basically you when your user selects the time, you will need to:

  1. Save that time in a file/db/shared prefs.
  2. Set the alarm manager to wake up your app at the right time
  3. When the alarm manager sends you a broadcast, start a service to do the necessary work (broadcast receivers have to complete their work in a short amount of time, so it's better to start a service)
  4. In your service, remove the time from your file/db/shared prefs

If your app ever gets the broadcast that the phone was rebooted, then all of your alarms are lost. You need to reset them using the times you saved in the file/db/shared prefs.

Upvotes: 1

Related Questions