【问题标题】:White notification icon for Android with Phonegap Build and PushPlugin带有 Phonegap Build 和 PushPlugin 的 Android 的白色通知图标
【发布时间】:2014-01-02 23:24:54
【问题描述】:

我正在使用 Adob​​e 的 Build 服务处理 Phonegap,我想知道是否(如果是,如何)我可以让我的通知显示一个白色图标(正如 Google 描述的 here)。我正在使用PushPlugin,但我找不到任何关于此的文档。目前我的应用只在通知内容旁边显示启动器图标。

那么有谁知道是否有某种方法可以使用 PushPlugin 启用备用通知图标,我是坚持使用启动器图标还是有其他插件具有此功能?

谢谢!

【问题讨论】:

    标签: android cordova build notifications icons


    【解决方案1】:

    我从未使用过 Phonegap 构建 - 我在我的机器上手动执行构建过程。无论如何,我认为您应该在 src/com/plugin/gcm 下找到 GCMIntentService.java 文件。在该文件中,搜索以下代码行:

    .setSmallIcon(context.getApplicationInfo().icon)
    

    这可能会替换为您想要的图标。如果在 /res/drawable 文件夹中放置一个名为“notification.png”的图标,则可以将代码行更改为:

    .setSmallIcon(your.package.name.R.drawable.notification);
    

    【讨论】:

    • /res/drawable 文件夹在哪里?
    • 使用您建议的修改构建它时,我得到了platforms/android/src/com/plugin/gcm/GCMIntentService.java:105: error: cannot find symbol .setSmallIcon(R.drawable .notification) ^ 符号:变量通知位置:可绘制类你知道为什么会这样吗?
    【解决方案2】:

    新的“phonegap-plugin-push”插件对新棒棒糖的通知图标有很好的支持。

    首先构建一个在透明图标上有颜色的图标。根据谷歌的guidelines,所有通知都会在通知托盘上显示为白色。

    因此构建图标,然后按如下方式指定它。 "icon" 指定图像。

    将具有适当分辨率的“myicon”副本放在 /platforms/android/res/drawable-RES/ 文件夹中,如下所示:

       mdpi    - 22x22 area in 24x24
       hdpi    - 33x33 area in 36x36
       xhdpi   - 44x44 area in 48x48
       xxhdpi  - 66x66 area in 72x72
       xxxhdpi - 88x88 area in 96x96
    

    现在在您的 JS 中,指定您的图标。 “iconColor”是可选的,当你向下滑动时会指定背景。

    var push = PushNotification.init({
        "android": {
            "senderID": "<<SENDER_ID>>",
            "icon": "myicon",
            "iconColor": "#123456"
        },
    

    在您的推送负载中,您现在可以通过添加“图像”参数来覆盖向下滑动时指定的图标。图片可以是资源,可以引用“www/image/image.png”,也可以是完整的 URL。

    plugin docs”非常好,我推荐阅读它们。希望这对某人有用。

    【讨论】:

      【解决方案3】:

      在您的 PhoneGap 或 Cordova config.xml

      上进行设置
      <preference name="android-targetSdkVersion" value="20"/>
      

      【讨论】:

      • 这是一个非常 hacky 的解决方案,而且不是很有未来的证明。它现在可以工作了,但特别从你的构建中排除了 Lollipop。
      • 但是谷歌已经限制使用最低目标 SDK 版本 >= 26 那么如何解决呢?
      【解决方案4】:

      这是给新插件的 - phonegap-plugin-push

      可能也适用于旧的 PushPlugin。

      Android 为 Lollipop 及更高版本的通知图标设置了指南。图标必须是纯白色和透明的。

      我做了什么:

      1. 使用tool 为我的标志图像(我们将其命名为“notif.png”)生成不同大小的透明背面。记得要有一个透明的背景图片,如果你没有透明的图片,android 只会显示白色图标。假设生成的图标名称是“ic_notif”。

      2. 将相应尺寸的图片复制到 res/drawables 文件夹中。

      3. 在推送 JS 代码中,将值“ic_notif”赋予“icon”属性。而“grey”到“iconColor”属性是这样的:PushNotification.init({"android":{"senderId":"XX","icon":"ic_notif","iconColor":"grey"}});

      使用此tool 测试您的通知。

      【讨论】:

        【解决方案5】:

        Android API 21+ 仅对小图标使用白色。 更改目标 SDK 版本会使您的编译器使用较旧的 SDK,因此您将被削减新的 SDK 功能。 所以最好的方法是有两个图标:小一个和一个大(当栏被向下拖动时)

        【讨论】:

          【解决方案6】:

          有一段时间,您可以将您的 android-targetSdkVersion 设置为 API 20。 此版本不使用新功能,破坏了旧资源。

          【讨论】:

            【解决方案7】:

            对我来说,结合此处的两个答案时,该解决方案有效:

            首先我设置目标sdk版本

            <preference name="android-targetSdkVersion" value="20"/>
            

            然后我按照这里的建议编辑了插件代码,在 GCMIntentService 类中我将 setNotificationSmallIcon 方法修改为:

             private void setNotificationSmallIcon(Context context, Bundle extras, String packageName, Resources resources, NotificationCompat.Builder mBuilder, String localIcon) {
                mBuilder.setSmallIcon(getApplicationContext().getApplicationInfo().icon);
            }
            

            或者如果你想设置一个自定义图标然后使用

             private void setNotificationSmallIcon(Context context, Bundle extras, String packageName, Resources resources, NotificationCompat.Builder mBuilder, String localIcon) {
                mBuilder.setSmallIcon(<your.package.name>.R.drawable.<notification-drawable-name>);
            }
            

            【讨论】:

              【解决方案8】:

              我在这方面也遇到了太多问题,但是在搜索了整个互联网后,我找到了针对此问题的不同解决方案。让我总结所有解决方案并解释一下:

              注意:此解决方案适用于 Phonegap cordova 用户

              1. Example

              &lt;preference name="android-targetSdkVersion" value="20"/&gt;

              您需要将您的 android-targetSdkVersion 值设置为小于 21。 所以设置此值后,通知图标图像会一直显示到 Android 6(Marshmallow),它不会在 Android 7(Nougat) 中工作。 这个解决方案对我有用。

              1. 更改配置文件中的状态栏样式。 Example

              &lt;preference name="StatusBarStyle" value="lightcontent" /&gt;

              但此解决方案仅在您的应用打开时才有效。 所以,我猜这个解决方案不是最好的解决方案,但它适用于许多用户。

              1. 使您的图标透明。 该解决方案适用于许多人。 实际上,在 Native 应用程序的开发中,我们需要为它们提供三个图像: (a)应用程序图标 (b)通知图标 (c)状态栏图标图像,但在混合移动应用程序开发的情况下,没有选择这样做。 所以让你的图标透明,这个解决方案将解决你的问题。

              而且我确信上述解决方案之一将适用于您的问题。

              【讨论】:

                【解决方案9】:

                随着最近将 PhoneGap Builder 更新到版本 2(和 CLI v7.0.1),我现在能够使用 cordova-plugin-push 正确显示白色通知图标。

                我首先在 GIMP 中创建了我的 .png 图标(具有透明度)。然后我将它们放在我的项目中/www/res/notify_icon/android/

                以下是我的 config.xml 文件的相关摘录:

                <widget id="com.myapp.app" version="1.2.0" xmlns="http://www.w3.org/ns/widgets" xmlns:gap="http://phonegap.com/ns/1.0">
                    <preference name="phonegap-version" value="cli-7.0.1" />
                    <preference name="pgb-builder-version" value="2" />
                
                    ...
                
                    <plugin name="phonegap-plugin-push" source="npm" spec="~2.0.0-rc5">
                        <param name="SENDER_ID" value="1xxxxxxxxxx0" />
                    </plugin>
                
                    ...
                
                    <platform name="android">
                        <resource-file src="google-services.json" target="google-services.json" />
                        <resource-file src="www/res/notify_icon/android/notify_icon-36-ldpi.png" target="res/drawable-ldpi/notify_icon.png" />
                        <resource-file src="www/res/notify_icon/android/notify_icon-48-mdpi.png" target="res/drawable-mdpi/notify_icon.png" />
                        <resource-file src="www/res/notify_icon/android/notify_icon-72-hdpi.png" target="res/drawable-hdpi/notify_icon.png" />
                        <resource-file src="www/res/notify_icon/android/notify_icon-96-xhdpi.png" target="res/drawable-xhdpi/notify_icon.png" />
                        <resource-file src="www/res/notify_icon/android/notify_icon-144-xxhdpi.png" target="res/drawable-xxhdpi/notify_icon.png" />
                        <resource-file src="www/res/notify_icon/android/notify_icon-192-xxxhdpi.png" target="res/drawable-xxxhdpi/notify_icon.png" />
                
                        ...
                
                    </platform>
                </widget>
                

                这里是我初始化推送的地方:

                    document.addEventListener("deviceready", function() {
                
                        var push_options = {
                            android: {
                                senderID: "1xxxxxxxxxxx0",
                                sound: true,
                                vibrate: true,
                                icon:  'notify_icon',
                                iconColor: '#66CC00'
                            },
                            browser: {},
                            ios: {
                                senderID: "1xxxxxxxxxxx0",
                                alert: true,
                                badge: true,
                                sound: true
                            },
                            windows: {}
                        };
                
                        var push = PushNotification.init(push_options);
                

                【讨论】:

                  【解决方案10】:

                  你只需要改变

                  android-targetSdkVersion

                  在您的 config.xml 文件中。

                  <preference name="android-targetSdkVersion" value="20" />
                  

                  20 android sdk 版本支持Android 中的logo。所以你必须在你的代码中更改android sdk版本。

                  【讨论】:

                    【解决方案11】:

                    如果您使用的是Build's PushPlugin;对.setSmallIcon()#100 的本地修改将丢失。

                    除非您在本地构建或使用替代的 GCM 插件,否则您只能修改:

                    title    extras.getString("title")
                    message  extras.getString("message")
                    msgcnt   extras.getString("msgcnt")
                    

                    【讨论】:

                      猜你喜欢
                      • 1970-01-01
                      • 1970-01-01
                      • 1970-01-01
                      • 1970-01-01
                      • 1970-01-01
                      • 1970-01-01
                      • 1970-01-01
                      • 1970-01-01
                      • 1970-01-01
                      相关资源
                      最近更新 更多