• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Create Notification

Hello
how can i create notification when date current equals date in the base data (mysql)
******************************************************************************************
package com.example.lenovo.pfe.Classe;

import android.app.DatePickerDialog;
import android.app.NotificationManager;
import android.app.ProgressDialog;
import android.app.TimePickerDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.graphics.Typeface;
import android.graphics.drawable.ColorDrawable;
import android.support.design.widget.Snackbar;
import android.support.v4.app.NotificationCompat;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.InputType;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.example.lenovo.pfe.R;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

public class partietache extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener {
Typeface typenomtache;
TextView txttache , txtheure , txtdate , txtstart , txtmenu , txtnot , txtdes;
EditText ed1 , ed2 , ed3 , ed4 , ed5;
Switch not;
Button btn;
RequestQueue requestQueue;
ProgressDialog progressDialog;
String insert = "http://192.168.100.41/projet/insert.php";
private static final String TAG = "partietache";
private DatePickerDialog.OnDateSetListener datelistener;
@override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_partietache);
txttache= (TextView) findViewById(R.id.nommenutach);
txtheure =(TextView) findViewById(R.id.heuretache);
txtdate = (TextView)findViewById(R.id.datetache);
txtstart = (TextView)findViewById(R.id.starttimetache);
txtmenu = (TextView)findViewById(R.id.nommenu);
txtdes = (TextView)findViewById(R.id.description);
txtnot = (TextView)findViewById(R.id.notification);
not = (Switch) findViewById(R.id.notifi);
ed1 = (EditText)findViewById(R.id.nomtache);
ed2 = (EditText)findViewById(R.id.edidatetache);
ed3 = (EditText)findViewById(R.id.editstarttache);
ed4 = (EditText)findViewById(R.id.editheuretache);
ed5 = (EditText)findViewById(R.id.editdes);
btn = (Button) findViewById(R.id.btnaddtask);
requestQueue = Volley.newRequestQueue(partietache.this);
progressDialog = new ProgressDialog (partietache.this);
ed2.setInputType(InputType.TYPE_NULL);
ed3.setInputType(InputType.TYPE_NULL);
typenomtache = Typeface.createFromAsset(getAssets(),"fonts/Montserrat-Regular.otf");
btn.setTypeface(typenomtache);
txttache.setTypeface(typenomtache);
txtheure.setTypeface(typenomtache);
txtdate.setTypeface(typenomtache);
txtstart.setTypeface(typenomtache);
txtmenu.setTypeface(typenomtache);
txtdes.setTypeface(typenomtache);
txtnot.setTypeface(typenomtache);
ed2.setOnClickListener(new View.OnClickListener() {
@override
public void onClick(View view) {
Calendar cal = Calendar.getInstance();
int year = cal.get(Calendar.YEAR );
int month = cal.get(Calendar.MONTH);
int day = cal.get(Calendar.DAY_OF_MONTH);
cal.set(Calendar.MONTH , month);
cal.set(Calendar.DAY_OF_MONTH , day);
DatePickerDialog dialog = new DatePickerDialog(partietache.this, android.R.style.Theme_Holo_Light_Dialog_MinWidth,
datelistener,
year, month, day);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
dialog.show();
}
});
datelistener = new DatePickerDialog.OnDateSetListener() {
@override
public void onDateSet(DatePicker datePicker, int year, int month, int day) {
month = month+1;
Log.d(TAG, "onDateSet: yyy-dd-mm: " + year + "-" + month + "-" + day);

String date = year + "-" + month + "-" + day ;
ed2.setText(date);
}
};
ed3.setOnClickListener(new View.OnClickListener() {
@override
public void onClick(View view) {
Calendar cal = Calendar.getInstance();
int hour = cal.get(Calendar.HOUR);
int minute = cal.get(Calendar.MINUTE);
TimePickerDialog time;
time = new TimePickerDialog(partietache.this, new TimePickerDialog.OnTimeSetListener() {
@override
public void onTimeSet(TimePicker timePicker, int i, int i1) {
ed3.setText(i + ":" + i1);

}
} , hour , minute , false);
time.show();
}
});
not.setOnCheckedChangeListener(this);


