Created
March 14, 2013 14:03
-
-
Save felipecaldas/5161569 to your computer and use it in GitHub Desktop.
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
----- Service class | |
public class NotifyService extends Service { | |
private final int NOTIFICATION_ID = 1; | |
@Override | |
public void onCreate() { | |
NotificationManager mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); | |
//R.drawable.ic_launcher is the icon for this notification | |
Notification notification = new Notification(R.drawable.ic_launcher, "This is the message that first appread in the top bar", System.currentTimeMillis()); | |
Intent myIntent = new Intent(this , MainActivity.class); | |
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, myIntent, 0); | |
notification.setLatestEventInfo(this, "This is the label of the notification", "Text to show in the notification", contentIntent); | |
//Adding this Flag so that when user clicks on the notification, it closes (cancels) | |
notification.flags |= Notification.FLAG_AUTO_CANCEL; | |
mNM.notify(NOTIFICATION_ID, notification); | |
} | |
} | |
----- Creating the alert | |
Calendar calendar = Calendar.getInstance(); | |
calendar.set(Calendar.SECOND, 0); | |
calendar.set(Calendar.MINUTE, 0); | |
calendar.set(Calendar.HOUR, 0); | |
calendar.set(Calendar.AM_PM, Calendar.AM); | |
calendar.add(Calendar.DAY_OF_MONTH, 1); | |
Intent myIntent = new Intent(this , NotifyService.class); | |
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE); | |
PendingIntent pendingIntent = PendingIntent.getService(this, 0, myIntent, 0); | |
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent); | |
---- AndroidManifest.xml | |
<service | |
android:name=".NotifyService" | |
android:icon="@drawable/ic_launcher" | |
android:label="@string/app_name" > | |
</service> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment