Using Mixpanel I am able to send out notifications directly from their control panel, however currently it uses a weirdly cropped version of the launcher icon as the notification icon.
I have seen some answers for customising the icon using a customised BroadcastReceiver, but I can't seem to get it to working in this case. Has anyone successfully managed to change the notification icon when sending directly from Mixpanel?
Thanks.
Actually, there is a way to set a custom icon for android push notifications without writing your own custom broadcast receiver. Recent versions of the Mixpanel android library understand a "mp_icnm" parameter that can refer to the name of a resource in your application. The library itself is also packaged with a set of predefined icons you can use. The quick way is to put the following snippet into the "custom data" field
{"mp_icnm":"com_mixpanel_android_ic_megaphone"}
I've attached a screenshot of the Mixpanel app with a picture of the text field. You'll want to be sure that this data is entered in "Android" preview mode when you enter your data, as shown in the illustration.

You can use any drawable resource in your app for an icon- the whole list of prepackaged notification icons can be found here in the Mixpanel library, and their resource names are listed below.
Be aware that the Mixpanel resources might be removed by your proguard configuration, so you'll want to be sure that you haven't stripped them if you want to use them.
To answer one aspect of @user1544797's question and in addition to @user128536's answer, you may want to let your app be responsible of configuring your notification icon and not rely on the Mixpanel preview mode. To do so, you must intercept Mixpanel broadcast by creating your own BroadcastReceiver that extends Mixpanel's GCMReceiver :
public class MixpanelGCMReceiver extends GCMReceiver {
@Override
public void onReceive(Context context, Intent intent) {
intent.putExtra("mp_icnm", "<your_icon_name>");
super.onReceive(context, intent);
}
}
Then declare your BroadcastReceiver in your AndroidManifest.xml file :
<receiver
android:name="<your_package_name>.MixpanelGCMReceiver"
android:permission="com.google.android.c2dm.permission.SEND">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="${applicationId}" />
</intent-filter>
</receiver>
Finally, @user128536 is right about warning you that Proguard is messing up with your notification icon if not managed properly (and unfortunately Mixpanel does not document how your app should handle this case). However, in addition to Proguard, there is another issue that you may need to tackle when you are using an applicationId that differs from your packageName (usually while using product flavors). In the ResourceReader class from Mixpanel SDK, you can see this comment :
MPLog.w(LOGTAG, "Can't load names for Android view ids from '" + localClassName + "', ids by name will not be available in the events editor.");
MPLog.i(LOGTAG, "You may be missing a Resources class for your package due to your proguard configuration, " +
"or you may be using an applicationId in your build that isn't the same as the package declared in your AndroidManifest.xml file.\n" +
"If you're using proguard, you can fix this issue by adding the following to your proguard configuration:\n\n" +
"-keep class **.R$* {\n" +
" <fields>;\n" +
"}\n\n" +
"If you're not using proguard, or if your proguard configuration already contains the directive above, " +
"you can add the following to your AndroidManifest.xml file to explicitly point the Mixpanel library to " +
"the appropriate library for your resources class:\n\n" +
"<meta-data android:name=\"com.mixpanel.android.MPConfig.ResourcePackageName\" android:value=\"YOUR_PACKAGE_NAME\" />\n\n" +
"where YOUR_PACKAGE_NAME is the same string you use for the \"package\" attribute in your <manifest> tag."
);
As indicated in the above comment, if you find yourself in this situation, just add in your AndroidManifest.xml file the following block :
<meta-data
android:name="com.mixpanel.android.MPConfig.ResourcePackageName"
android:value="<your_package_name>" />
That's it, you should be done by now ;)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With