Android通知

通知在实际开发中还是比较常见的,例如新闻,音乐播放器,等。

1,基本通知

//初始化通知管理器
NotificationManager notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);

Notification.Builder builder = new Notification.Builder(this);

Intent mIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.jianshu.com/p/890acf8e5080"));

    PendingIntent pendingIntent = PendingIntent.getActivity(this,0,mIntent,0);

    builder.setContentIntent(pendingIntent);

    builder.setSmallIcon(R.drawable.ic_launcher_background);

    builder.setContentTitle("标题");

    builder.setAutoCancel(true);

    builder.setContentText("通知消息");

    Notification notification = builder.build();

    notificationManager.notify(0, notification);
Screenshot_1531046655.png

2,基础扩展通知自定义布局

xml >> item_notification

<?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:orientation="vertical">
<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@mipmap/ic_launcher"/>
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="this is my View ,https://www.jianshu.com/u/6bbde9df8181https://www.jianshu.com/u/6bbde9df8181https://www.jianshu.com/u/6bbde9df8181https://www.jianshu.com/u/6bbde9df8181https://www.jianshu.com/u/6bbde9df8181https://www.jianshu.com/u/6bbde9df8181"
    />
</LinearLayout> 

JAVA类

   首先要创建一个RemoteViews自定义视图 详情https://blog.csdn.net/baidu_26352053/article/details/54943759 

    RemoteViews remoteViews = new RemoteViews(getPackageName(),R.layout.item_notification);

    Notification notification1 = builder.build();

    notification1.bigContentView= remoteViews;

    notificationManager.notify(1, notification1);

注意,这里是在第一个通知的基础上写的,和第一个的区别就是可展开的自定义视图,
notification1.bigContentView= remoteViews;表示设置为普通视图,但是可以展开,如果需要默认展开,则设置notification1.contentView= remoteViews; 效果图如下


Screenshot_1531046886.png

可以看到在第二个通知中存在可以展开的符号,也就是,layout >> item_notification这个布局。

3,基础再扩展,悬浮通知,都是基于第一个例子

            Intent flutterIntent =new Intent();

            flutterIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

            flutterIntent.setClass(NotificationActivity.this,RecycleViewActivity.class);

            PendingIntent flutterPendingIntent = PendingIntent.getActivity(NotificationActivity.this,0,flutterIntent,PendingIntent.FLAG_CANCEL_CURRENT);

            builder.setFullScreenIntent(flutterPendingIntent,true);

            Notification notification2 = builder.build();

            notificationManager.notify(2,notification2);

值得注意的是这种方式弹窗不会自动消失


Screenshot_1531046892.png

除了上述的用法,通知在5.0之后添加了通知等级,分别有

builder.setVisibility(Notification.VISIBILITY_PUBLIC); 任何情况都都通知

builder.setVisibility(Notification.VISIBILITY_PRIVATE); 只有在没有锁屏情况下通知

builder.setVisibility(Notification.VISIBILITY_SECRET); 在pin,password等安全锁和没有锁屏的情况下才能通知。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 原文出处: http://www.androidchina.net/6174.html Notification在...
    木木00阅读 14,245评论 3 32
  • Notification简介 通知是在常规UI界面之外向用户展示消息的工具,当系统发出通知时,它会以图表的形式显示...
    Cris_Ma阅读 11,846评论 2 10
  • 前两天看了官方的教学视频,讲的是使用NotificationCompact来使用通知.后来网上搜索了关与通知的文章...
    花京院典明阅读 10,589评论 0 5
  • 系统默认的通知风格 可以看到,Android通知栏默认是标题显示一行,内容显示一行,对于一行显示不完的,用省略号代...
    骑着海去看蜗牛阅读 20,439评论 8 44
  • 世界说大很大,说小很小。大到走了那么久,还没跟对的人相遇,小到围着喜欢的人绕一圈,就看到了全世界。
    kines阅读 1,355评论 0 0

友情链接更多精彩内容