简体   繁体   English

适用于Android的白色通知图标,带有Phonegap Build和PushPlugin

[英]White notification icon for Android with Phonegap Build and PushPlugin

I'm messing around with Phonegap using Adobe's Build service and I was wondering if (and if so, how) I could make my notifications display a white icon (as Google describes here ). 我正在使用Adobe的Build服务搞乱Phonegap,我想知道是否(如果是的话,如何)我可以让我的通知显示一个白色图标(正如谷歌在这里描述的那样)。 I'm using the PushPlugin but I couldn't find any documentation on this. 我正在使用PushPlugin,但我找不到任何关于此的文档。 At the moment my app just displays the launcher icon next to the notification content. 目前,我的应用只显示通知内容旁边的启动器图标。

So does anyone know if there's some way to enable an alternate notification icon with the PushPlugin, am I stuck with the launcher icon or is there another plugin with this feature? 所以有人知道是否有某种方法可以使用PushPlugin启用备用通知图标,我是否坚持使用启动器图标或是否有另一个具有此功能的插件?

Thanks! 谢谢!

I have never used Phonegap build - I do the build process manually on my machine. 我从未使用Phonegap构建 - 我在我的机器上手动执行构建过程。 Anyway, I think you should find the GCMIntentService.java file under src/com/plugin/gcm. 无论如何,我认为你应该在src / com / plugin / gcm下找到GCMIntentService.java文件。 In that file, search for the following line of code: 在该文件中,搜索以下代码行:

.setSmallIcon(context.getApplicationInfo().icon)

This may be replaced by the icon you want. 这可能会被您想要的图标取代。 If you place an icon named "notification.png" in the /res/drawable folder, you could change the code line to: 如果在/ res / drawable文件夹中放置名为“notification.png”的图标,则可以将代码行更改为:

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

在PhoneGap或Cordova config.xml上设置此项

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

The new " phonegap-plugin-push " plugin has excellent support for notification icons for the new Lollipop. 新的“ phonegap-plugin-push ”插件非常支持新Lollipop的通知图标。

Build an icon that has color on a transparent icon first. 首先在透明图标上构建一个颜色的图标。 Per google's guidelines all notifications will display white on the notification tray. 根据Google的指导原则,所有通知都会在通知托盘上显示为白色。

So build the icon and then specify it as follows. 然后构建图标,然后按如下方式指定它。 "icon" specifies the image. “icon”指定图像。

Place copies of "myicon" with appropriate resolutions in /platforms/android/res/drawable-RES/ folders as follows: 将具有适当分辨率的“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

Now in your JS, specify your icon. 现在在你的JS中,指定你的图标。 The "iconColor" is optional and will specify the background when you swipe down. “iconColor”是可选的,将在您向下滑动时指定背景。

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

In your push payload you can now-override the icon that is specified on swipe down by adding an "image" parameter. 在推送有效负载中,您现在可以通过添加“图像”参数来覆盖在向下滑动时指定的图标。 Image can either be a resource, it can reference "www/image/image.png", or can be a full URL. 图像既可以是资源,也可以是“www / image / image.png”,也可以是完整的URL。

The " plugin docs " are excellent, I recommend reading them. 插件文档 ”非常好,我建议阅读它们。 Hope this is useful to somebody. 希望这对某人有用。

This is for the new plugin - phonegap-plugin-push 这是针对新插件 - phonegap-plugin-push

Might work for the old PushPlugin as well. 可能也适用于旧的PushPlugin。

Android has set guidelines for the notification icon for Lollipop and above. Android已为Lollipop及更高版本的通知图标设置了指南。 The icon needs to be purely white and transparent. 图标需要纯白色和透明。

What I did: 我做了什么:

  1. Generated different sizes for my logo image (lets name it "notif.png") which had transparent back, using this tool . 使用此工具为我的徽标图像生成不同的尺寸(让它命名为“notif.png”),它具有透明的背面。 Remember to have a transparent back image, if you dont have a transparent image, android will just show white icon only. 记得有一个透明的背面图像,如果你没有透明的图像,android只会显示白色图标。 Lets assume the generated icon's name is "ic_notif". 让我们假设生成的图标的名称是“ic_notif”。

  2. Copy the respective size images to res/drawables folder. 将相应大小的图像复制到res / drawables文件夹。

  3. In the push JS code, give the value "ic_notif" to "icon" property. 在push JS代码中,将值“ic_notif”赋予“icon”属性。 And "grey" to "iconColor" property like this: PushNotification.init({"android":{"senderId":"XX","icon":"ic_notif","iconColor":"grey"}}); 并且“灰色”到“iconColor”属性如下: PushNotification.init({"android":{"senderId":"XX","icon":"ic_notif","iconColor":"grey"}});

