From 6fb29bbf602fc3dd708aeb360758b3a11991e1fe Mon Sep 17 00:00:00 2001 From: zlzw <583819556@qq.com> Date: Mon, 28 Nov 2022 15:27:28 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=8E=A8=E9=80=81=E9=80=9A?= =?UTF-8?q?=E7=9F=A5=E6=9D=83=E9=99=90=E6=8F=90=E7=A4=BA=E6=A1=86=E9=9D=9E?= =?UTF-8?q?activity=E5=AF=BC=E8=87=B4=E7=9A=84=E9=97=AA=E5=9B=BE=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../utils/CustomMessageReceiver.java | 39 +++++++++++-------- 1 file changed, 22 insertions(+), 17 deletions(-) diff --git a/app/src/main/java/com/shayu/phonelive/utils/CustomMessageReceiver.java b/app/src/main/java/com/shayu/phonelive/utils/CustomMessageReceiver.java index 48fc30fd8..844c12b45 100644 --- a/app/src/main/java/com/shayu/phonelive/utils/CustomMessageReceiver.java +++ b/app/src/main/java/com/shayu/phonelive/utils/CustomMessageReceiver.java @@ -4,6 +4,7 @@ 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; @@ -34,6 +35,7 @@ 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; @@ -57,7 +59,7 @@ public class CustomMessageReceiver extends PushMessageReceiver { 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); + channel.setSound(Uri.parse("android.resource://" + context.getPackageName() + "/raw/pdlive_sound"), null); manager.createNotificationChannel(channel); return channelID; } else { @@ -86,19 +88,22 @@ public class CustomMessageReceiver extends PushMessageReceiver { @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")){ - DialogUitl.showSimpleDialog(context, "应用需要通知权限", 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()); - context.startActivity(intent); - } - }); + if (!SpUtil.getInstance().getBooleanValue("NOTIFICATION")) { + Activity activity = AppContext.activityWeakReference.get(); + if (activity != null) { + DialogUitl.showSimpleDialog(activity, "应用需要通知权限", 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()); @@ -218,7 +223,7 @@ public class CustomMessageReceiver extends PushMessageReceiver { 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 { + } else { pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); } String channelId = createNotificationChannel(context, "my_channel_ID", "my_channel_NAME", NotificationManager.IMPORTANCE_HIGH); @@ -252,7 +257,7 @@ public class CustomMessageReceiver extends PushMessageReceiver { 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 { + } else { pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); } String channelId = createNotificationChannel(context, "my_channel_ID", "my_channel_NAME", NotificationManager.IMPORTANCE_HIGH); @@ -295,7 +300,7 @@ public class CustomMessageReceiver extends PushMessageReceiver { 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 { + } else { pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); } String channelId = createNotificationChannel(context, "my_channel_ID", "my_channel_NAME", NotificationManager.IMPORTANCE_HIGH);