#Android Learning# Learning of Three Kinds of Notification

Notification allows us to display corresponding information on the status bar and lock screen interface when obtaining information. If there is no Notification, then our QQ, WeChat and other applications cannot actively notify us, we need to check the mobile phone from time to time to check whether there are new information and reminders, if this is the case, then the experience of Android applications It was too bad. This also reflects the importance of Notification in the application. Learn three kinds of Notification today, which are ordinary Notification, folding Notification and hanging Notification:

The characteristics of the three Notifications:

  • Ordinary Notification: First displayed on the notification bar of the mobile phone, you need to pull down the notification page to see it, and the displayed content includes the APP icon, title, and part of the content;
  • Foldable Notification: It is firstly displayed on the notification bar of the mobile phone, and you need to pull down the notification page to see it, and the displayed content is more than that of ordinary Notificaton;
  • Hanging Notification: It displays the same content as ordinary Notification, but it hangs directly above the screen without pulling down the notification page, and the focus remains unchanged, and it is still in the user interface;

The renderings of three kinds of Notification:

The renderings of three kinds of Notification

The general steps to implement Notification:

  • [1.0] Pass NOTIFICATION_SERVICE through the getSystemService() method to get the system notification manager notificationManager;
  • [2.0] Through the Builder class Android 3.0 (API level 11) of the Notification object, a convenient method is provided to set each field of the notification, and the notification layout template of the Android system platform is used to generate the prompt frame of the Notification;
  • [3.0] Set a series of attributes such as content intent (for jumping), icon, content, etc. through the Notification builder;
  • [4.0] Finally, notify the manager's notify() through the NotificationManager to initiate a notification;

1.0 Realize the code logic of ordinary Notification:

//[1.0] 获取系统的通知管理者 notificationManager
        notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

The NOTIFICATION_SERVICE string is a static constant in the Content class;

public void sendNomalNotification() {
        /**
         *  Builder class for Notification objects.
         * notification对象的 Builder 类,提供一种方便的方法来设置通知的各个字段,
         * 并使用平台的通知的布局模板生成内容视图
         */
        builder = new Notification.Builder(mContext);
        //[2.0] 创建一个意图
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://haimabai.github.io/"));
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
        //[3.0] 给 notification 的构建者设置内容意图
        builder.setContentIntent(pendingIntent);
        builder.setSmallIcon(R.mipmap.ic_launcher_round);
        builder.setLargeIcon(BitmapFactory.decodeResource(getResources(),
        R.mipmap.ic_launcher_round));
        builder.setAutoCancel(true);
        builder.setContentTitle("普通通知");
        //设置可见性
        //VISIBILITY_PRIVATE:展示这个通知锁屏的情况下,但是隐藏敏感的或者私有的信息在安全锁屏;
        //VISIBILITY_PUBLIC:展示这个通知在整一个锁屏;
        //VISIBILITY_SECRET:没有揭示任何通知的信息在锁屏的情况下;
        builder.setVisibility(Notification.VISIBILITY_PUBLIC);
        builder.setContentText("这是普通通知的内容");
        notificationManager.notify(0, builder.build());
    }

The above is the detailed implementation of ordinary Notification. The main implementation logic is that the button is implemented by clicking, and then call sendNomalNotification() in the implementation method of the monitoring interface onClick() method.
One thing to note is that the use of the setVisibility() method is mainly to set different visibility levels according to the importance of notifying user information: - VISIBILITY_PRIVATE: Show
this notification in the case of lock screen, but hide sensitive or private information In safe lock screen;
- VISIBILITY_PUBLIC: show this notification in whole lock screen;
- VISIBILITY_SECRET: don't notify any notification information in case of lock screen;

One more thing, in the last line notificationManager.notify(0, builder.build()), the notify() method has two parameters:
- id: the identifier of the notification for this application
- notification: a Notification object describing what to display kind of notification to the user

2.0 Implement the code logic of folding Notification:

public void sendFlodingNotification() {
        builder = new Notification.Builder(mContext);
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://haimabai.github.io/"));
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
        builder.setContentIntent(pendingIntent);
        builder.setSmallIcon(R.mipmap.ic_launcher_round);
        builder.setLargeIcon(BitmapFactory.decodeResource(getResources(),
        R.mipmap.ic_launcher_round));
        builder.setAutoCancel(true);
        builder.setContentTitle("折叠式 Notification");
        RemoteViews remoteViews = new RemoteViews(getPackageName(),
        R.layout.view_fold);
        Notification notification = builder.build();
        notification.bigContentView = remoteViews;
        notificationManager.notify(1, builder.build());
    }

The biggest feature of the folded Notification is that a layout can be embedded into the Notification, making the notification more friendly and enabling the user to obtain more information from the notification; R.layout.view_fold is the embedded layout:

The original rendering of the mosaic layout:

The original rendering of the mosaic layout

The code to realize this layout is also quite simple, I will not copy and paste the specific code;

3.0 Implement the code logic of hanging Notification:

public void sendFloatingNotification() {
        builder = new Notification.Builder(mContext);
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://haimabai.github.io/"));
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
        builder.setContentIntent(pendingIntent);
        builder.setSmallIcon(R.mipmap.ic_launcher_round);
        builder.setLargeIcon(BitmapFactory.decodeResource(getResources(),
        R.mipmap.ic_launcher_round));
        builder.setAutoCancel(true);
        builder.setContentTitle("悬挂式 Notification");
        //设置点击跳转
        Intent hangIntent = new Intent();
        hangIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        hangIntent.setClass(this, MainActivity.class);
        //如果描述的PendingIntent已经存在,则在产生新的Intent之前会先取消掉当前的
        PendingIntent hangPendingIntent = PendingIntent.getActivity(this, 0,
        hangIntent, PendingIntent.FLAG_CANCEL_CURRENT);
        builder.setFullScreenIntent(hangPendingIntent, true);

        notificationManager.notify(2, builder.build());
    }

The code logic of the above three Notifications is displayed on the mobile terminal, only need to implement the click event of the button, and then directly call the corresponding method;

Of course, the current Android 8.0 (API level 26) or higher system supports better notification methods, such as:

  • APP can be displayed directly on the application icon

  • Users can directly long press the APP icon to view the notification

If you want to know and use more Notification, it is recommended to go to Google's official documentation, haha

Guess you like

Origin blog.csdn.net/HongHua_bai/article/details/77947587