0

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?

Naitik Soni
  • 700
  • 8
  • 16

1 Answers1

0

Starting with Android 8.0 (API 28), Android requires notification channels to be provided. So notification will not be displayed without the channel. From the doc:

Starting in Android 8.0 (API level 26), all notifications must be assigned to a channel. For each channel, you can set the visual and auditory behavior that is applied to all notifications in that channel. Then, users can change these settings and decide which notification channels from your app should be intrusive or visible at all.

Check here for more information: Create and Manage Notification Channels

Ruslan Myhal
  • 266
  • 1
  • 2
  • 9