How to change android local notification icon in i

2019-06-25 06:14发布

I'm using ionic to build an android app. I'm using $cordovaLocalNotification for local notifications. The notification works but it shows a default bell icon. How can I customize the notification icon?

3条回答
趁早两清
2楼-- · 2019-06-25 06:48

According to this post on the forum, you can simply set the notification's icon and smallIcon properties. You have to put the files into /platforms/android/res/drawable/ folder. (also mind that the icon has to be started with 'res://somename')

Note: You shall replace ngCordova's notification handling functions, since they are faulty.

查看更多
虎瘦雄心在
3楼-- · 2019-06-25 06:48

In the latest ionic-cli 3,

Copy your icon.png into the following folder.

platforms/android/res/drawable/icon.png

Note that this is in android only.

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.

Now if we you are using push plugin do the following;

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.

查看更多
姐就是有狂的资本
4楼-- · 2019-06-25 06:50
$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".

But the trick is: after this you need to quit your livereload mode and execute again CLI command "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.

查看更多
登录 后发表回答