先前由于不知道一些手机系统问题踩了一些坑,现在我分享下我得出的正确结果:
NotificationUtil类
package com.ztk.demo.lockdemo.notification; import android.app.Activity; import android.app.Notification; import android.app.NotificationChannel; import android.app.NotificationManager; import android.app.PendingIntent; import android.app.Service; import android.content.Context; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Color; import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; import android.os.Build; import android.os.IBinder; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.widget.RemoteViews; import android.widget.TextView; import com.ztk.demo.lockdemo.MainActivity; import com.ztk.demo.lockdemo.R; import androidx.annotation.Nullable; import androidx.core.app.NotificationCompat; import java.io.IOException; import java.io.InputStream; /** * @author ztk */ public class NotificationUtil { private NotificationManager notificationManager; private Context mContext; private Notification notification; public static final int NOTIFICATION_ID = 1; public NotificationUtil(Context mContext) { this.mContext = mContext; notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE); } private NotificationCompat.Builder builder; /** * 展示通知栏 */ public void showNotification() { String id = "channel_demo"; notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { Log.d("Logger", "showNotification: 1"); Intent intent = new Intent(mContext, MainActivity.class); PendingIntent pendingIntent = PendingIntent.getActivity(mContext, 0, intent, 0); NotificationChannel mChannel = new NotificationChannel(id, mContext.getString(R.string.app_name), NotificationManager.IMPORTANCE_HIGH); mChannel.setDescription("通知栏"); mChannel.enableLights(false); mChannel.setLightColor(Color.BLUE); // mChannel.enableVibration(false);//振动 mChannel.setSound(null, null);//声音 // mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});//振动 notificationManager.createNotificationChannel(mChannel); builder = new NotificationCompat.Builder(mContext, id); notification = builder .setLargeIcon(BitmapFactory.decodeResource(mContext.getResources(), R.drawable.ic_launcher)) .setSmallIcon(R.drawable.ic_launcher) .setWhen(System.currentTimeMillis()) .setContentIntent(pendingIntent) .setCustomBigContentView(getContentView(false)) .setCustomContentView(getContentView(false)) .setPriority(NotificationCompat.PRIORITY_HIGH) .setTicker("正在播放") .setOngoing(true) .setChannelId(mChannel.getId()) .build(); } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { Log.d("Logger", "showNotification: 2"); notification = new NotificationCompat.Builder(mContext, id) .setWhen(System.currentTimeMillis()) .setSmallIcon(R.mipmap.ic_launcher) .setContentIntent(getDefaultIntent(Notification.FLAG_ONGOING_EVENT)) .setCustomBigContentView(getContentView(true)) .setCustomContentView(getContentView(false)) .setPriority(NotificationCompat.PRIORITY_HIGH) .setTicker("正在播放") .setOngoing(true) .build(); } else { Log.d("Logger", "showNotification: 3"); notification = new NotificationCompat.Builder(mContext, id) .setWhen(System.currentTimeMillis()) .setSmallIcon(R.mipmap.ic_launcher) .setContentIntent(getDefaultIntent(Notification.FLAG_ONGOING_EVENT)) .setContent(getContentView(false)) .setPriority(NotificationCompat.PRIORITY_HIGH) .setTicker("正在播放") .setOngoing(true) .build(); } //前台服务必须加这句 notificationManager.notify(NOTIFICATION_ID, notification); } private PendingIntent getDefaultIntent(int flags) { return PendingIntent.getActivity(mContext, 1, new Intent(), flags); } /** * 获取自定义通知栏view * * @param showBigView * @return */ private RemoteViews getContentView(boolean showBigView) { mRemoteViews = new RemoteViews(mContext.getPackageName(), showBigView ? R.layout.view_notify_big : R.layout.view_notify_small); return mRemoteViews; } private RemoteViews mRemoteViews; public void refreshUI(String msg,String msg2) { mRemoteViews.setTextViewText(R.id.tv_custom_song_singer,msg); mRemoteViews.setTextViewText(R.id.tv_custom_song_name,msg2); notification = builder .setCustomBigContentView(mRemoteViews) .setCustomContentView(mRemoteViews) .build(); notificationManager.notify(NOTIFICATION_ID, notification); // notification = builder // .setContentTitle(R.id.xx,"aaaa") // .setContentText(R.id.xxxx,"bbbbbb") // .build(); // notificationManager.notify(NOTIFICATION_ID, notification); } }
自定义通知栏布局view_notify_small.xml
<?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="64dp" android:orientation="horizontal"> <ImageView android:id="@+id/custom_song_icon" android:layout_width="64dp" android:layout_height="64dp" android:layout_gravity="center_vertical" android:src="@mipmap/ic_launcher" android:scaleType="centerCrop" /> <RelativeLayout android:layout_width="0.0dp" android:layout_height="match_parent" android:layout_weight="1.0" android:background="#ffffff"> <LinearLayout android:id="@+id/ll_custom_button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_alignParentLeft="true" android:layout_toLeftOf="@+id/ll_custom_button" android:layout_marginLeft="12dp" android:layout_marginRight="12dp" android:orientation="vertical"> <TextView android:id="@+id/tv_custom_song_singer" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_toLeftOf="@+id/btn_custom_close" android:ellipsize="end" android:lines="1" android:singleLine="true" android:text="标题" android:textColor="#333437" android:textSize="15sp" android:textStyle="normal" /> <TextView android:id="@+id/tv_custom_song_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="4dp" android:ellipsize="end" android:fadingEdge="horizontal" android:lines="1" android:singleLine="true" android:text="内容" android:textColor="#999999" android:textSize="12sp" /> </LinearLayout> <LinearLayout android:id="@+id/ll_custom_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_marginRight="4dp" android:layout_centerVertical="true" android:gravity="center_vertical" android:orientation="horizontal"> <ImageView android:id="@+id/btn_custom_play" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:padding="8dp" android:src="@mipmap/go_next" /> </LinearLayout> </RelativeLayout> </LinearLayout>
当我们需要实时刷新通知栏内容时可调用
refreshUI(info1,info2)方法实现
注意:由于一开始没有调用notificationManager.notify(NOTIFICATION_ID, notification);刷新 导致一直无法实现动态更改内容,费了不少时间