使用Mixpanel自定义Android的通知图标

时间:2015-02-04 12:21:14

标签: android broadcastreceiver mixpanel

使用Mixpanel我可以直接从他们的控制面板发送通知,但是目前它使用了一个奇怪的裁剪版本的启动器图标作为通知图标。

我已经看到了使用自定义的BroadcastReceiver自定义图标的一些答案,但我似乎无法在这种情况下使用它。有没有人成功设法在直接从Mixpanel发送时更改通知图标?

感谢。

4 个答案:

答案 0 :(得分:5)

实际上,有一种方法可以为Android推送通知设置自定义图标,而无需编写自己的自定义广播接收器。 Mixpanel安卓库的最新版本了解" mp_icnm"可以引用应用程序中资源名称的参数。库本身也包含一组您可以使用的预定义图标。快速的方法是将以下代码段放入"自定义数据"字段

{"mp_icnm":"com_mixpanel_android_ic_megaphone"}

我附上了Mixpanel应用的截图,上面有文字字段的图片。您需要确保在" Android"中输入此数据。输入数据时的预览模式,如图所示。

Illustration of custom data field in Mixpanel push notification composer screen, showing the appropriate spot for the JSON above

您可以在应用中使用任何可绘制资源作为图标 - 可以在Mixpanel库中找到预包装通知图标的完整列表,其资源名称如下所示。

  • com_mixpanel_android_ic_bell
  • com_mixpanel_android_ic_clipboard_checkmark
  • com_mixpanel_android_ic_coin
  • com_mixpanel_android_ic_flag
  • com_mixpanel_android_ic_gear
  • com_mixpanel_android_ic_inbox
  • com_mixpanel_android_ic_megaphone
  • com_mixpanel_android_ic_phone
  • com_mixpanel_android_ic_rocket
  • com_mixpanel_android_ic_sale_tag
  • com_mixpanel_android_ic_sync
  • com_mixpanel_android_ic_trophy
  • com_mixpanel_android_ic_vip
  • com_mixpanel_android_ic_warning

请注意,您的proguard配置可能会删除Mixpanel资源,因此如果您想要使用它们,请确保您没有删除它们。

答案 1 :(得分:2)

要回答@ user1544797问题的一个方面,除了@ user128536的答案之外,您可能希望让您的应用负责配置通知图标,而不是依赖于Mixpanel预览模式。为此,您必须通过创建自己的BroadcastReceiver来扩展Mixpanel GCMReceiver来拦截Mixpanel广播:

public class MixpanelGCMReceiver extends GCMReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        intent.putExtra("mp_icnm", "<your_icon_name>");
        super.onReceive(context, intent);
    }

}

然后在 AndroidManifest.xml 文件中声明您的BroadcastReceiver

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

最后,如果管理不当,@ user128536警告你Proguard正在弄乱你的通知图标(不幸的是,Mixpanel没有记录你的应用应该如何处理这种情况)。但是,除了Proguard之外,当您使用与 packageName 不同的 applicationId 时,您可能需要解决另一个问题(通常在使用产品风格时)。在Mixpanel SDK的ResourceReader课程中,您可以看到以下评论:

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

如上面的评论中所示,如果您发现自己处于这种情况,只需在 AndroidManifest.xml 文件中添加以下块:

<meta-data
  android:name="com.mixpanel.android.MPConfig.ResourcePackageName"
  android:value="<your_package_name>" />

那就是它,你现在应该完成;)

答案 2 :(得分:1)

此外,您可以添加小图标女巫将显示在状态栏中。

"mp_icnm_w": "your_small_icon_id"

它应该是白色的透明bg。

答案 3 :(得分:0)

我将这个答案用于即使在阅读了所有之前的答案之后仍无法找出完美解决方案的人们。当您收到Mixpanel按钮时,我将总结显示应用程序图标所需的所有关键点。

  1. 如果您使用的是 ProGuard (在我的情况下),则需要在 Proguard Rule 文件中放入以下设置。

keepclassmembers class **.R$* {
    public static <fields>;
}

  1. 在您的 Manifest.xml 文件中添加以下代码。

<meta-data
    android:name="com.mixpanel.android.MPConfig.ResourcePackageName"
    android:value="YOUR_PACKAGE_NAME" /> 

这是在应用程序清单文件中声明的包名称(package="YOUR_PACKAGE_NAME")(例如,以YOUR_PACKAGE_NAME命名。将其替换为实际的包名称值)。请勿将其与应用程序ID混淆。如果您将应用程序ID 放在此处,则无法使用。

在上面的答案之一中提到的ResourceReader类中也提到了第1点和第2点。

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

  1. 如果您已经在使用FCMPushReceiver,并且想要集成Mixpanel通知支持,请遵循here中提到的准则。

无论您使用的是MixpanelFCMMessagingService还是FirebaseMessagingService,都需要在onMessageReceived()部分的下面放置代码。

/* drawable_name is just the Drawable Name like if you app logo is app_icon, 
   use "app_icon" instead of "R.drawable.app_icon" */
if (TextUtils.isEmpty(intent.getStringExtra("mp_icnm"))) {
    intent.putExtra("mp_icnm", "drawable_name"); // mp_icnm is used for the app icon
}
if (TextUtils.isEmpty(intent.getStringExtra("mp_icnm_l"))) {
    intent.putExtra("mp_icnm_l", "drawable_name"); // mp_icnm_l is used for the large icon
}
if (TextUtils.isEmpty(intent.getStringExtra("mp_icnm_w"))) {
    intent.putExtra("mp_icnm_w", "drawable_name"); // mp_icnm_w is used for the White icon
} 

有关Mixpanel使用的参数的更多信息,可以检查parseIntent类的MixpanelPushNotification方法。

对于MixpanelFCMMessagingService,可以在method参数中获得意图。对于FirebaseMessagingService,您可以通过以下操作获得意图-

final Intent intent = remoteMessage.toIntent();