Android Notification send notification

1. Overview of Notification

Notification, commonly known as notification, is a notification with a global effect. It is displayed at the top of the screen. It will first appear in the form of an icon. When the user swipes down, the specific content of the notification is displayed. The system provides developers with different types of notification style templates to use, and developers can also customize notification styles according to their needs.

2. Notification Channel (NotificationChannel)

The notification channel is a new feature added by Google in Android O. In the new version, vibration, sound effects and lighting effects are placed in the notification channel to control, so that users can selectively control a certain type of notification of the application The notification effect, unlike the previous version, all notifications are controlled by one setting. It should be noted that in the Android O version, the notification channel must be set for the notification when sending the notification, otherwise the notification will not be sent .

public void addNotificationChannel() {
    
    
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    
    
        //创建通知渠道
        CharSequence name = "渠道名称1";
        String description = "渠道描述1";
        int importance = NotificationManager.IMPORTANCE_DEFAULT;//重要性级别 这里用默认的
        NotificationChannel mChannel = new NotificationChannel(channelId, name, importance);

        mChannel.setDescription(description);//渠道描述
        mChannel.enableLights(true);//是否显示通知指示灯
        mChannel.enableVibration(true);//是否振动

        mNotificationManager.createNotificationChannel(mChannel);//创建通知渠道
    }
}

Three, send notification

1. Simple notification

The effect diagram is as follows:


The specific code is as follows:

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, channelId)
        .setSmallIcon(R.mipmap.ic_launcher)//小图标
        .setContentTitle("我是标题")
        .setContentText("我是内容内容");

mNotificationManager.notify(id, mBuilder.build());

2. Large text notification

The effect diagram is as follows:


The specific code is as follows:

NotificationCompat.Builder mBuilder2 = new NotificationCompat.Builder(this, channelId)
        .setSmallIcon(R.mipmap.ic_launcher)//小图标
        .setStyle(new NotificationCompat.BigTextStyle().bigText("通知渠道是 Google 在 Android O 中新增加的功能," +
                "新的版本中把振动、音效和灯效等相关效果放在了通知渠道中控制," +
                "这样用户就可以有选择的控制应用的某一类通知的通知效果," +
                "而不像之前版本中应用所有通知都受控于一种设置。需要注意的是," +
                "在 Android O 版本中,发送通知的时候必须要为通知设置通知渠道,否则通知不会被发送"))
        .setContentTitle("我是标题");

mNotificationManager.notify(id, mBuilder2.build());

3. The notification can be clicked

The effect diagram is as follows: the
gif display
specific code is as follows:

NotificationCompat.Builder mBuilder2 = new NotificationCompat.Builder(this, channelId)
        .setSmallIcon(R.mipmap.ic_launcher)//小图标
        .setStyle(new NotificationCompat.BigTextStyle().bigText("通知渠道是 Google 在 Android O 中新增加的功能," +
                "新的版本中把振动、音效和灯效等相关效果放在了通知渠道中控制," +
                "这样用户就可以有选择的控制应用的某一类通知的通知效果," +
                "而不像之前版本中应用所有通知都受控于一种设置。需要注意的是," +
                "在 Android O 版本中,发送通知的时候必须要为通知设置通知渠道,否则通知不会被发送"))
        .setContentTitle("我是标题");

Intent intent = new Intent(this, NotificationActivity.class);
PendingIntent ClickPending = PendingIntent.getActivity(this, 0, intent, 0);

mBuilder2.setContentIntent(ClickPending);
mBuilder2.setAutoCancel(true);//点击这条通知自动从通知栏中取消

mNotificationManager.notify(id, mBuilder2.build());

4. Click event monitoring

We all know that the system does not provide the corresponding API to listen for notification click events. So what if you want to capture the notification click. A method getBroadcast() is provided in PendingIntent, which we can handle by broadcasting: when the user clicks on the notification, a broadcast is sent, and we can do the required operations in the broadcast receiver.

The specific code is as follows:

NotificationCompat.Builder mBuilder3 = new NotificationCompat.Builder(this, channelId)
        .setSmallIcon(R.mipmap.ic_launcher)//小图标
        .setStyle(new NotificationCompat.BigTextStyle().bigText("通知渠道是 Google 在 Android O 中新增加的功能," +
                "新的版本中把振动、音效和灯效等相关效果放在了通知渠道中控制," +
                "这样用户就可以有选择的控制应用的某一类通知的通知效果," +
                "而不像之前版本中应用所有通知都受控于一种设置。需要注意的是," +
                "在 Android O 版本中,发送通知的时候必须要为通知设置通知渠道,否则通知不会被发送"))
        .setContentTitle("我是标题");

Intent intent3 = new Intent(BROADCAST_ACTION);
intent3.putExtra("data","12345");//带上参数
PendingIntent pendingIntent = PendingIntent.getBroadcast(this,id,intent3, PendingIntent.FLAG_UPDATE_CURRENT);

mBuilder3.setContentIntent(pendingIntent);
mBuilder3.setAutoCancel(true);//点击这条通知自动从通知栏中取消

mNotificationManager.notify(id, mBuilder3.build());
break;

The accepted code is as follows:

public class DynamicBroadcast extends BroadcastReceiver {
    
    
    @Override
    public void onReceive(Context context, Intent intent){
    
    
        String data = intent.getStringExtra("data");
        Log.i("dataTag","点击通知事件发过来的数据为: " + data);
    }
}

