Android Notification 手机系统横幅弹出提示框调用

    xiaoxiao2021-03-25  109

    类似于仿微信信息提示提出框。

    在项目当中集成了推送功能,当手机接收到消息后只是在手机通知栏有提示信息。所以需要展示像微信信息弹出框一样的效果,开始自己还以为微信信息弹出框是自定义Dialog之类的自定义控件;后面发现其实微信也是调用 Android Notification 系统自带的通知栏,在其中可以定义各种各样的通知栏样式。

    具体详情也可以参看:

    https://developer.android.com/guide/topics/ui/notifiers/notifications.html#lockscreenNotification

    https://shoewann0402.github.io/2016/05/12/Android-Notification-通知样式总结/

    这里只是列出仿微信信息通知的横幅通知栏:

    notificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);         if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {             Toast.makeText(context, "此类通知在Android 5.0以上版本才会有横幅有效!", Toast.LENGTH_SHORT).show();         }         //为了版本兼容  选择V7包下的NotificationCompat进行构造         NotificationCompat.Builder builder = new NotificationCompat.Builder(context);         builder.setContentTitle("横幅通知");         builder.setContentText("请在设置通知管理中开启消息横幅提醒权限");         builder.setDefaults(NotificationCompat.DEFAULT_ALL);         builder.setSmallIcon(R.drawable.app_icon);         builder.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_launcher));         Intent intent = new Intent(context, TestActivity.class);         PendingIntent pIntent = PendingIntent.getActivity(context, 1, intent, 0);         builder.setContentIntent(pIntent);         builder.setFullScreenIntent(pIntent, true);         builder.setAutoCancel(true);         Notification notification = builder.build();         notificationManager.notify(1, notification);//注意这里 1 为当前通知栏的 Id 号,和 Fragment 设置 Id 是一样的         // 设置 heads-up 消失任务         TimerTask task = new TimerTask() {             @Override             public void run() {                 System.out.println("start the cancel task....");                 notificationManager.cancel(1); // 根据之前设置的通知栏 Id 号,让相关通知栏消失掉             }         };         Timer timer = new Timer();         timer.schedule( task , 2000);     }

    由于等待通知栏自己消失的时间过于长,找了半天也没有找到合适的API;所以只有自己设置一个定时任务,弹出两秒后让其消失。

    转载请注明原文地址: https://ju.6miu.com/read-13259.html

    最新回复(0)