Android-通知

android8.0以降のバージョンとandroid7.0の通知の違いは、上記のバージョンでは
、通知を作成する前にチャネルNotificationChannelを追加する必要があることです。Androidのバージョンを判断する必要があります。

  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)

通知に応答するボタンはactivity_main.xmlコードに1つだけあり、コードは
次のようにMainActivity.javaコードを表示しなくなりました

public class MainActivity extends AppCompatActivity {
    private Button GetNotification;
    private static final int ID = 1;
    private static final String CHANNELID ="1";
    private static final String CHANNELNAME = "channel1";

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

        GetNotification = (Button) findViewById(R.id.GetNotification);
        GetNotification.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
               // manager.cancel(1);
                //安卓8.0以上弹出通知需要添加渠道NotificationChannel
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
                    //创建渠道
                    /**
                     * importance:用于表示渠道的重要程度。这可以控制发布到此频道的中断通知的方式。
                     * 有以下6种重要性,是NotificationManager的静态常量,依次递增:
                     * IMPORTANCE_UNSPECIFIED(值为-1)意味着用户没有表达重要性的价值。此值用于保留偏好设置,不应与实际通知关联。
                     * IMPORTANCE_NONE(值为0)不重要的通知:不会在阴影中显示。
                     * IMPORTANCE_MIN(值为1)最低通知重要性:只显示在阴影下,低于折叠。这不应该与Service.startForeground一起使用,因为前台服务应该是用户关心的事情,所以它没有语义意义来将其通知标记为最低重要性。如果您从Android版本O开始执行此操作,系统将显示有关您的应用在后台运行的更高优先级通知。
                     * IMPORTANCE_LOW(值为2)低通知重要性:无处不在,但不侵入视觉。
                     * IMPORTANCE_DEFAULT (值为3):默认通知重要性:随处显示,产生噪音,但不会在视觉上侵入。
                     * IMPORTANCE_HIGH(值为4)更高的通知重要性:随处显示,造成噪音和窥视。可以使用全屏的Intent。
                     */
                    NotificationChannel channel = new NotificationChannel(CHANNELID,CHANNELNAME,NotificationManager.IMPORTANCE_HIGH);
                    manager.createNotificationChannel(channel);//开启渠道
                    Intent intent = new Intent(MainActivity.this,notification.class);
                    PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this,0,intent,0);
                    NotificationCompat.Builder builder = new NotificationCompat.Builder(MainActivity.this,CHANNELID);
                    builder .setContentTitle("Title")//通知标题
                            .setContentText("ContentText")//通知内容
                            .setWhen(System.currentTimeMillis())//通知显示时间
                            .setContentIntent(pendingIntent)
                            .setSmallIcon(R.drawable.smile)
                            .setAutoCancel(true)//点击通知取消
                            //.setSound()
                            //第一个参数为手机静止时间,第二个参数为手机震动时间,周而复始
                            .setVibrate(new long[] {0,1000,1000,1000})//手机震动
                            //第一个参数为LED等颜色,第二个参数为亮的时长,第三个参数为灭的时长
                            .setLights(Color.BLUE,1000,1000)
                            /**表示通知的重要程度
                             * RIORITY_DEFAULT
                             * RIORITY_MIN
                             * RIORITY_LOW
                             * RIORITY_HIGE
                             * RIORITY_MAX
                            **/
                            .setPriority(NotificationCompat.PRIORITY_MAX)
                            .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.smile)).build();
                    manager.notify(1,builder.build());
                } else{
                Notification notification = new NotificationCompat.Builder(MainActivity.this)
                        .setContentTitle("Title")
                        .setContentText("ContentText")
                        .setWhen(System.currentTimeMillis())
                        .setSmallIcon(R.drawable.smile)
                        .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.smile))
                        .build();
                manager.notify(1,notification);
            }
       }
        });
    }
}

また、通知をクリックした後に実行された操作に応答するアクティビティ通知を作成する必要があります。ここでは、TextViewを使用してテキストを表示します。
電話のバイブレーションを設定する場合は、マニフェストファイルで権限を宣言する必要があります。 。

 <uses-permission android:name="android.permission.VIBRATE"/>

レンダリングは次のとおりです
。ボタンをクリックすると通知が表示されます。

通知をクリックして通知アクティビティにジャンプします。.setAutoCancel(true)を設定している

ため、すべての通知をクリックすると通知が消えます。

おすすめ

転載: blog.csdn.net/News53231323/article/details/113883871