btn.setOnClickListener(new View.OnClickListener() {
@override
public void onClick(final View view) {
StringRequest request = new StringRequest(Request.Method.POST, insert, new Response.Listener<String>() {
@override
public void onResponse(String response) {

}
}, new Response.ErrorListener() {
@override
public void onErrorResponse(VolleyError error) {
progressDialog.dismiss();
Toast.makeText(partietache.this,error.toString(),Toast.LENGTH_LONG).show();

}
}){

@override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> para = new HashMap<String , String>();
para.put("taskname", ed1.getText().toString().trim());
para.put("totalwork",ed4.getText().toString().trim());
para.put("datetask",ed2.getText().toString().trim());
para.put("starttime",ed3.getText().toString().trim());
para.put("description",ed5.getText().toString().trim());
return para;

}
};
RequestQueue requestQueue = Volley.newRequestQueue(partietache.this);
requestQueue.add(request);
Snackbar snackbar = Snackbar.make(view,"Add with Succesful" , Snackbar.LENGTH_LONG);
snackbar.show();
new Timer().schedule(new TimerTask(){
public void run() {
partietache.this.runOnUiThread(new Runnable() {
@override
public void run() {
finish();
}
});
}

} , 4000);
}
});

ImageButton imgfermer = (ImageButton)findViewById(R.id.fermer);
imgfermer.setOnClickListener(new View.OnClickListener() {
@override
public void onClick(View view) {
AlertDialog.Builder alert = new AlertDialog.Builder(partietache.this);
alert.setTitle("Fermer ?");
alert.setMessage("voulez-vous fermer cette fenêtre ?");
alert.setPositiveButton("Oui", new DialogInterface.OnClickListener() {
@override
public void onClick(DialogInterface dialogInterface, int i) {
finish();
}
});
alert.setNegativeButton("Non", new DialogInterface.OnClickListener() {
@override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.cancel();
}
});
alert.show();
}
});
}

@override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (not.isChecked()) {
Toast.makeText(getApplicationContext() , "Notification " + not.getTextOn().toString() , Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext() , "Notification " + not.getTextOff().toString() , Toast.LENGTH_SHORT).show();
}
}
}
 
If you enclose your code in [code][/code] tags it will make it easier for the folks that can help to read your post. :)
 
Code:
public class partietache extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener {
Typeface typenomtache;
TextView txttache , txtheure , txtdate , txtstart , txtmenu , txtnot , txtdes;
EditText ed1 , ed2 , ed3 , ed4 , ed5;
Switch not;
Button btn;
RequestQueue requestQueue;
ProgressDialog progressDialog;
String insert = "http://192.168.100.41/projet/insert.php";
private static final String TAG = "partietache";
private DatePickerDialog.OnDateSetListener datelistener;
@override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_partietache);
txttache= (TextView) findViewById(R.id.nommenutach);
txtheure =(TextView) findViewById(R.id.heuretache);
txtdate = (TextView)findViewById(R.id.datetache);
txtstart = (TextView)findViewById(R.id.starttimetache);
txtmenu = (TextView)findViewById(R.id.nommenu);
txtdes = (TextView)findViewById(R.id.description);
txtnot = (TextView)findViewById(R.id.notification);
not = (Switch) findViewById(R.id.notifi);
ed1 = (EditText)findViewById(R.id.nomtache);
ed2 = (EditText)findViewById(R.id.edidatetache);
ed3 = (EditText)findViewById(R.id.editstarttache);
ed4 = (EditText)findViewById(R.id.editheuretache);
ed5 = (EditText)findViewById(R.id.editdes);
btn = (Button) findViewById(R.id.btnaddtask);
requestQueue = Volley.newRequestQueue(partietache.this);
progressDialog = new ProgressDialog (partietache.this);
ed2.setInputType(InputType.TYPE_NULL);
ed3.setInputType(InputType.TYPE_NULL);
typenomtache = Typeface.createFromAsset(getAssets(),"fonts/Montserrat-Regular.otf");
btn.setTypeface(typenomtache);
txttache.setTypeface(typenomtache);
txtheure.setTypeface(typenomtache);
txtdate.setTypeface(typenomtache);
txtstart.setTypeface(typenomtache);
txtmenu.setTypeface(typenomtache);
txtdes.setTypeface(typenomtache);
txtnot.setTypeface(typenomtache);
Code:
ed2.setOnClickListener(new View.OnClickListener() {
@override
public void onClick(View view) {
Calendar cal = Calendar.getInstance();
int year = cal.get(Calendar.YEAR );
int month = cal.get(Calendar.MONTH);
int day = cal.get(Calendar.DAY_OF_MONTH);
cal.set(Calendar.MONTH , month);
cal.set(Calendar.DAY_OF_MONTH , day);
DatePickerDialog dialog = new DatePickerDialog(partietache.this, android.R.style.Theme_Holo_Light_Dialog_MinWidth,
datelistener,
year, month, day);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
dialog.show();
}
});
datelistener = new DatePickerDialog.OnDateSetListener() {
@override
public void onDateSet(DatePicker datePicker, int year, int month, int day) {
month = month+1;
Log.d(TAG, "onDateSet: yyy-dd-mm: " + year + "-" + month + "-" + day);

String date = year + "-" + month + "-" + day ;
ed2.setText(date);
}
};
ed3.setOnClickListener(new View.OnClickListener() {
@override
public void onClick(View view) {
Calendar cal = Calendar.getInstance();
int hour = cal.get(Calendar.HOUR);
int minute = cal.get(Calendar.MINUTE);
TimePickerDialog time;
time = new TimePickerDialog(partietache.this, new TimePickerDialog.OnTimeSetListener() {
@override
public void onTimeSet(TimePicker timePicker, int i, int i1) {
ed3.setText(i + ":" + i1);

}
} , hour , minute , false);
time.show();
}
});
not.setOnCheckedChangeListener(this);
Code:
btn.setOnClickListener(new View.OnClickListener() {
@override
public void onClick(final View view) {
StringRequest request = new StringRequest(Request.Method.POST, insert, new Response.Listener<String>() {
@override
public void onResponse(String response) {

}
}, new Response.ErrorListener() {
@override
public void onErrorResponse(VolleyError error) {
progressDialog.dismiss();
Toast.makeText(partietache.this,error.toString(),Toast.LENGTH_LONG).show();

}
}){

@override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> para = new HashMap<String , String>();
para.put("taskname", ed1.getText().toString().trim());
para.put("totalwork",ed4.getText().toString().trim());
para.put("datetask",ed2.getText().toString().trim());
para.put("starttime",ed3.getText().toString().trim());
para.put("description",ed5.getText().toString().trim());
return para;

}
};
RequestQueue requestQueue = Volley.newRequestQueue(partietache.this);
requestQueue.add(request);
Snackbar snackbar = Snackbar.make(view,"Add with Succesful" , Snackbar.LENGTH_LONG);
snackbar.show();
new Timer().schedule(new TimerTask(){
public void run() {
partietache.this.runOnUiThread(new Runnable() {
@override
public void run() {
finish();
}
});
}

} , 4000);
}
});
Code:
ImageButton imgfermer = (ImageButton)findViewById(R.id.fermer);
imgfermer.setOnClickListener(new View.OnClickListener() {
@override
public void onClick(View view) {
AlertDialog.Builder alert = new AlertDialog.Builder(partietache.this);
alert.setTitle("Fermer ?");
alert.setMessage("voulez-vous fermer cette fenêtre ?");
alert.setPositiveButton("Oui", new DialogInterface.OnClickListener() {
@override
public void onClick(DialogInterface dialogInterface, int i) {
finish();
}
});
alert.setNegativeButton("Non", new DialogInterface.OnClickListener() {
@override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.cancel();
}
});
alert.show();
}
});
}

