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);