How To Automatically Copy OneSignal Custom Notification Icons Into The Android Resources Directories For Ionic Projects

If you are using OneSignal for your push notifications in your Ionic project then here’s a useful hook to automatically copy your custom icons into your android resources folder.

The OneSignal Ionic SDK installation is here and the note about custom android notification icons is here which links to the custom android notification generator.

Once you have generated your icons and downloaded them follow these instructions:

1) Create a directory at the root of your project called ‘onesignal’, extract the files from ic_stat_onesignal_default.zip into it

2) Add a file to your ‘hooks’ directory inside the ‘after_prepare’ folder called ‘030_copy_android_notification_icons.js’

Put the following code in it:

#!/usr/bin/env node 
var filestocopy = [{ 
    "onesignal/res/drawable-hdpi/ic_stat_onesignal_default.png": 
    "platforms/android/res/drawable-hdpi/ic_stat_onesignal_default.png" 
}, { 
    "onesignal/res/drawable-mdpi/ic_stat_onesignal_default.png": 
    "platforms/android/res/drawable-mdpi/ic_stat_onesignal_default.png" 
}, { 
    "onesignal/res/drawable-xhdpi/ic_stat_onesignal_default.png": 
    "platforms/android/res/drawable-xhdpi/ic_stat_onesignal_default.png" 
}, { 
    "onesignal/res/drawable-xxhdpi/ic_stat_onesignal_default.png": 
    "platforms/android/res/drawable-xxhdpi/ic_stat_onesignal_default.png" 
}, { 
    "onesignal/res/drawable-xxxhdpi/ic_stat_onesignal_default.png": 
    "platforms/android/res/drawable-xxxhdpi/ic_stat_onesignal_default.png" 
} ]; 

var fs = require('fs'); 
var path = require('path'); 
// no need to configure below 
var rootdir = process.argv[2]; 

filestocopy.forEach(function(obj) { 
    Object.keys(obj).forEach(function(key) { 
        var val = obj[key]; 
        var srcfile = path.join(rootdir, key); 
        var destfile = path.join(rootdir, val); 
        //console.log("copying "+srcfile+" to "+destfile); 
        var destdir = path.dirname(destfile); 
        if (fs.existsSync(srcfile) && fs.existsSync(destdir)) { 
            fs.createReadStream(srcfile).pipe( fs.createWriteStream(destfile)); 
        } 
    }); 
});

(Many thanks to DevGirl for the code that I have adapted for this tutorial. See the original blog post here)

3) From the root of your project make the file executable:

$ chmod +x hooks/after_prepare/030_copy_android_notification_icons.js

4) Run ionic prepare

$ ionic prepare android

And your files will be copied into the correct directories for your android project

One thought on “How To Automatically Copy OneSignal Custom Notification Icons Into The Android Resources Directories For Ionic Projects

  • Thanks! This was really helpful.

    I had to add to the hook the script to create the folders if they do not exists though.
    I added this just before the line “if (fs.existsSync(srcfile) && fs.existsSync(destdir)) {”

    var destfolder = destfile.split(‘/’);
    destfolder.pop();
    destfolder = destfolder.join(‘/’);
    if (!fs.existsSync(destfolder)) {
    fs.mkdirSync(destfolder);
    }

Leave a Reply