Android通知

2018-07-08  本文已影响0人  aidlFor

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

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等安全锁和没有锁屏的情况下才能通知。

上一篇下一篇

猜你喜欢

热点阅读