美文网首页
Android Notification

Android Notification

作者: OldWang12138 | 来源:发表于2020-12-05 16:26 被阅读0次

    方便各位同学可以方便的发通知

    想起两年前开发8.0时, 弹出通知的功能突然不能用了!!!!  吓的我赶紧先看官方相关的文档

    以下是关于通知项的8.0更新

    提醒窗口

    如果应用使用 SYSTEM_ALERT_WINDOW 权限并且尝试使用以下窗口类型之一来在其他应用和系统窗口上方显示提醒窗口:

    TYPE_PHONE,

    TYPE_PRIORITY_PHONE,

    TYPE_SYSTEM_ALERT,

    TYPE_SYSTEM_OVERLAY,

    TYPE_SYSTEM_ERROR...那么,这些窗口将始终显示在使用 TYPE_APPLICATION_OVERLAY 窗口类型的窗口下方。如果应用针对的是 Android 8.0,则应用会使用 TYPE_APPLICATION_OVERLAY 窗口类型来显示提醒窗口。如需了解详细信息,请参阅针对 Android 8.0 的应用的行为变更内的提醒窗口的常用窗口类型部分。

    好像当初也解决了8.0以上不弹出问题, 毕竟已经这么久了.....

    这个类一直雪藏着, 希望可以帮到你们!

    /**

    * @author OldWang

    * @date 2020-12-5.

    * sendNotification 发送一条通知

    */

    public class NotificationUtilsextends ContextWrapper {

        public static final StringID ="android";

        public static final Stringname ="FJX";

        private NotificationManagermanager;

        private ContextmContext;

        public NotificationUtils(Context context) {

            super(context);

            mContext = context;

        }

        @RequiresApi(api = Build.VERSION_CODES.O)

        private void createNotificationChannel() {

            NotificationChannel channel =new NotificationChannel(ID, name, NotificationManager.IMPORTANCE_HIGH);

            getManager().createNotificationChannel(channel);

        }

        private NotificationManagergetManager() {

            if (manager ==null) {

            manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

             }

            return manager;

        }

        /**

        * What you need for Android 8.0 and above

        */

        @RequiresApi(api = Build.VERSION_CODES.O)

        private Notification.BuildergetChannelNotification(String title, String content) {

            // 这里点击通知跳转的界面

            Intent mainIntent =new Intent(this, LoginActivity.class);

            PendingIntent mainPendingIntent = PendingIntent.getActivity(this, 0, mainIntent, PendingIntent.FLAG_UPDATE_CURRENT);

            return new Notification.Builder(getApplicationContext(), ID)

                    .setContentTitle(title)

                    .setContentText(content)

                    .setSmallIcon(R.mipmap.fjx_logo)// 提示的LOGO

                    .setDefaults(Notification.DEFAULT_ALL)

                    .setAutoCancel(true)

                    .setVibrate(new long[]{100})// 震动

                    .setContentIntent(mainPendingIntent);

        }

    private NotificationCompat.BuildergetNotificationN(String title, String content) {

            Intent mainIntent =new Intent(this, LoginActivity.class);

            PendingIntent mainPendingIntent = PendingIntent.getActivity(this, 0, mainIntent, PendingIntent.FLAG_UPDATE_CURRENT);

            return new NotificationCompat.Builder(getApplicationContext())

                     .setContentTitle(title)

                     .setContentText(content)

                    .setSmallIcon(R.mipmap.fjx_logo)

                    .setDefaults(Notification.DEFAULT_ALL)

                    .setAutoCancel(true)

                    .setVibrate(new long[]{100})

                    .setContentIntent(mainPendingIntent);

        }

        /**

        * Send notice

        * @param title Title of notification

        * @param content  The content of the notification

        */

        public void sendNotification(String title, String content) {

                Logger logger = LoggerFactory.getLogger(NotificationUtils.class);

                // 这里是我们设置可关闭通知

                boolean isShowNotice = (boolean) SPUtils.get(mContext, Constant.IS_MESSAGE_NOTICE,

                        Constant.FILE_NAME_SETTING, true);

                logger.info("isShowNotice-----Notification-- : {}", isShowNotice);

                if (isShowNotice) {

                    if (Build.VERSION.SDK_INT >=26) {

                        createNotificationChannel();

                        Notification notification = getChannelNotification(title, content).build();

                        getManager().notify(1, notification);

                     } else {

                        Notification notification = getNotificationN(title, content).build();

                        getManager().notify(1, notification);

                    }

            }

        }

    }

    谢谢大家 

    相关文章

      网友评论

          本文标题:Android Notification

          本文链接:https://www.haomeiwen.com/subject/wnygwktx.html