The results of the operation are as follows:
data
we can see that we have successfully detected the click event and received the transmitted data.

Fourth, the complete code

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    
    

    private Button btn_send;
    private Button btn_send_big_text;
    private Button btn_broadcast;

    // 通知相关
    private int id = 1111;
    private String channelId = "channelId1";//渠道id
    private NotificationManager mNotificationManager;

    private String BROADCAST_ACTION="android.intent.action.BROADCAST_ACTION";
    private DynamicBroadcast dynamicBroadcast;

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

        btn_send = findViewById(R.id.btn_send);
        btn_send_big_text = findViewById(R.id.btn_send_big_text);
        btn_broadcast = findViewById(R.id.btn_broadcast);
        btn_send.setOnClickListener(this);
        btn_send_big_text.setOnClickListener(this);
        btn_broadcast.setOnClickListener(this);

        mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        addNotificationChannel();


        //动态注册广播
        dynamicBroadcast=new DynamicBroadcast();
        IntentFilter intentFilter=new IntentFilter(BROADCAST_ACTION);
        registerReceiver(dynamicBroadcast,intentFilter);
    }

    @Override
    protected void onDestroy() {
    
    
        super.onDestroy();

        if(dynamicBroadcast!=null){
    
    
            unregisterReceiver(dynamicBroadcast);
        }
    }

    public void addNotificationChannel() {
    
    
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    
    
            //创建通知渠道
            CharSequence name = "渠道名称1";
            String description = "渠道描述1";
            int importance = NotificationManager.IMPORTANCE_DEFAULT;//重要性级别 这里用默认的
            NotificationChannel mChannel = new NotificationChannel(channelId, name, importance);

            mChannel.setDescription(description);//渠道描述
            mChannel.enableLights(true);//是否显示通知指示灯
            mChannel.enableVibration(true);//是否振动

            mNotificationManager.createNotificationChannel(mChannel);//创建通知渠道
        }
    }


    @Override
    public void onClick(View v) {
    
    
        switch (v.getId()) {
    
    
            case R.id.btn_send:
                NotificationCompat.Builder mBuilder1 = new NotificationCompat.Builder(this, channelId)
                        .setSmallIcon(R.mipmap.ic_launcher)//小图标
                        .setContentTitle("我是标题")
                        .setContentText("我是内容内容");

                mNotificationManager.notify(id, mBuilder1.build());
                break;
            case R.id.btn_send_big_text:
                NotificationCompat.Builder mBuilder2 = new NotificationCompat.Builder(this, channelId)
                        .setSmallIcon(R.mipmap.ic_launcher)//小图标
                        .setStyle(new NotificationCompat.BigTextStyle().bigText("通知渠道是 Google 在 Android O 中新增加的功能," +
                                "新的版本中把振动、音效和灯效等相关效果放在了通知渠道中控制," +
                                "这样用户就可以有选择的控制应用的某一类通知的通知效果," +
                                "而不像之前版本中应用所有通知都受控于一种设置。需要注意的是," +
                                "在 Android O 版本中,发送通知的时候必须要为通知设置通知渠道,否则通知不会被发送"))
                        .setContentTitle("我是标题");

                Intent intent = new Intent(this, NotificationActivity.class);
                PendingIntent ClickPending = PendingIntent.getActivity(this, 0, intent, 0);

                mBuilder2.setContentIntent(ClickPending);
                mBuilder2.setAutoCancel(true);//点击这条通知自动从通知栏中取消

                mNotificationManager.notify(id, mBuilder2.build());

                break;
            case R.id.btn_broadcast:
                NotificationCompat.Builder mBuilder3 = new NotificationCompat.Builder(this, channelId)
                        .setSmallIcon(R.mipmap.ic_launcher)//小图标
                        .setStyle(new NotificationCompat.BigTextStyle().bigText("通知渠道是 Google 在 Android O 中新增加的功能," +
                                "新的版本中把振动、音效和灯效等相关效果放在了通知渠道中控制," +
                                "这样用户就可以有选择的控制应用的某一类通知的通知效果," +
                                "而不像之前版本中应用所有通知都受控于一种设置。需要注意的是," +
                                "在 Android O 版本中,发送通知的时候必须要为通知设置通知渠道,否则通知不会被发送"))
                        .setContentTitle("我是标题");

                Intent intent3 = new Intent(BROADCAST_ACTION);
                intent3.putExtra("data","12345");//带上参数
                PendingIntent pendingIntent = PendingIntent.getBroadcast(this,id,intent3, PendingIntent.FLAG_UPDATE_CURRENT);

                mBuilder3.setContentIntent(pendingIntent);
                mBuilder3.setAutoCancel(true);//点击这条通知自动从通知栏中取消

                mNotificationManager.notify(id, mBuilder3.build());
                break;
        }
    }
}

Five, remarks

During the test, it was found that on Google's Pixel phone, when the notification is displayed, the icon style corresponding to the small icon is a pure white small square or small round block. As shown in the figure below:





The reason for this problem is that the picture set by the setSmallIcon() method on the google native mobile phone must have a transparent background, and all opaque points will eventually be displayed as white. See this blog for details .

Guess you like

Origin blog.csdn.net/weixin_38478780/article/details/108355483