简体   繁体   English

如何在离子中更改android本地通知图标?

[英]How to change android local notification icon in ionic?

I'm using ionic to build an android app. 我正在使用ionic来构建一个Android应用程序。 I'm using $cordovaLocalNotification for local notifications. 我正在使用$cordovaLocalNotification进行本地通知。 The notification works but it shows a default bell icon. 通知有效但显示默认铃声图标。 How can I customize the notification icon? 如何自定义通知图标?

$scope.scheduleSingleNotification = function () {
    $cordovaLocalNotification.schedule({
      id: 1,
      title: 'GRM APP Builder',
      text: 'Quer café?!?',
      badge: 1,
      icon: 'res://coffee.png',
      data: {
        customProperty: 'custom value 1'
      }
    }).then(function (result) {
      console.log('Notification 1 triggered');
    });
  };

After spend hours with this question, I saw that one comment above it's really right. 花了好几个小时讨论这个问题之后,我看到上面的一条评论是非常正确的。

If you want to change icon, you need to create a folder called "drawable" in "[my ionic app folder]\\platforms\\android\\res\\drawable". 如果要更改图标,则需要在“[my ionic app folder] \\ platforms \\ android \\ res \\ drawable”中创建一个名为“drawable”的文件夹。

But the trick is: after this you need to quit your livereload mode and execute again CLI command "ionic run android -l -c -s". 但诀窍是:在此之后你需要退出livereload模式并再次执行CLI命令“ionic run android -l -c -s”。 It's necessary because you need to copy new assets to device. 这是必要的,因为您需要将新资产复制到设备。

I only tested with Android device, if you can test with iOS please send a review here. 我只测试了Android设备,如果你可以用iOS测试,请在这里发送评论。

According to this post on the forum, you can simply set the notification's icon and smallIcon properties. 根据论坛上的这篇文章 ,您只需设置通知的图标smallIcon属性即可。 You have to put the files into /platforms/android/res/drawable/ folder. 你必须把文件放到/ platforms / android / res / drawable /文件夹中。 (also mind that the icon has to be started with 'res://somename') (还要注意图标必须以'res:// somename'开头)

Note: You shall replace ngCordova's notification handling functions, since they are faulty. 注意:您应该替换ngCordova的通知处理功能,因为它们有问题。

In the latest ionic-cli 3 , 在最新的ionic-cli 3

Copy your icon.png into the following folder. icon.png复制到以下文件夹中。

platforms/android/res/drawable/icon.png

Note that this is in android only. 请注意,这只是在android中。

Once this is done( make sure that the image is a transparent icon), 完成后(确保图像是透明图标),

next step is to initialize this icon in the notification.init function. 下一步是在notification.init函数中初始化此图标。

Now if we you are using push plugin do the following; 现在,如果我们使用push plugin请执行以下操作:

const pushObj: PushObject = this.push.init({
                        android: {
                            senderID: "your_id",
                            icon: "icon",
                            forceShow: "true"
                        },
                        ios: {
                            alert: "true",
                            badge: "true",
                            sound: "true"
                        }
                    });

As you can see that the Only the name of the icon is added not the extenstion. 如您所见, 添加了图标的名称而不是扩展名。

Once this is done, include a same key value pair in the server side code as well, which pushes the notification into your device. 完成此操作后,还要在服务器端代码中包含相同的键值对,这会将通知推送到您的设备中。

All will be working well. 一切都会好起来的。

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

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