Receiver class
public class NotificationPublisher extends BroadcastReceiver {public static String NOTIFICATION_ID = "notification-id";
public static String NOTIFICATION = "notification";
@Override
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);
}
}
Main Class
private void scheduleNotification(Notification notification, Calendar cal) { int newid = NotificationID.getID(); Intent notificationIntent = new Intent(this, NotificationPublisher.class); //making the intent unique notificationIntent.setAction("actionstring" + System.currentTimeMillis()); //Sending variables to the Broadcast Reciever notificationIntent.putExtra(NotificationPublisher.NOTIFICATION_ID, newid); notificationIntent.putExtra(NotificationPublisher.NOTIFICATION, notification); PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT); AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE); alarmManager.set(AlarmManager.RTC, cal.getTimeInMillis(), pendingIntent); } @TargetApi(Build.VERSION_CODES.JELLY_BEAN) //Create and returns a notification object private Notification getNotification(String content) { Notification.Builder builder = new Notification.Builder(this); builder.setContentTitle("Wedding App Reminder"); builder.setContentText(content); builder.setSmallIcon(R.drawable.alarm); return builder.build(); } private void setReminder(){ setReminder.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String reminderDate = editTextDate.getText().toString(); String reminderTime = editTextTime.getText().toString(); String notes = editTextNotes.getText().toString(); String[] daySplits = reminderDate.split(" "); String[] timeSplits = reminderTime.split(":"); day = Integer.parseInt(daySplits[0]); for (int i = 0; i < 12; i++) { if (daySplits[1].equals(MONTHS[i])) { month = i + 1; } } year = Integer.parseInt(daySplits[2]); hour = Integer.parseInt(timeSplits[0]); String[] splits = timeSplits[1].split(" "); minute = Integer.parseInt(splits[0]); ampm = splits[1]; second = 00; Calendar c = Calendar.getInstance(); c.set(year, (month - 1), day); c.set(Calendar.HOUR_OF_DAY, hour); c.set(Calendar.MINUTE, minute); c.set(Calendar.SECOND, second); if (ampm.equals("AM")) c.set(Calendar.AM_PM, Calendar.AM); else if (ampm.equals("PM")) c.set(Calendar.AM_PM, Calendar.PM); // Ask our service to set an alarm for that date, this activity talks to the client that talks to the service scheduleNotification(getNotification(name + " task was scheduled to " + date + " " + mny), c); // Notify the user what they just did showToast("Notification set for: " + day + "/" + month + "/" + year); } }); } }NotificatioID class
public class NotificationID { private final static AtomicInteger c = new AtomicInteger(0); public static int getID() { return c.incrementAndGet(); } }AndroidManifest.XML
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> <receiver android:name=".NotificationPublisher_CA" android:enabled="true" android:label="NotificationPublisher"> android:exported="true" <!--<intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> </intent-filter>--> </receiver>
No comments:
Post a Comment