通知未出现在NotificationBar中

时间:2019-08-08 22:15:13

标签: android android-notifications

我需要在我的应用中发出通知,我正在尝试此操作,但它不起作用。 我正在PdfCreator活动中实现功能addNotification,我正在这样做:

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());
}

然后我以这种方式在侦听器类中使用addNotification(): 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();
                    }
                }
            });

我没有收到错误,但是创建pdf后没有任何提示吗?

1 个答案:

答案 0 :(得分:0)

从Android 8.0(API 28)开始,Android需要提供通知通道。因此,没有频道就不会显示通知。 从文档中:

  

从Android 8.0(API级别26)开始,必须将所有通知分配给一个频道。对于每个渠道,您可以设置应用于该渠道中所有通知的视觉和听觉行为。然后,用户可以更改这些设置,并确定您应用中的哪些通知渠道应该是侵入性的或根本不可见的。

在此处查看更多信息:Create and Manage Notification Channels

相关问题