Test your notification using this tool . 使用此工具测试您的通知。

Android API 21+ uses only white color for small icon. Android API 21+仅使用白色作为小图标。 Changing target SDK Version makes your compiler to use elder SDK so U`ll be cut in new SDK features. 更改目标SDK版本使您的编译器可以使用旧版SDK,因此可以使用新的SDK功能。 So the best way is to have two icons: small and big one (when bar is dragged down) 所以最好的方法是有两个图标:小图标和大图标(当栏被拖下来时)

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

For me the solution works when combining two answers from here: 对我来说,解决方案适用于从这里结合两个答案:

First I set the target sdk version 首先,我设置目标sdk版本

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

And then I edited the plugin code as suggested here, in GCMIntentService class I modified the setNotificationSmallIcon method to be: 然后我按照这里的建议编辑了插件代码,在GCMIntentService类中我将setNotificationSmallIcon方法修改为:

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

or if you want to set a custom icon then use 或者如果要设置自定义图标,请使用

 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>);
}

I also faced too many problem in this but after searching all over the internet i found different solutions for this issue.Let me sum up all the solutions and explain: 我在这方面也遇到了太多问题,但在搜索了整个互联网后,我找到了解决这个问题的不同解决方案。让我总结所有解决方案并解释:

Note:This solution is for Phonegap cordova users 注意:此解决方案适用于Phonegap cordova用户

  1. Example

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

You need to set your android-targetSdkVersion value to less than 21. So after setting this value Notification icon image will appear till Android 6(Marshmallow), It won't work in Android 7(Nougat). 您需要将android-targetSdkVersion值设置为小于21.因此设置此值后,通知图标图像将出现在Android 6(Marshmallow)中,它将无法在Android 7(Nougat)中运行。 This solution worked for me. 这个解决方案对我有用。

  1. Change the statusbarStyle in your config file. 更改配置文件中的statusbarStyle。 Example

<preference name="StatusBarStyle" value="lightcontent" />

But this solution will work only when your app is opened. 但此解决方案仅在您的应用程序打开时才有效。 So, I guess this solution is not the best solution but it worked for many users. 所以,我猜这个解决方案不是最好的解决方案,但它适用于许多用户。

  1. Make your icon transparent. 使您的图标透明。 This solution worked for many people. 这个解决方案适用于很多人。 Actually the thing is, In the development of Native aplication we need to provide them three images: (a)App icon (b)Notification icon (c)Status bar icon image, but in case of Hybrid mobile application development there is no option to do so. 实际上,在本机应用程序的开发中,我们需要为它们提供三个图像:(a)应用程序图标(b)通知图标(c)状态栏图标图像,但在混合移动应用程序开发的情况下,没有选项这样做。 So make your icon transparent and this solution will solve your problem. 因此,使您的图标透明,此解决方案将解决您的问题。

And I am sure one of the above solution will work for your issue. 我相信上述解决方案之一将适用于您的问题。

With the recent update to PhoneGap Builder to version 2 (and CLI v7.0.1), I'm now able to get white notification icons to appear correctly with cordova-plugin-push. 随着PhoneGap Builder最近更新到版本2(和CLI v7.0.1),我现在能够通过cordova-plugin-push获得正确显示白色通知图标。

I first created my .png icons (with transparency) in GIMP. 我首先在GIMP中创建了我的.png图标(带透明度)。 I then put them in my project at /www/res/notify_icon/android/ 然后我把它们放在/www/res/notify_icon/android/

Here are the relevant excerpts of my config.xml file: 以下是我的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>

Here is the where I initialize push: 这是我初始化推送的位置:

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

You just need to the change the 你只需要改变

android-targetSdkVersion Android的targetSdkVersion

in your config.xml file. 在您的config.xml文件中。

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

20 android sdk version support the logo in Android. 20个 android sdk版本支持Android中的徽标。 So you have to change the android sdk version in your code. 所以你必须在你的代码中更改android sdk版本。

If you're using Build's PushPlugin ; 如果您正在使用Build的 PushPlugin ; local modifications to .setSmallIcon() #100 will be lost. .setSmallIcon() #100的本地修改将丢失。

Unless you build locally or use an alternative GCM plugin, you're restricted to modifying: 除非您在本地构建或使用替代GCM插件,否则您只能修改:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM