单挑通知仅显示一次

时间:2018-07-04 08:12:17

标签: java android

尽管我多次单击按钮,抬头通知仍显示一次。即使我杀死了该应用程序,然后再次单击同一按钮,也没有显示提示通知。(相反,它在状态栏中显示为图标。但是,不是提示提示)

除非我卸载整个应用程序并重新安装,否则它不起作用。

这是什么原因?以及如何确保每次单击按钮时它都显示通知?

MainActivity.java

package com.example.notificationtest3;

public class MainActivity extends AppCompatActivity implements  View.OnClickListener{
    private String CHANNEL_ID="channel_id_1";
    private String textTitle="This is a title";
    private String textContent="This is a content";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button button=(Button)findViewById(R.id.button);
        button.setOnClickListener(this);

    }

    @Override
    public void onClick(View v){
        switch(v.getId()){
            case R.id.button:
                if(Build.VERSION.SDK_INT>=26){
                    Toast.makeText(MainActivity.this, ">26",Toast.LENGTH_SHORT).show();

                    NotificationChannel channel = new NotificationChannel(CHANNEL_ID, "Channel name",
                            NotificationManager.IMPORTANCE_HIGH);
                    channel.setDescription("description");
                    NotificationManager notificationManager = getSystemService(NotificationManager.class);
                    notificationManager.createNotificationChannel(channel);


                    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, CHANNEL_ID)
                            .setSmallIcon(R.mipmap.ic_launcher)
                            .setContentTitle(textTitle)
                            .setContentText(textContent);
                    notificationManager.notify(1, mBuilder.build());

                }else{
                    Toast.makeText(MainActivity.this, "<26",Toast.LENGTH_SHORT).show();

                    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, CHANNEL_ID)
                            .setSmallIcon(R.mipmap.ic_launcher)
                            .setContentTitle(textTitle)
                            .setContentText(textContent)
                            .setVibrate(new long[0])
                            .setPriority(NotificationCompat.PRIORITY_MAX);
                    NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
                    notificationManager.notify(1, mBuilder.build());
                }

                break;
            default:
                break;
        }

    }
}

0 个答案:

没有答案
相关问题