-
-
Save wtrocki/e8ef29af3878ee4583ba091c61b54c49 to your computer and use it in GitHub Desktop.
Quick example of how to schedule a notification in the future using AlarmManager
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | |
package="com.cards.notification"> | |
<uses-sdk | |
android:minSdkVersion="17" | |
android:targetSdkVersion="17" /> | |
<application | |
android:allowBackup="true" | |
android:icon="@drawable/ic_launcher" | |
android:label="@string/app_name" | |
android:theme="@style/AppTheme" > | |
<activity | |
android:name="com.cards.notification.MainActivity" | |
android:label="@string/app_name" > | |
<intent-filter> | |
<action android:name="android.intent.action.MAIN" /> | |
<category android:name="android.intent.category.LAUNCHER" /> | |
</intent-filter> | |
</activity> | |
<receiver android:name=".NotificationPublisher" /> | |
</application> | |
</manifest> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<menu xmlns:android="http://schemas.android.com/apk/res/android"> | |
<item android:id="@+id/action_5" | |
android:title="5 seconds" | |
android:showAsAction="never" /> | |
<item android:id="@+id/action_10" | |
android:title="10 seconds" | |
android:showAsAction="never" /> | |
<item android:id="@+id/action_30" | |
android:title="30 seconds" | |
android:showAsAction="never" /> | |
</menu> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.cards.notification; | |
import android.app.AlarmManager; | |
import android.app.Notification; | |
import android.app.PendingIntent; | |
import android.content.Context; | |
import android.content.Intent; | |
import android.os.Bundle; | |
import android.app.Activity; | |
import android.os.SystemClock; | |
import android.view.Menu; | |
import android.view.MenuItem; | |
public class MainActivity extends Activity { | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
} | |
@Override | |
public boolean onCreateOptionsMenu(Menu menu) { | |
// Inflate the menu; this adds items to the action bar if it is present. | |
getMenuInflater().inflate(R.menu.main, menu); | |
return true; | |
} | |
@Override | |
public boolean onOptionsItemSelected(MenuItem item) { | |
switch (item.getItemId()) { | |
case R.id.action_5: | |
scheduleNotification(getNotification("5 second delay"), 5000); | |
return true; | |
case R.id.action_10: | |
scheduleNotification(getNotification("10 second delay"), 10000); | |
return true; | |
case R.id.action_30: | |
scheduleNotification(getNotification("30 second delay"), 30000); | |
return true; | |
default: | |
return super.onOptionsItemSelected(item); | |
} | |
} | |
private void scheduleNotification(Notification notification, int delay) { | |
Intent notificationIntent = new Intent(this, NotificationPublisher.class); | |
notificationIntent.putExtra(NotificationPublisher.NOTIFICATION_ID, 1); | |
notificationIntent.putExtra(NotificationPublisher.NOTIFICATION, notification); | |
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT); | |
long futureInMillis = SystemClock.elapsedRealtime() + delay; | |
AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE); | |
alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, futureInMillis, pendingIntent); | |
} | |
private Notification getNotification(String content) { | |
Notification.Builder builder = new Notification.Builder(this); | |
builder.setContentTitle("Scheduled Notification"); | |
builder.setContentText(content); | |
builder.setSmallIcon(R.drawable.ic_launcher); | |
return builder.build(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.cards.notification; | |
import android.app.Notification; | |
import android.app.NotificationManager; | |
import android.content.BroadcastReceiver; | |
import android.content.Context; | |
import android.content.Intent; | |
public class NotificationPublisher extends BroadcastReceiver { | |
public static String NOTIFICATION_ID = "notification-id"; | |
public static String NOTIFICATION = "notification"; | |
public void onReceive(Context context, Intent intent) { | |
NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE); | |
Notification notification = intent.getParcelableExtra(NOTIFICATION); | |
int id = intent.getIntExtra(NOTIFICATION_ID, 0); | |
notificationManager.notify(id, notification); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment