I need notification in my app, i'm tring this but it doesn't work. I implement the function addNotification in PdfCreator activity, i'm doing this:
PdfCreatorActivity:
public class PdfCreatorActivity extends AppCompatActivity {
private ArrayList<String> listaDate = new ArrayList<>();
ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pdf_creator);
listView= findViewById(R.id.list_item) ;
ArrayList<MatchModel> matchModelList = PdfBusiness.getOurInstance().TrovaDatePartite(this);
// Initializing an ArrayAdapter
for (int i=0 ; i<matchModelList.size() ; i++){
listaDate.add(matchModelList.get(i).getData() + " " + matchModelList.get(i).getSquadraCasa() + " - " + matchModelList.get(i).getSquadraOspite());
}
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, listaDate);
listView.setAdapter(adapter);
ListViewListener listener = new ListViewListener(matchModelList, this);
listView.setOnItemClickListener(listener);
}
@Override
public void onBackPressed() {
super.onBackPressed();
this.finish();
}
public void addNotification() {
// Builds your notification
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher_round)
.setContentTitle("John's Android Studio Tutorials")
.setContentText("A video has just arrived!");
// Creates the intent needed to show the notification
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(contentIntent);
// Add as notification
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(0, builder.build());
}
then i use addNotification() in a listener class in this way: ListViewListener:
public class ListViewListener implements AdapterView.OnItemClickListener {
ArrayList<MatchModel> matchModel;
PdfCreatorActivity activity;
MatchModel match;
public ListViewListener(ArrayList<MatchModel> matchModel, PdfCreatorActivity activity) {
this.matchModel = matchModel;
this.activity=activity;
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
MatchModel model = matchModel.get(position);
setMatch(model);
//APRO UN POPUP
AlertDialog.Builder builder = new AlertDialog.Builder(activity);
builder.setCancelable(true);
builder.setTitle("Cosa vuoi fare?");
builder.setMessage("I PDF VENGONO SALVATI NELLA CARTELLA DOWNLOAD");
builder.setPositiveButton("Genera PDF",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (ContextCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED || ContextCompat.checkSelfPermission(activity,
Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
if (ContextCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED)
ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
1);
if (ContextCompat.checkSelfPermission(activity, Manifest.permission.READ_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED)
ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
1);
} else {
createPDF();
activity.addNotification();
dialog.cancel();
}
}
});
I don't get error but it doesn't show notification after create pdf, any ideas?