@override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (not.isChecked()) {
Toast.makeText(getApplicationContext() , "Notification " + not.getTextOn().toString() , Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext() , "Notification " + not.getTextOff().toString() , Toast.LENGTH_SHORT).show();
}
}
}
 
Could you, please, provide PHP files for local web server for this project and XML for activity.
It's not a problem, but without these files the answer will take a little bit more time.
 
Code:
<?php
  if ($_SERVER["REQUEST_METHOD"] == "POST"){
      require'connect.php';
      createTask();
  }
  function createTask(){
     
      global $connect ;
      $nametask = $_POST["taskname"];
      $tot = $_POST["totalwork"];
      $da = $_POST["datetask"];
      $st = $_POST["starttime"];
    $des = $_POST["description"];
  $query = " Insert into addtask (taskname,totalwork,datetask,starttime,description) values ('$nametask', '$tot', '$da','$st','$des');";
   if (mysqli_query($connect, $query))
   {
    echo 'Data Inserted Successfully';
   }
 else{

 echo 'Try Again';}
 mysqli_close($connect);
 }
 
?>
 
Notifications created at the moment when data input. So when you choose date and time and press button "put into database", at this moment you should create notification.
Notification manager will remember this notification and suspend it in background until the time, that set in the notification will come.
When current time equals the time in notification, Notification Manager activate this Notification automatically.
It's not necessary to compare current time and all time stamps in database. This work automatically done by Notification Manager.

Now the question is: what do you mean "when date current equals date in the base data (mysql)"?
Do you want put date and time into database. And after some delay create notifications for some records in the database?
Please confirm, that you want to start task on Date: datetask at Time: starttask.
 
Last edited:
Hello bro ,
what i mean is when the date current equals date in data , i need notification
for exemple
the date into data 02/04/2018
and date current also
02/04/2018 the notification created
 
Please find solution here https://github.com/v777779/aad_20180328
This is demo project based on your code.
Notification management implemented with Firebase Job Service. Project has all necessary utils for Notification management.
You can create and remove notification job task before it fired by Job Service ID.
When Notification fired, the Task Activity starts with intent extra parameter message of Notification.
You can use this message to identify Task that cause this Notification and Activity and perform specific processing.
 
Back
Top Bottom