davide-scalzo / react-native-mixpanel

A React Native wrapper for Mixpanel tracking
MIT License
455 stars 195 forks source link

Android push notification icon #229

Closed SMJ93 closed 4 years ago

SMJ93 commented 4 years ago

Hey, first of all, great library!! It's been really easy to setup :)

Is there a way to set a default icon / icon colour for the mix panel push notification icon like with Firebase?

As you can see in the screenshot below it just shows a grey circle instead of the app icon:

Image from iOS

SMJ93 commented 4 years ago

After speaking to Mixpanel support they said to create a subclass that adds in the default icon and colour. See these release notes for more details.

This didn't work for me as I am using both Firebase and Mixpanel. As a workaround I set the intent extras mp_icnm and mp_color, before calling MixpanelFCMMessagingService.showPushNotification(getApplicationContext(), intent);.

Here is my full PushNotificationService class incase someone else encounters a similar problem:

package com.myproject;

import io.invertase.firebase.messaging.RNFirebaseMessagingService;

import com.google.firebase.messaging.RemoteMessage;

import com.mixpanel.android.mpmetrics.MixpanelFCMMessagingService;

import android.util.Log;
import android.content.Intent;

import java.lang.Override;
import java.lang.String;

public class PushNotificationService extends RNFirebaseMessagingService {
  private static final String TAG = "PushNotificationService";

  @Override
  public void onNewToken(String newToken) {
    super.onNewToken(newToken);
    Log.d(TAG, "newToken: " + newToken);

    MixpanelFCMMessagingService.addToken(newToken);
  }

  @Override
  public void onMessageReceived(RemoteMessage remoteMessage) {
    super.onMessageReceived(remoteMessage);
    Log.d(TAG, "onMessageReceived");

    if (remoteMessage.getData().containsKey( "mp_message")) {
      Log.d(TAG, "onMessageReceived hasMpMessage");

      // Set default mixpanel push notification icon and colour
      Intent intent = remoteMessage.toIntent();
      intent.putExtra("mp_icnm", "ic_notification");
      intent.putExtra("mp_color", getResources().getString(R.color.notification));

      MixpanelFCMMessagingService.showPushNotification(getApplicationContext(), intent);
    }
  }
}