Notification Android

Notification Classes :-

Manifest xml Receiver must add to Broadcast class alarm

receiver android:name=".activity.Alarm" android:enabled="true" intent-filter action android:name="android.intent.action.MAIN" /action intent-filter /receiver

Transaction

package app.dailyexpensemanager.activity; import android.app.Activity; import android.app.AlarmManager; import android.app.DatePickerDialog; import android.app.Dialog; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.app.TaskStackBuilder; import android.app.TimePickerDialog; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.content.res.Resources; import android.os.Bundle; import android.support.v4.app.NotificationCompat; import android.support.v7.widget.Toolbar; import android.text.Editable; import android.text.Html; import android.text.TextWatcher; import android.util.Log; import android.view.Menu; import android.view.MotionEvent; import android.view.View; import android.view.WindowManager; import android.widget.DatePicker; import android.widget.EditText; import android.widget.TextView; import android.widget.TimePicker; import android.widget.Toast; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.Random; import java.util.TimeZone; import app.dailyexpensemanager.R; import app.dailyexpensemanager.db.Transactiondata; /** * Created by Fame ITC on 28-Apr-16. */ public class AddReminderActivity extends MainActivity{ CustomDateTimePicker custom; Context context = this; TextView save , reset; String sql_date , sql_time , sql_heading , sql_price , sql_description ; EditText date_et, time_et , heading, price , description; long time; long added; AlarmManager alarmManager; private int year; private int month; private int day; private int hh , mm; int reminder_id_no; static final int DATE_PICKER_ID = 1111; static final int TIME_PICKER_ID = 2222; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_add_reminder); Toolbar toolbarTop = (Toolbar) findViewById(R.id.toolbar); TextView mTitle = (TextView) toolbarTop.findViewById(R.id.title); mTitle.setText("Add Reminder"); date_et = (EditText) findViewById(R.id.date); time_et = (EditText) findViewById(R.id.time); heading = (EditText) findViewById(R.id.heading); price = (EditText) findViewById(R.id.price); description = (EditText) findViewById(R.id.description); save = (TextView) findViewById(R.id.save); reset = (TextView) findViewById(R.id.reset); date_et.addTextChangedListener(new GenericTextWatcher(date_et)); time_et.addTextChangedListener(new GenericTextWatcher(time_et)); heading.addTextChangedListener(new GenericTextWatcher(heading)); price.addTextChangedListener(new GenericTextWatcher(price)); description.addTextChangedListener(new GenericTextWatcher(description)); final Calendar c = Calendar.getInstance(); year = c.get(Calendar.YEAR); month = c.get(Calendar.MONTH); day = c.get(Calendar.DAY_OF_MONTH); Date date= new Date(); hh = date.getHours(); mm = date.getMinutes(); date_et.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { showDialog(DATE_PICKER_ID); return false; } }); time_et.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { showDialog(TIME_PICKER_ID); return false; } }); reset.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { price.setText(""); heading.setText(""); date_et.setText(""); time_et.setText(""); description.setText(""); heading.clearFocus(); price.clearFocus(); description.clearFocus(); // heading.requestFocus(); } }); save.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if(!validateDate()){ return; } if(!validateTime()){ return; } if(!validateHeading()){ return; } if(!validatePrice()){ return; } if(!validateDescription()){ return; } sql_date = date_et.getText().toString(); sql_time = time_et.getText().toString(); sql_heading = heading.getText().toString(); sql_price = price.getText().toString(); sql_description = description.getText().toString(); StringBuilder sb = new StringBuilder(); String sql_date_time = sb.append(sql_date).append(" ").append(sql_time).toString(); SimpleDateFormat simpleDateFormate = new SimpleDateFormat("dd-MM-yyyy HH:mm"); // simpleDateFormate.setTimeZone(TimeZone.getTimeZone("GMT")); // String inputString = "2016-6-5 05:30"; Date date = null; try { date = simpleDateFormate.parse(sql_date_time); } catch (ParseException e) { e.printStackTrace(); } System.out.println("in millSeconds:............ " + date.getTime()); time = date.getTime(); long currentTime = System.currentTimeMillis(); added = time - currentTime; // added = Math.abs(currentTime - time);// Negative number convert in positive Log.d(" Time ... ", "onClick: " + added + " " + time + " " + currentTime); /* Calendar calendar = Calendar.getInstance(); calendar.clear(); calendar.setTimeInMillis(date.getTime()); String newtime = simpleDateFormate.format(calendar.getTime()); System.out.println("in Time ..........: " + newtime);*/ Random random = new Random(); reminder_id_no = random.nextInt(9999 - 1000) + 1000; Transactiondata newEntry = new Transactiondata(context); newEntry.open(); newEntry.reminderPutInformation(reminder_id_no ,sql_date_time, sql_heading, sql_price, sql_description); newEntry.close(); Toast.makeText(AddReminderActivity.this, "Reminder Added Successfully.", Toast.LENGTH_LONG).show(); call(); date_et.setText(""); time_et.setText(""); heading.setText(""); price.setText(""); description.setText(""); heading.clearFocus(); price.clearFocus(); description.clearFocus(); } }); } public void call(){ Intent intent = new Intent(this, Alarm.class); intent.putExtra("title" , sql_heading ); intent.putExtra("price" , sql_price ); intent.putExtra("message" , sql_description ); intent.putExtra("reminder_id" , reminder_id_no ); // Request code must be different to get every alarm notification ... random number .... PendingIntent pendingIntent = PendingIntent.getBroadcast( this.getApplicationContext(), reminder_id_no , intent, PendingIntent.FLAG_UPDATE_CURRENT); alarmManager = (AlarmManager)this.getSystemService(Context.ALARM_SERVICE); alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + added, pendingIntent); // Toast.makeText(this, "" + added + " miliseconds",Toast.LENGTH_LONG).show(); } @Override protected Dialog onCreateDialog(int id) { switch (id) { case DATE_PICKER_ID: // open datepicker dialog. // set date picker for current date // add pickerListener listner to date picker return new DatePickerDialog(this, pickerListener, year, month,day); case TIME_PICKER_ID: // open datepicker dialog. // set date picker for current date // add pickerListener listner to date picker return new TimePickerDialog(this, picker, hh, mm,true); } return null; } private DatePickerDialog.OnDateSetListener pickerListener = new DatePickerDialog.OnDateSetListener() { // when dialog box is closed, below method will be called. @Override public void onDateSet(DatePicker view, int selectedYear, int selectedMonth, int selectedDay) { year = selectedYear; month = selectedMonth; day = selectedDay; Calendar c = Calendar.getInstance(); // Show selected date if greater then current time /*if(c.get(Calendar.YEAR) <= year && c.get(Calendar.MONTH) <= month && c.get(Calendar.DAY_OF_MONTH) <= day){ date_et.setText(new StringBuilder().append(day) .append("-").append(month + 1).append("-").append(year) .append(" ")); }else{ }*/ date_et.setText(new StringBuilder().append(day) .append("-").append(month + 1).append("-").append(year) .append(" ")); } }; private TimePickerDialog.OnTimeSetListener picker = new TimePickerDialog.OnTimeSetListener() { @Override public void onTimeSet(TimePicker view, int hourOfDay, int minute) { hh = hourOfDay; mm = minute; time_et.setText(new StringBuilder().append(hh) .append(":").append(mm).append(" ")); } }; private boolean validateDate() { if (date_et.getText().toString().trim().isEmpty()) { date_et.setError(Html.fromHtml("Enter the date")); requestFocus(date_et); return false; } else { date_et.setError(null); } return true; } private boolean validateTime() { if (time_et.getText().toString().trim().isEmpty()) { time_et.setError(Html.fromHtml("Enter the time")); requestFocus(time_et); return false; } else { time_et.setError(null); } return true; } private boolean validateHeading() { if (heading.getText().toString().trim().isEmpty()) { heading.setError(Html.fromHtml("Enter the heading")); requestFocus(heading); return false; } else { heading.setError(null); } return true; } private boolean validatePrice() { if (price.getText().toString().trim().isEmpty()) { price.setError(Html.fromHtml("Enter the price")); requestFocus(price); return false; } else { price.setError(null); } return true; } private boolean validateDescription() { if (description.getText().toString().trim().isEmpty()) { description.setError(Html.fromHtml("Enter the description")); requestFocus(description); return false; } else { description.setError(null); } return true; } private void requestFocus(View view) { if (view.requestFocus()) { getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE); } } private class GenericTextWatcher implements TextWatcher { private View view; private GenericTextWatcher(View view) { this.view = view; } public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {} public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {} public void afterTextChanged(Editable editable) { String text = editable.toString(); switch(view.getId()){ case R.id.date: if(text.length()>0) date_et.setError(null); break; case R.id.time: if(text.length()>0) time_et.setError(null); break; case R.id.heading: if(text.length()>0) heading.setError(null); break; case R.id.price: if(text.length()>0) price.setError(null); break; case R.id.description: if(text.length()>0) description.setError(null); break; } } } }

Transaction

package app.dailyexpensemanager.activity; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.widget.Toast; import java.util.Random; import app.dailyexpensemanager.R; /** * Created by Fame ITC on 02-May-16. */ public class Alarm extends BroadcastReceiver { /*Intent intent2 ;*/ String title , message , price ; int reminder_id , activity_code ; @Override public void onReceive(Context context, Intent intent) { title = intent.getStringExtra("title"); message = intent.getStringExtra("message"); price = intent.getStringExtra("price"); reminder_id = intent.getIntExtra("reminder_id", 0); Intent intent2 = new Intent(context, DailyExpensesActivity.class); intent2.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK); PendingIntent pIntent = PendingIntent.getActivity(context, reminder_id, intent2, PendingIntent.FLAG_ONE_SHOT); // Build notification // Actions are just fake Notification noti = new Notification.Builder(context) .setContentTitle(title) .setContentText(message) .setContentIntent(pIntent) .setSmallIcon(R.drawable.ic_menu_manage) /*.addAction(R.drawable.ic_menu_camera, "Call", pIntent) .addAction(R.drawable.ic_menu_gallery, "And more", pIntent)*/.build(); noti.defaults |= Notification.DEFAULT_SOUND; noti.defaults |= Notification.DEFAULT_VIBRATE; NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE); // hide the notification after its selected noti.flags |= Notification.FLAG_AUTO_CANCEL; notificationManager.notify(reminder_id, noti); //Toast.makeText(context, "Alarm worked.", Toast.LENGTH_LONG).show(); } }

Remove added Notification

public static void call(){ Intent intent = new Intent(mContext , Alarm.class); intent.putExtra("reminder_id", reminder_id_code); intent.putExtra("activity_code", 0); // Request code must be different to get every alarm notification ... random number .... PendingIntent pendingIntent = PendingIntent.getBroadcast( mContext.getApplicationContext() , reminder_id_code , intent, PendingIntent.FLAG_UPDATE_CURRENT); alarmManager = (AlarmManager)mContext.getSystemService(Context.ALARM_SERVICE); alarmManager.cancel(pendingIntent); Log.d(" Cancel .....", "alarm ...... .......... ......... ....................: "); }