发送到通知栏
NotificationManager.notify(int id, Notification notification) ;
删除对应的通知消息
NotificationManager.cancel(id);
下面的案例调用方式在最下面
在Application自定义一个通知栏
安卓7.0后好像不太支持从后台直接唤起程序,我就想到了从通知栏获取进入,事实证明还是可以达到目的的
private static final int VISIBILITY_SCREEN = -1000;
private NotificationManager mManager;
主要方法
/**
* 发送自定义通知栏
*/
private void sendCustomNotify(String nickname) {
Notification.Builder builder = creatNotification();
RemoteViews remoteView = new RemoteViews(getPackageName(), R.layout.notify_layout);
remoteView.setTextViewText(R.id.notify_title, nickname);
remoteView.setTextViewText(R.id.notify_content, "来自数字孪生的一则消息");
//即将发生的意图可以取消可以更新
/*
* 点击进入会话
* */
PendingIntent pendingIntent = PendingIntent.getActivity(this, -1,
new Intent(this, FragmentManagerActivity.class), PendingIntent.FLAG_UPDATE_CURRENT);
/*
* 直接挂断
* */
remoteView.setOnClickPendingIntent(R.id.ring_hangoff, pendingIntent);
/*
* 视频聊天
* */
remoteView.setOnClickPendingIntent(R.id.ring_pickup, pendingIntent);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
builder.setCustomContentView(remoteView);
}
mManager.notify(3, builder.build());
}
/**
* 封装一个通知builder
*
* @return
*/
private Notification.Builder creatNotification() {
Notification.Builder builder = new Notification.Builder(this)
.setAutoCancel(true)
.setContentTitle("消息来了")
.setContentText("消息通知内容")
.setSmallIcon(R.drawable.logo);
//适配8.0以上通知栏
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
//第三个参数设置通知的优先级别
NotificationChannel channel =
new NotificationChannel("channel_id", "app_msg", NotificationManager.IMPORTANCE_DEFAULT);
channel.canBypassDnd();//是否可以绕过请勿打扰模式
channel.canShowBadge();//是否可以显示icon角标
channel.enableLights(true);//是否显示通知闪灯
channel.enableVibration(true);//收到小时时震动提示
channel.setBypassDnd(true);//设置绕过免打扰
channel.setLockscreenVisibility(VISIBILITY_SCREEN);
channel.setLightColor(Color.RED);//设置闪光灯颜色
channel.getAudioAttributes();//获取设置铃声设置
channel.setVibrationPattern(new long[]{100, 200, 100});//设置震动模式
channel.shouldShowLights();//是否会闪光
mManager.createNotificationChannel(channel);
builder.setChannelId("channel_id");//这个id参数要与上面channel构建的第一个参数对应
}
return builder;
}
图片自己充填一下
<?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="100dp"
android:background="@drawable/date_background"
android:padding="10dp"
android:orientation="horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<LinearLayout
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp">
<ImageView
android:padding="8dp"
android:src="@drawable/ic_callicon"
android:layout_width="40dp"
android:layout_height="match_parent"/>
<TextView
android:textColor="#ffffff"
android:layout_gravity="center_vertical"
android:id="@+id/notify_title"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="18dp"
android:gravity="center_vertical"
android:text="标题" />
</LinearLayout>
<TextView
android:textColor="#ffffff"
android:layout_weight="1"
android:layout_gravity="center_vertical"
android:id="@+id/notify_content"
android:layout_width="match_parent"
android:layout_height="0dp"
android:gravity="center_vertical"
android:text="来自数字孪生的一则消息" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="60dp"
android:layout_height="match_parent">
<ImageView
android:padding="10dp"
android:id="@+id/ring_hangoff"
android:layout_width="match_parent"
android:layout_height="60dp"
android:src="@drawable/icon_hangup"/>
<TextView
android:textColor="#ffffff"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="12dp"
android:gravity="center"
android:text="挂断" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="60dp"
android:layout_height="match_parent">
<ImageView
android:padding="10dp"
android:id="@+id/ring_pickup"
android:layout_width="match_parent"
android:layout_height="60dp"
android:src="@drawable/icon_pickup"/>
<TextView
android:textColor="#ffffff"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="12sp"
android:gravity="center"
android:text="接听" />
</LinearLayout>
</LinearLayout>
启用
sendCustomNotify(send_nick);
清除
mManager.cancel(3);