From 8186c2a02506023264ec434ac4a19244ef2004fb Mon Sep 17 00:00:00 2001 From: mchuangatmp Date: Thu, 3 Nov 2022 14:01:45 -0700 Subject: [PATCH] fix: fix pendingIntent issue --- .../java/com/mparticle/MPServiceUtil.java | 20 +++++++++++++------ .../messaging/ProviderCloudMessage.java | 15 +++++++++++--- 2 files changed, 26 insertions(+), 9 deletions(-) diff --git a/android-core/src/main/java/com/mparticle/MPServiceUtil.java b/android-core/src/main/java/com/mparticle/MPServiceUtil.java index e17f65be9..17c93be59 100644 --- a/android-core/src/main/java/com/mparticle/MPServiceUtil.java +++ b/android-core/src/main/java/com/mparticle/MPServiceUtil.java @@ -1,5 +1,7 @@ package com.mparticle; +import static android.content.Context.NOTIFICATION_SERVICE; + import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; @@ -9,19 +11,18 @@ import android.os.Handler; import android.os.Looper; import android.os.PowerManager; + import androidx.annotation.RequiresApi; import androidx.localbroadcastmanager.content.LocalBroadcastManager; import com.mparticle.internal.ConfigManager; import com.mparticle.internal.Constants; +import com.mparticle.internal.KitsLoadedListener; import com.mparticle.internal.KitsLoadedListenerConfiguration; import com.mparticle.internal.Logger; -import com.mparticle.internal.KitsLoadedListener; import com.mparticle.messaging.MPMessagingAPI; import com.mparticle.messaging.ProviderCloudMessage; -import static android.content.Context.NOTIFICATION_SERVICE; - public class MPServiceUtil { public static final String NOTIFICATION_CHANNEL = "com.mparticle.default"; public static final String INTERNAL_NOTIFICATION_TAP = "com.mparticle.push.notification_tapped"; @@ -136,10 +137,17 @@ private void handleNotificationTap(Intent intent) { Intent pe = message.getDefaultOpenIntent(mContext, message); pe.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); pe.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); - PendingIntent actionIntent = PendingIntent.getActivity(mContext, 0, pe, PendingIntent.FLAG_UPDATE_CURRENT); - if (actionIntent != null) { + + PendingIntent pendingIntent; + if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.S) { + pendingIntent = PendingIntent.getActivity + (mContext, 0, pe, PendingIntent.FLAG_MUTABLE); + } else { + pendingIntent = PendingIntent.getActivity(mContext, 0, pe, PendingIntent.FLAG_UPDATE_CURRENT); + } + if (pendingIntent != null) { try { - actionIntent.send(); + pendingIntent.send(); } catch (PendingIntent.CanceledException e) { } diff --git a/android-core/src/main/java/com/mparticle/messaging/ProviderCloudMessage.java b/android-core/src/main/java/com/mparticle/messaging/ProviderCloudMessage.java index 8a2361bac..f0ca8041a 100644 --- a/android-core/src/main/java/com/mparticle/messaging/ProviderCloudMessage.java +++ b/android-core/src/main/java/com/mparticle/messaging/ProviderCloudMessage.java @@ -1,5 +1,7 @@ package com.mparticle.messaging; +import static com.mparticle.MPServiceUtil.NOTIFICATION_CHANNEL; + import android.app.Notification; import android.app.PendingIntent; import android.content.Context; @@ -11,6 +13,7 @@ import android.os.Bundle; import android.os.Parcel; import android.os.Parcelable; + import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.core.app.NotificationCompat; @@ -25,8 +28,6 @@ import java.util.Set; -import static com.mparticle.MPServiceUtil.NOTIFICATION_CHANNEL; - /** * Representation of an FCM/push sent by a 3rd party such as Urban Airship or Mixpanel. */ @@ -150,7 +151,15 @@ protected static PendingIntent getLoopbackIntent(Context context, ProviderCloudM intent.setClass(context, MPService.class); intent.putExtra(MPMessagingAPI.CLOUD_MESSAGE_EXTRA, message); - return PendingIntent.getService(context, id.hashCode(), intent, PendingIntent.FLAG_UPDATE_CURRENT); + PendingIntent pendingIntent; + if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.S) { + pendingIntent = PendingIntent.getService + (context, id.hashCode(), intent, PendingIntent.FLAG_MUTABLE); + } else { + pendingIntent = PendingIntent.getService(context, id.hashCode(), intent, PendingIntent.FLAG_UPDATE_CURRENT); + } + + return pendingIntent; } @Nullable