NotificationUtil(通知栏工具类)
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Build;
import android.widget.RemoteViews;
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;
import com.example.a_notification.BroadcastReceiver.MyBroadcastReceiver;
import com.example.a_notification.NotificationActivity;
import com.example.a_notification.R;
import com.example.a_notification.Util.BitmapUtils;
/**
*
**/
public class NotificationUtil {
private static final String CHANNEL_ID ="通知名字";
public static void addNotifiion(Context context,int noId,String title,String content,int leftImg,String actionName,int heart){
createNotificationChannel(context);
Intent intent = new Intent(context, NotificationActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
NotificationCompat.DecoratedCustomViewStyle decoratedCustomViewStyle= new NotificationCompat.DecoratedCustomViewStyle();
RemoteViews contentView = new RemoteViews(context.getPackageName(), R.layout.notification_large);
RemoteViews contentView1 = new RemoteViews(context.getPackageName(),R.layout.notification_small);
//按钮点击事件:
Intent intentClick=new Intent(context,MyBroadcastReceiver.class);
intentClick.putExtra("typeNotifi",noId);
intentClick.setAction(actionName);//按钮动作明
PendingIntent homeIntent = PendingIntent.getBroadcast(context,1,intentClick,PendingIntent.FLAG_UPDATE_CURRENT);
contentView.setOnClickPendingIntent(R.id.b1,homeIntent);
contentView.setTextViewText(R.id.t1,title);//设置标题
contentView.setTextViewText(R.id.t2,content);//设置内容
Bitmap bitmap= BitmapUtils.getBitmapFromResource(context.getResources(),leftImg);
contentView.setImageViewBitmap(R.id.leftimg,bitmap);
if(heart!=-1){
Bitmap bitmap1= BitmapUtils.getBitmapFromResource(context.getResources(),heart);
contentView.setImageViewBitmap(R.id.i3,bitmap1);
}
//暂停
Intent intent_click=new Intent(context,MyBroadcastReceiver.class);
intent_click.setAction("zanting");
intent_click.putExtra("MESSAGE","暂停");
intent_click.putExtra("typeNotifi",noId);
PendingIntent mPI = PendingIntent.getBroadcast(context,1,intent_click,PendingIntent.FLAG_ONE_SHOT);
contentView.setOnClickPendingIntent(R.id.i1,mPI);
//下一首
Intent intent_click1=new Intent(context,MyBroadcastReceiver.class);
intent_click1.setAction("xiayishou");
intent_click1.putExtra("MESSAGE","下一首");
intent_click1.putExtra("typeNotifi",noId);
PendingIntent mPI1 = PendingIntent.getBroadcast(context,1,intent_click1,PendingIntent.FLAG_ONE_SHOT);
contentView.setOnClickPendingIntent(R.id.i2,mPI1);
//喜欢
Intent intent_click2=new Intent(context,MyBroadcastReceiver.class);
intent_click2.setAction("xihuan");
intent_click2.putExtra("MESSAGE","喜欢");
intent_click2.putExtra("typeNotifi",noId);
PendingIntent mPI2 = PendingIntent.getBroadcast(context,1,intent_click2,PendingIntent.FLAG_ONE_SHOT);
contentView.setOnClickPendingIntent(R.id.i3,mPI2);
//关闭
Intent intent_click3=new Intent(context,MyBroadcastReceiver.class);
intent_click3.setAction("guanbi");
intent_click3.putExtra("MESSAGE","关闭");
intent_click3.putExtra("typeNotifi",noId);
PendingIntent mPI3 = PendingIntent.getBroadcast(context,1,intent_click3,PendingIntent.FLAG_ONE_SHOT);
contentView.setOnClickPendingIntent(R.id.i4,mPI3);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, CHANNEL_ID)
.setSmallIcon(R.drawable.ic_snooze)
.setContentTitle("My notification")
.setContentText("Hello World!")
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
// 设置当用户轻敲通知时将触发的意图
.setContentIntent(pendingIntent)
//用户点击之后消失
.setAutoCancel(true)
.setStyle(decoratedCustomViewStyle)
.setCustomContentView(contentView)
.setCustomBigContentView(contentView1);
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
// notificationId是必须定义的每个通知的唯一int
notificationManager.notify(noId, builder.build());
}
//申请弹出通知的权限
public static void createNotificationChannel(Context context) {
// Create the NotificationChannel, but only on API 26+ because
// the NotificationChannel class is new and not in the support library
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = context.getString(R.string.channel_name);
String description = context.getString(R.string.channel_description);
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
channel.setDescription(description);
// Register the channel with the system; you can't change the importance
// or other notification behaviors after this
NotificationManager notificationManager = context.getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
}
}
2.操作的activity
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.RemoteViews;
import com.example.a_notification.NotificationBtil.NotificationUtil;
public class XiazaiActivity extends AppCompatActivity {
private int noId=1;
private static final String CHANNEL_ID ="通知名字";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_xiazai);
setTitle("测试更新通知");
}
public void download(View v){
//意图 点击通知跳转页面
NotificationUtil.addNotifiion(XiazaiActivity.this,noId,"我是标题1","我是内容1",R.mipmap.dui,"yinyue_notification",0);
}
public void cancel(View v){
NotificationUtil.addNotifiion(XiazaiActivity.this,noId,"我是标题2","我是内容2",R.mipmap.guanbi,"yinyue_notification",0);
}
}
3.notification_large(小的 通知栏布局)
<?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="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center_vertical">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="点击事件测试"
android:id="@+id/b1"
></Button>
<ImageView
android:layout_width="40dp"
android:layout_height="40dp"
android:src="@mipmap/ic_launcher"
android:layout_marginRight="15dp"
android:id="@+id/leftimg"
></ImageView>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="center_vertical"
android:layout_weight="1"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="您知道菜鸟驿站能赚呢个多少钱吗"
android:textSize="15sp"
android:maxLines="1"
android:ellipsize="end"
android:id="@+id/t1"
></TextView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="智慧文学社"
android:textSize="10dp"
android:id="@+id/t2"
></TextView>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:orientation="horizontal"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
>
<ImageView
android:layout_width="15dp"
android:layout_height="15dp"
android:src="@mipmap/zanting"
android:layout_marginRight="20dp"
android:id="@+id/i1"
></ImageView>
<ImageView
android:layout_width="15dp"
android:layout_height="15dp"
android:src="@mipmap/xiayishou"
android:layout_marginRight="20dp"
android:id="@+id/i2"
></ImageView>
<ImageView
android:layout_width="15dp"
android:layout_height="15dp"
android:src="@mipmap/shoucang"
android:layout_marginRight="20dp"
android:id="@+id/i3"
></ImageView>
<ImageView
android:layout_width="15dp"
android:layout_height="15dp"
android:src="@mipmap/guanbi"
android:layout_marginRight="20dp"
android:id="@+id/i4"
></ImageView>
</LinearLayout>
</LinearLayout>
4.notification_small 大的通知栏
<?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="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center_vertical">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="点击事件测试"
android:id="@+id/b1"
></Button>
<ImageView
android:layout_width="40dp"
android:layout_height="40dp"
android:src="@mipmap/ic_launcher"
android:layout_marginRight="15dp"
android:id="@+id/leftimg"
></ImageView>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="center_vertical"
android:layout_weight="1"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="您知道菜鸟驿站能赚呢个多少钱吗"
android:textSize="15sp"
android:maxLines="1"
android:ellipsize="end"
android:id="@+id/t1"
></TextView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="智慧文学社"
android:textSize="10dp"
android:id="@+id/t2"
></TextView>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:orientation="horizontal"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
>
<ImageView
android:layout_width="15dp"
android:layout_height="15dp"
android:src="@mipmap/zanting"
android:layout_marginRight="20dp"
android:id="@+id/i1"
></ImageView>
<ImageView
android:layout_width="15dp"
android:layout_height="15dp"
android:src="@mipmap/xiayishou"
android:layout_marginRight="20dp"
android:id="@+id/i2"
></ImageView>
<ImageView
android:layout_width="15dp"
android:layout_height="15dp"
android:src="@mipmap/shoucang"
android:layout_marginRight="20dp"
android:id="@+id/i3"
></ImageView>
<ImageView
android:layout_width="15dp"
android:layout_height="15dp"
android:src="@mipmap/guanbi"
android:layout_marginRight="20dp"
android:id="@+id/i4"
></ImageView>
</LinearLayout>
</LinearLayout>
5.接受点击事件的广播(MyBroadcastReceiver)
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;
import com.example.a_notification.NotificationBtil.NotificationUtil;
import com.example.a_notification.R;
public class MyBroadcastReceiver extends BroadcastReceiver {
private static final String TAG ="MyBroadcastReceiver" ;
//更新通知栏
private static final String CHANNEL_ID = "id";
private String NotificationBroadcastRece="notificatopnType";
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Log.d(TAG, "actionaction---"+action+"\"触发广播\"");
int type = intent.getIntExtra("typeNotifi", -1);
Log.d(TAG, type+"--typetype---"+action);
if (type != -1) {
Log.d(TAG, "onReceive:type------ +"+type);
}
if (action.equals("aaaa")) {
//处理点击事件
String message = intent.getStringExtra("MESSAGE");
Log.d(TAG, "onReceive: 我是aaaa------"+message);
Toast.makeText(context, "clicked " + message, Toast.LENGTH_LONG).show();
}else
if (action.equals("notification_cancelled")) {
//处理滑动清除和点击删除事件
Toast.makeText(context, "cancelled", Toast.LENGTH_LONG).show();
}else
if (action.equals("yinyue_notification")) {
//音乐播放器的按钮
Toast.makeText(context, "音乐播放器", Toast.LENGTH_LONG).show();
}
else
if (action.equals("zanting")) {
//音乐播放器的按钮
Toast.makeText(context, "暂停", Toast.LENGTH_LONG).show();
}
else
if (action.equals("xiayishou")) {
//音乐播放器的按钮
Toast.makeText(context, "下一首", Toast.LENGTH_LONG).show();
}
else
if (action.equals("xihuan")) {
//音乐播放器的按钮
Toast.makeText(context, "收藏"+type, Toast.LENGTH_LONG).show();
NotificationUtil.addNotifiion(context,type,"我是标题1","我是内容1",R.mipmap.dui,"yinyue_notification",R.mipmap.heart);
}
else
if (action.equals("guanbi")) {
//音乐播放器的按钮
Toast.makeText(context, "关闭", Toast.LENGTH_LONG).show();
}
}
//修改视图
public void updateView(){
}
}