android notification()

    //创建普通通知
     String channelId = "测试渠道";
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            Notification notification = new Notification.Builder(getApplicationContext(),channelId)
                    .setContentTitle("测试通知标题")
                    .setContentText("测试通知文本")
                    .setWhen(System.currentTimeMillis())
                    .setSmallIcon(R.drawable.chips)
                    .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.chips))
                    .build();
            NotificationManager notificationManager = (NotificationManager)getApplicationContext().getSystemService(NOTIFICATION_SERVICE);
            NotificationChannel channel = new NotificationChannel(channelId,"测试渠道名称", NotificationManager.IMPORTANCE_DEFAULT);
            notificationManager.createNotificationChannel(channel);
            notificationManager.notify((int)Math.random(), notification);
        }

 //创建自定义布局的通知:

MainActivity:

    private static final String CHANNEL_ID = "channelID";

    private void customNotification() {
        // Get the layouts to use in the custom notification
        RemoteViews rv = new RemoteViews(getPackageName(), R.layout.custom_notification);
        // Apply the layouts to the notification
        Notification customNotification = new NotificationCompat.Builder(this, CHANNEL_ID)
                .setSmallIcon(R.drawable.chips)
                .setCustomContentView(rv)
                .build();
        NotificationManager notificationManager = (NotificationManager) this.getSystemService(NOTIFICATION_SERVICE);
        NotificationChannel channel = null;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            channel = new NotificationChannel(CHANNEL_ID, "测试渠道名称", NotificationManager.IMPORTANCE_DEFAULT);
            notificationManager.createNotificationChannel(channel);
        }

        // adding action to left button(添加点击事件)
        Intent rightIntent = new Intent(this, NotificationIntentService.class);
        rightIntent.setAction("left");
        rv.setOnClickPendingIntent(R.id.button2, PendingIntent.getService(this, 1, rightIntent, PendingIntent.FLAG_UPDATE_CURRENT));

        notificationManager.notify((int) Math.random(), customNotification);
    }

布局:(注意:不可以使用constraint布局)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="10dp"
    >
    <Button
        android:layout_marginRight="5dp"
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="show"
        />

    <Button
        android:layout_marginLeft="5dp"
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="hide"
        />
</LinearLayout>

AndroidManifest.xml(注册service)

<service android:name=".NotificationIntentService"></service>

获取点击事件:

public class NotificationIntentService extends IntentService {
    public NotificationIntentService() {
        super("notificationIntentService");
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        switch (intent.getAction()){
            case "left":
                android.os.Handler leftHandler = new android.os.Handler(Looper.getMainLooper());
                leftHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(getBaseContext(),
                                "You clicked the left button", Toast.LENGTH_LONG).show();
                    }
                });
                break;
        }
    }
}
————————
    //创建普通通知
     String channelId = "测试渠道";
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            Notification notification = new Notification.Builder(getApplicationContext(),channelId)
                    .setContentTitle("测试通知标题")
                    .setContentText("测试通知文本")
                    .setWhen(System.currentTimeMillis())
                    .setSmallIcon(R.drawable.chips)
                    .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.chips))
                    .build();
            NotificationManager notificationManager = (NotificationManager)getApplicationContext().getSystemService(NOTIFICATION_SERVICE);
            NotificationChannel channel = new NotificationChannel(channelId,"测试渠道名称", NotificationManager.IMPORTANCE_DEFAULT);
            notificationManager.createNotificationChannel(channel);
            notificationManager.notify((int)Math.random(), notification);
        }

 //创建自定义布局的通知:

MainActivity:

    private static final String CHANNEL_ID = "channelID";

    private void customNotification() {
        // Get the layouts to use in the custom notification
        RemoteViews rv = new RemoteViews(getPackageName(), R.layout.custom_notification);
        // Apply the layouts to the notification
        Notification customNotification = new NotificationCompat.Builder(this, CHANNEL_ID)
                .setSmallIcon(R.drawable.chips)
                .setCustomContentView(rv)
                .build();
        NotificationManager notificationManager = (NotificationManager) this.getSystemService(NOTIFICATION_SERVICE);
        NotificationChannel channel = null;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            channel = new NotificationChannel(CHANNEL_ID, "测试渠道名称", NotificationManager.IMPORTANCE_DEFAULT);
            notificationManager.createNotificationChannel(channel);
        }

        // adding action to left button(添加点击事件)
        Intent rightIntent = new Intent(this, NotificationIntentService.class);
        rightIntent.setAction("left");
        rv.setOnClickPendingIntent(R.id.button2, PendingIntent.getService(this, 1, rightIntent, PendingIntent.FLAG_UPDATE_CURRENT));

        notificationManager.notify((int) Math.random(), customNotification);
    }

布局:(注意:不可以使用constraint布局)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="10dp"
    >
    <Button
        android:layout_marginRight="5dp"
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="show"
        />

    <Button
        android:layout_marginLeft="5dp"
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="hide"
        />
</LinearLayout>

AndroidManifest.xml(注册service)

<service android:name=".NotificationIntentService"></service>

获取点击事件:

public class NotificationIntentService extends IntentService {
    public NotificationIntentService() {
        super("notificationIntentService");
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        switch (intent.getAction()){
            case "left":
                android.os.Handler leftHandler = new android.os.Handler(Looper.getMainLooper());
                leftHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(getBaseContext(),
                                "You clicked the left button", Toast.LENGTH_LONG).show();
                    }
                });
                break;
        }
    }
}