Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Invalid data, chunk must be a string or buffer, not object - ionic and firebase

I am currently working on setting up my ionic app in on my MAC but I keep running into an issue where, when I add in cordova-plugin-fcm, using Cordova version 8.0.0 and android version 6.4.0, I get the following error:

Invalid data, chunk must be a string or buffer, not object

Please note that I don't have the ios platform install on my app. Also, since I have the FCM plugin installed, I have added the google-services.json file to the root of my project.

Finally, the weirdest thing is that my project works just fine when I use my PC. However, on my MAC (which I use all the time), doesn't work with my project.

Any idea why this is not working. FYI - I have tried so many solutions online

  • uninstalling and re-installing cordova
  • cordova version 7.1.0
  • commenting out the line below from the cordova-plugin-fcm folder fcm_config_files_process.js file:

-

//fs.writeFileSync("platforms/ios/" + name + "/Resources/GoogleService-Info.plist", contents)

Any ideas what I'm doing wrong?

like image 644
AppFirl Avatar asked Jul 22 '26 15:07

AppFirl


2 Answers

The solution provided over cordova-plugin-fcm Github issue works as per the upvotes and comments by over 100 users.

Thanks to Cesar Cruz for mentioning it. +upvote

It works for Ionic as for me it is the solution for my Ionic project.

The extract from my solution at:https://github.com/fechanique/cordova-plugin-fcm/issues/213#issuecomment-357162384

The fix is to replace the code in /cordova-plugin-fcm/scripts/fcm_config_files_process.js as below:

#!/usr/bin/env node
'use strict';

var fs = require('fs');
var path = require('path');

fs.ensureDirSync = function (dir) {
    if (!fs.existsSync(dir)) {
        dir.split(path.sep).reduce(function (currentPath, folder) {
            currentPath += folder + path.sep;
            if (!fs.existsSync(currentPath)) {
                fs.mkdirSync(currentPath);
            }
            return currentPath;
        }, '');
    }
};

var config = fs.readFileSync('config.xml').toString();
var name = getValue(config, 'name');

var IOS_DIR = 'platforms/ios';
var ANDROID_DIR = 'platforms/android';

var PLATFORM = {
    IOS: {
        dest: [
            IOS_DIR + '/' + name + '/Resources/GoogleService-Info.plist',
            IOS_DIR + '/' + name + '/Resources/Resources/GoogleService-Info.plist'
        ],
        src: [
            'GoogleService-Info.plist',
            IOS_DIR + '/www/GoogleService-Info.plist',
            'www/GoogleService-Info.plist'
        ]
    },
    ANDROID: {
        dest: [
            ANDROID_DIR + '/google-services.json',
            ANDROID_DIR + '/app/google-services.json',
        ],
        src: [
            'google-services.json',
            ANDROID_DIR + '/assets/www/google-services.json',
            'www/google-services.json'
        ],
        stringsXml: ANDROID_DIR + '/app/src/main/res/values/strings.xml'
    }
};

// Copy key files to their platform specific folders
if (directoryExists(IOS_DIR)) {
    copyKey(PLATFORM.IOS);
}
if (directoryExists(ANDROID_DIR)) {
    copyKey(PLATFORM.ANDROID, updateStringsXml)
}

function updateStringsXml(contents) {
    var json = JSON.parse(contents);
    var strings = fs.readFileSync(PLATFORM.ANDROID.stringsXml).toString();

    // strip non-default value
    strings = strings.replace(new RegExp('<string name="google_app_id">([^\@<]+?)</string>', 'i'), '');

    // strip non-default value
    strings = strings.replace(new RegExp('<string name="google_api_key">([^\@<]+?)</string>', 'i'), '');

    // strip empty lines
    strings = strings.replace(new RegExp('(\r\n|\n|\r)[ \t]*(\r\n|\n|\r)', 'gm'), '$1');

    // replace the default value
    strings = strings.replace(new RegExp('<string name="google_app_id">([^<]+?)</string>', 'i'), '<string name="google_app_id">' + json.client[0].client_info.mobilesdk_app_id + '</string>');

    // replace the default value
    strings = strings.replace(new RegExp('<string name="google_api_key">([^<]+?)</string>', 'i'), '<string name="google_api_key">' + json.client[0].api_key[0].current_key + '</string>');

    fs.writeFileSync(PLATFORM.ANDROID.stringsXml, strings);
}

function copyKey(platform, callback) {
    for (var i = 0; i < platform.src.length; i++) {
        var file = platform.src[i];
        if (fileExists(file)) {
            try {
                var contents = fs.readFileSync(file).toString();

                try {
                    platform.dest.forEach(function (destinationPath) {
                        var folder = destinationPath.substring(0, destinationPath.lastIndexOf('/'));
                        fs.ensureDirSync(folder);
                        fs.writeFileSync(destinationPath, contents);
                    });
                } catch (e) {
                    // skip
                }

                callback && callback(contents);
            } catch (err) {
                console.log(err)
            }

            break;
        }
    }
}

function getValue(config, name) {
    var value = config.match(new RegExp('<' + name + '>(.*?)</' + name + '>', 'i'));
    if (value && value[1]) {
        return value[1]
    } else {
        return null
    }
}

function fileExists(path) {
    try {
        return fs.statSync(path).isFile();
    } catch (e) {
        return false;
    }
}

function directoryExists(path) {
    try {
        return fs.statSync(path).isDirectory();
    } catch (e) {
        return false;
    }
}
  1. Make sure you have copied the above file in "plugins" folder as cordova copies all the cordova-plugins from node_modules directory to plugins directory.
  2. If you have already added the platforms before modifying the file plugins/cordova-plugin-fcm/scripts/fcm_config_files_process.js, you need to remove the platforms and add again.
like image 141
Ketan Yekale Avatar answered Jul 24 '26 10:07

Ketan Yekale


Take a look at the solution from @ketanyekale at https://github.com/fechanique/cordova-plugin-fcm/issues/213#issuecomment-357162384 It might be useful, although is not for ionic.

Maybe you could get a clue about how to solve the issue.

It worked for me in a cordova/phonegap project.

like image 26
NeuronTI Avatar answered Jul 24 '26 08:07

NeuronTI