Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: mParticle crashing on push notifications for android 12 and above. #266

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions android-core/src/main/java/com/mparticle/MPServiceUtil.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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";
Expand Down Expand Up @@ -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) {

}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand All @@ -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.
*/
Expand Down Expand Up @@ -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
Expand Down