pdlivexp/app/src/main/java/com/shayu/phonelive/utils/CustomMessageReceiver.java
2023-11-11 09:59:01 +08:00

330 lines
16 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package com.shayu.phonelive.utils;
import static android.content.Intent.FLAG_ACTIVITY_CLEAR_TOP;
import android.Manifest;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.app.Notification;
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.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.net.Uri;
import android.os.Build;
import android.util.Log;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RemoteViews;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.app.NotificationCompat;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.target.Target;
import com.bumptech.glide.request.transition.Transition;
import com.makeramen.roundedimageview.RoundedImageView;
import com.shayu.phonelive.AppContext;
import com.shayu.phonelive.activity.LauncherActivity;
import com.yunbao.common.bean.NotificationMsgBean;
import com.yunbao.common.glide.ImgLoader;
import com.yunbao.common.utils.DialogUitl;
import com.yunbao.common.utils.SpUtil;
import com.yunbao.common.utils.WordUtil;
import com.yunbao.main.activity.MainActivity;
import org.json.JSONException;
import org.json.JSONObject;
import io.rong.imlib.ConnectChangeReceiver;
import io.rong.push.PushType;
import io.rong.push.notification.PushMessageReceiver;
import io.rong.push.notification.PushNotificationMessage;
import myname.pdlive.shayu.R;
public class CustomMessageReceiver extends PushMessageReceiver {
private String createNotificationChannel(Context context, String channelID, String channelNAME, int level) {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
NotificationManager manager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
NotificationChannel channel = new NotificationChannel(channelID, channelNAME, level);
channel.setSound(Uri.parse("android.resource://" + context.getPackageName() + "/raw/pdlive_sound"), null);
manager.createNotificationChannel(channel);
return channelID;
} else {
return null;
}
}
@Override
public boolean onNotificationMessageClicked(
Context context, PushType pushType, PushNotificationMessage message) {
if (pushType.equals(PushType.GOOGLE_FCM)) {
Log.i("gmc", "gmc");
// TODO
} else {
Log.i("gmc", "gmc1111");
}
// 返回 true 表示拦截false 为不拦截
return true;
}
NotificationMsgBean msg = new NotificationMsgBean();
@Override
public boolean onNotificationMessageArrived(Context context, PushType pushType, PushNotificationMessage notificationMessage) {
Log.i("gmc", notificationMessage.getPushContent() + "VVV" + notificationMessage.getPushTitle() + "gmc11112222" + notificationMessage.getExtra());
if (!SpUtil.getInstance().getBooleanValue("NOTIFICATION")) {
if (AppContext.activityWeakReference != null) {
Activity activity = AppContext.activityWeakReference.get();
if (activity != null) {
DialogUitl.showSimpleDialog(activity, WordUtil.isNewZh()?"应用需要通知权限":"Application requires notification permission", new DialogUitl.SimpleCallback() {
@Override
public void onConfirmClick(Dialog dialog, String content) {
Intent intent = new Intent();
intent.setAction("android.settings.APP_NOTIFICATION_SETTINGS");
intent.putExtra("app_package", context.getPackageName());
intent.putExtra("app_uid", context.getApplicationInfo().uid);
// for Android 8 and above
intent.putExtra("android.provider.extra.APP_PACKAGE", context.getPackageName());
activity.startActivity(intent);
}
});
}
}
}
if (notificationMessage.getExtra() == null) {
msg.setImg("" + notificationMessage.getSenderPortrait());
msg.setTitle(notificationMessage.getPushTitle());
msg.setContent(notificationMessage.getPushContent());
msg.setType("-1");
sendNotification(context, msg);
} else {
try {
JSONObject obj = new JSONObject(notificationMessage.getExtra());
Log.i("gmc", "1111222");
//开播
if (obj.getString("type").equals("1") || obj.getString("type").equals("9")) {
Log.i("gmc", "1111");
msg.setImg("" + obj.getString("avatar"));
msg.setTitle(notificationMessage.getPushTitle());
msg.setContent(notificationMessage.getPushContent());
msg.setLiveuid(obj.getString("liveuid"));
msg.setType("1");
liveNotification(context, msg);
//发布动态 2
//点赞 3
//评论 4
//鼓励 5
} else if (obj.getString("type").equals("2") || obj.getString("type").equals("3") || obj.getString("type").equals("4") || obj.getString("type").equals("5")) {
msg.setImg("" + obj.getString("avatar"));
msg.setTitle(notificationMessage.getPushTitle());
msg.setContent(notificationMessage.getPushContent());
msg.setType("2");
dynamicNotification(context, msg);
} else if (obj.getString("type").equals("8") || obj.getString("type").equals("7")) {
msg.setImg("" + obj.getString("avatar"));
msg.setTitle(notificationMessage.getPushTitle());
msg.setContent(notificationMessage.getPushContent());
msg.setType(obj.getString("type"));
msg.setActivityurl(obj.getString("activityUrl"));
serviceNotification(context, msg);
}
} catch (JSONException e) {
Log.i("gmc", "1111" + e.getMessage());
e.printStackTrace();
}
}
return true;
}
public static Bitmap makeRoundCorner(Bitmap bitmap, float roundPx) {
try {
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
bitmap.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(output);
final int color = 0xff424242;// 颜色值0xff---alpha
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
final RectF rectF = new RectF(rect);// Rect是使用int类型作为数值RectF是使用float类型作为数值
// --------抗锯齿-------//
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
final Rect src = new Rect(0, 0, bitmap.getWidth(),
bitmap.getHeight());
canvas.drawBitmap(bitmap, null, rect, paint);
return output;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
//私聊
private void sendNotification(Context context, NotificationMsgBean message) {
// 设置通知的点击行为:这里启动一个 Activity
Intent intent = new Intent(context, MainActivity.class);
intent.setFlags(FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.S) {
pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_IMMUTABLE);
} else {
pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_ONE_SHOT);
}
String channelId = createNotificationChannel(context, "my_channel_ID", "my_channel_NAME", NotificationManager.IMPORTANCE_HIGH);
// 构建 remoteView
RemoteViews remoteView = new RemoteViews(context.getPackageName(), R.layout.notification_message);
remoteView.setTextViewText(R.id.title, message.getTitle());
remoteView.setTextViewText(R.id.content, message.getContent());
Glide.with(context).asBitmap().load(message.getImg()).into(new SimpleTarget<Bitmap>() {
@Override
public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
remoteView.setImageViewBitmap(R.id.img, makeRoundCorner(resource, 50));
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, channelId);
// 设置自定义 RemoteViews
builder.setContent(remoteView).setSmallIcon(R.drawable.small_icons);
// 设置通知的优先级(悬浮通知)
builder.setPriority(NotificationCompat.PRIORITY_MAX);
builder.setContentIntent(pendingIntent);
builder.setAutoCancel(true);
Notification notification = builder.build();
NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(0, notification);
}
});
}
int id = 1;
//动态
private void dynamicNotification(Context context, NotificationMsgBean message) {
// 设置通知的点击行为:这里启动一个 Activity
Intent intent = new Intent(context, MainActivity.class);
intent.putExtra("type", "2");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.S) {
pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_IMMUTABLE);
} else {
pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}
String channelId = createNotificationChannel(context, "my_channel_ID", "my_channel_NAME", NotificationManager.IMPORTANCE_HIGH);
// 构建 remoteView
RemoteViews remoteView = new RemoteViews(context.getPackageName(), R.layout.notification_message);
remoteView.setTextViewText(R.id.title, message.getTitle());
remoteView.setTextViewText(R.id.content, message.getContent());
remoteView.setImageViewResource(R.id.img, R.mipmap.ic_launcher);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, channelId);
// 设置自定义 RemoteViews
builder.setContent(remoteView).setSmallIcon(R.drawable.small_icons);
// 设置通知的优先级(悬浮通知)
builder.setPriority(NotificationCompat.PRIORITY_MAX);
builder.setContentIntent(pendingIntent);
builder.setAutoCancel(true);
Notification notification = builder.build();
NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
id = (int) (id + System.currentTimeMillis());
manager.notify(id, notification);
}
//开播
private void liveNotification(Context context, NotificationMsgBean message) {
// 设置通知的点击行为:这里启动一个 Activity
Intent intent = new Intent(context, LauncherActivity.class);
intent.setFlags(FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("liveid", message.getLiveuid());
Log.e("gmc", message.getLiveuid() + "21121");
PendingIntent pendingIntent;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.S) {
pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_IMMUTABLE);
} else {
pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}
String channelId = createNotificationChannel(context, "my_channel_ID", "my_channel_NAME", NotificationManager.IMPORTANCE_HIGH);
// 构建 remoteView
RemoteViews remoteView = new RemoteViews(context.getPackageName(), R.layout.notification_message);
remoteView.setTextViewText(R.id.title, message.getTitle());
remoteView.setTextViewText(R.id.content, message.getContent());
Glide.with(context).asBitmap().load(message.getImg()).into(new SimpleTarget<Bitmap>() {
@Override
public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
remoteView.setImageViewBitmap(R.id.img, makeRoundCorner(resource, 50));
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, channelId);
// 设置自定义 RemoteViews
builder.setContent(remoteView).setSmallIcon(R.drawable.small_icons);
// 设置通知的优先级(悬浮通知)
builder.setPriority(NotificationCompat.PRIORITY_MAX);
builder.setContentIntent(pendingIntent);
builder.setAutoCancel(true);
Notification notification = builder.build();
NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
id = (int) (id + System.currentTimeMillis());
manager.notify(id, notification);
}
});
Log.i("gmc", "11123231");
}
//后台
private void serviceNotification(Context context, NotificationMsgBean message) {
// 设置通知的点击行为:这里启动一个 Activity
Intent intent = new Intent(context, MainActivity.class);
intent.putExtra("type", message.getType());
if (message.getType().equals("8")) {
intent.putExtra("activityUrl", message.getActivityurl());
}
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.S) {
pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_IMMUTABLE);
} else {
pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}
String channelId = createNotificationChannel(context, "my_channel_ID", "my_channel_NAME", NotificationManager.IMPORTANCE_HIGH);
// 构建 remoteView
RemoteViews remoteView = new RemoteViews(context.getPackageName(), R.layout.notification_message);
remoteView.setTextViewText(R.id.title, message.getTitle());
remoteView.setTextViewText(R.id.content, message.getContent());
remoteView.setImageViewResource(R.id.img, R.mipmap.ic_launcher);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, channelId);
// 设置自定义 RemoteViews
builder.setContent(remoteView).setSmallIcon(R.drawable.small_icons);
// 设置通知的优先级(悬浮通知)
builder.setPriority(NotificationCompat.PRIORITY_MAX);
builder.setContentIntent(pendingIntent);
builder.setAutoCancel(true);
Notification notification = builder.build();
NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
id = (int) (id + System.currentTimeMillis());
manager.notify(id, notification);
}
}