1、实现自定义通知样式,本文基于极光推送,核心处理逻辑,不区分具体推送平台
2、通知类型判断:
根据极光通知类型判断(自定义消息,极光本身是不支持通知展示回复、点赞等功能)
if(JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())){
//极光自定义消息,服务端传递给极光自定义消息内容
String message = bundle.getString(JPushInterface.EXTRA_MESSAGE);
//极光通知消息Id,后面创建消息notifyId(消息唯一性)
String msgId = bundle.getString(JPushInterface.EXTRA_MSG_ID);
//极光通知类型数据,自定义消息则没有值
String extra = bundle.getString(JPushInterface.EXTRA_EXTRA);
}
3、判断应用通知权限
/**
* 判断通知权限是否开启
* @return true,开启通知权限 false:未开启通知权限
*/
public static boolean hasNotifyPermission(Context context) {
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.O) {
NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
if (manager != null) {
return manager.areNotificationsEnabled();
}
} else {
NotificationManagerCompat manager = NotificationManagerCompat.from(context);
if (manager != null) {
return manager.areNotificationsEnabled();
}
}
return true;
}
4、根据自定义消息message获取相关字段(用户头像、用户发送内容大图、多文字、回复、点赞等)
1、获取头像Bitmap
/**
* 获取头像图片bitmap
*
* @param context
* @param userImageUrl
* @param bigImageUrl
* @param glideLoadCallBack
*/
private static void notificationBitmap(Context context, String userImageUrl, String bigImageUrl, GlideLoadCallBack glideLoadCallBack) {
if (context != null) {
if (userImageUrl != null && userImageUrl.length() > 0) {
Glide.with(context).asBitmap().load(userImageUrl).addListener(new RequestListener<Bitmap>() {
@Override
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Bitmap> target, boolean isFirstResource) {
notificationBigBitmap(context, null, bigImageUrl, glideLoadCallBack);
return false;
}
@Override
public boolean onResourceReady(Bitmap resource, Object model, Target<Bitmap> target, DataSource dataSource, boolean isFirstResource) {
notificationBigBitmap(context, resource, bigImageUrl, glideLoadCallBack);
return false;
}
}).submit(60, 60);
} else {
notificationBigBitmap(context, null, bigImageUrl, glideLoadCallBack);
}
} else if (glideLoadCallBack != null) {
if (glideLoadCallBack != null) {
glideLoadCallBack.onLoadBitmap(null, null);
}
}
}
2、获取大图Bitmap
/**
* 获取大图 bitmap
*
* @param context
* @param userBitmap
* @param bigImageUrl
* @param glideLoadCallBack
*/
private static void notificationBigBitmap(Context context, Bitmap userBitmap, String bigImageUrl, GlideLoadCallBack glideLoadCallBack) {
if (context != null) {
if (bigImageUrl != null && bigImageUrl.length() > 0) {
Glide.with(context).asBitmap().load(bigImageUrl).addListener(new RequestListener<Bitmap>() {
@Override
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Bitmap> target, boolean isFirstResource) {
if (glideLoadCallBack != null) {
glideLoadCallBack.onLoadBitmap(userBitmap, null);
}
return false;
}
@Override
public boolean onResourceReady(Bitmap resource, Object model, Target<Bitmap> target, DataSource dataSource, boolean isFirstResource) {
if (glideLoadCallBack != null) {
glideLoadCallBack.onLoadBitmap(userBitmap, resource);
}
return false;
}
}).submit(100, 300);
} else if (glideLoadCallBack != null) {
if (glideLoadCallBack != null) {
glideLoadCallBack.onLoadBitmap(userBitmap, null);
}
}
}
}
3、图片加载回调
public interface GlideLoadCallBack {
void onLoadBitmap(Bitmap userBitmap, Bitmap bigBitmap);
}
5、创建通知(重点)
/**
* 创建通知
*
* @param context
* @param pushBean
* @param bigBitmap
*/
public static void createNotification(Context context,Bitmap userBitmap, Bitmap bigBitmap, String msgId, String msg) {
NotificationCompat.Builder newMessageNotification = new NotificationCompat.Builder(context, CHANNEL_ONE_ID);
//输入框action
RemoteInput remoteInput = new RemoteInput.Builder(PushJobIntentService.INPUT_TEXT).setLabel("请输入内容").build();
PendingIntent replyIntent = customNotificationByClickButton(context, pushBean, 1);
NotificationCompat.Action remoteInputBuilder = new NotificationCompat.Action.Builder(R.mipmap.comment_icon, "回复", replyIntent).addRemoteInput(remoteInput).build();
newMessageNotification.addAction(remoteInputBuilder);
//点赞
PendingIntent likeIntent = customNotificationByClickButton(context, pushBean, 2);
NotificationCompat.Action likeBuilder = new NotificationCompat.Action.Builder(R.mipmap.comment_like_icon, "点赞", likeIntent).build();
newMessageNotification.addAction(likeBuilder);
//大图
notificationBigPicture(newMessageNotification, userBitmap, bigBitmap, pushBean.getPushContent());
// Issue the notification.
showNotification(context, pushBean.getPushNotifyId(), newMessageNotification, bigBitmap, pushBean);
}
/**
* 点击事件,对应 pendingIntent
*
* @param context
* @param pushBean
* @param clickType 1:回复or评论 2:点赞
* @return
*/
public static PendingIntent customNotificationByClickButton(Context context, PushBean pushBean, int clickType) {
if (pushBean != null) {
Intent intent = new Intent(context, PushJobIntentService.class);
intent.putExtra("自定义消息数据", "自定义消息数据");
return PendingIntent.getService(context, randomNextInt(), intent, PendingIntent.FLAG_CANCEL_CURRENT);
}
return null;
}
/**
* 添加大图片 BigPictureStyle
*
* @param newMessageNotification
* @param bigBitmap
*/
private static void notificationBigPicture(NotificationCompat.Builder newMessageNotification, Bitmap userBitmap, Bitmap bigBitmap, String bigText) {
if (newMessageNotification != null) {
if (bigBitmap != null) {
newMessageNotification.setStyle(new NotificationCompat.BigPictureStyle()
.bigPicture(bigBitmap)
.bigLargeIcon(userBitmap));
} else if (bigText != null && bigText.length() > 0) {
notificationBigText(newMessageNotification, bigText);
}
}
}
/**
* 添加 多文字 BigTextStyle
*
* @param newMessageNotification
* @param bigText
*/
private static void notificationBigText(NotificationCompat.Builder newMessageNotification, String bigText) {
if (bigText != null && newMessageNotification != null) {
newMessageNotification.setStyle(new NotificationCompat.BigTextStyle().bigText(bigText));
}
}
/**
* 显示通知
*
* @param context
* @param notifyId
*/
@SuppressLint("ResourceAsColor")
public static void showNotification(Context context, int notifyId, NotificationCompat.Builder notificationBuilder, Bitmap bigBitmap, PushBean pushBean) {
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
PendingIntent startActivityIntent = customNotificationByClickStartActivity(context, CommonStartActivityUtils.startActivityByActivityOpen(context, pushBean));
if (startActivityIntent != null) {
notificationBuilder.setContentIntent(startActivityIntent);
notificationBuilder.setContentTitle(pushBean.getPushTitle() + "");
notificationBuilder.setContentText(pushBean.getPushContent() + "");
//设置通知的小图标
// if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && userBitmap != null) {
// IconCompat withBitmap = IconCompat.createWithBitmap(userBitmap);
// notificationBuilder.setSmallIcon(withBitmap);
// } else {
// //设置右侧头像
if (bigBitmap != null)
notificationBuilder.setLargeIcon(bigBitmap);
notificationBuilder.setSmallIcon(R.drawable.common_app_icon);
// }
//设置点击消失
notificationBuilder.setAutoCancel(true);
//铃声、闪光、震动均系统默认
notificationBuilder.setDefaults(NotificationCompat.DEFAULT_ALL);
//设置级别
notificationBuilder.setPriority(NotificationCompat.PRIORITY_LOW);
notificationBuilder.setChannelId(CHANNEL_ONE_ID);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// Register the channel with the system. You can't change the importance
// or other notification behaviors after this.
NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ONE_ID, "自定义通知", NotificationManager.IMPORTANCE_LOW);
notificationChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);//锁屏显示全部通知
notificationChannel.setName("自定义通知");
notificationChannel.setDescription("接收通知需要您开启应用通知权限");
notificationManager.createNotificationChannel(notificationChannel);
}
try {
notificationManager.notify(notifyId, notificationBuilder.build());
} catch (Exception e) {
e.printStackTrace();
}
}
}
6、PushJobIntentService处理相关逻辑
public class PushJobIntentService extends IntentService {
public PushJobIntentService() {
super("push_click");
}
@Override
protected void onHandleIntent(@Nullable Intent intent) {
if (intent != null) {
Bundle remoteInput = RemoteInput.getResultsFromIntent(intent);
//判断是否有输入内容
if (remoteInput != null) {
CharSequence charSequence = remoteInput.getCharSequence("INPUT_TEXT");
//获取输入内容
//处理相关逻辑,发送回复网络请求
}
//移除通知栏这条通知
cancelNotification(notifyId, context);
}
}
@Override
public void onDestroy() {
super.onDestroy();
}
}
7、当用户操作通知后,移除通知栏通知
/**
* 清除id
*
* @param notifyId
* @param context
*/
public static void cancelNotification(int notifyId, Context context) {
if (context != null) {
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
notificationManager.cancel(notifyId);
}
}