New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
cyclonmaster opened this Issue Jan 18, 2017 · 47 comments

Comments

Projects
None yet
@cyclonmaster

cyclonmaster commented Jan 18, 2017

I run this to add this plugin in my project:
cordova plugin add cordova-plugin-fcm --save

At the end I get this error:
Invalid data, chunk must be a string or buffer, not object

`Fetching plugin "cordova-plugin-fcm" via npm
Installing "cordova-plugin-fcm" for android
ANDROID_HOME=C:\Users\mkhairunns\AppData\Local\Android\sdk
JAVA_HOME=C:\Program Files\Java\jdk1.8.0_20
Subproject Path: CordovaLib
Incremental java compilation is an incubating feature.
:clean
:CordovaLib:clean

BUILD SUCCESSFUL

Total time: 9.497 secs
Subproject Path: CordovaLib

            Cordova FCM plugin v2.1.0 installed
            For more details visit https://github.com/fechanique/cordova-plu

gin-fcm

Saved plugin info for "cordova-plugin-fcm" to config.xml
Error: Invalid data, chunk must be a string or buffer, not object`

The I try cordova build android, I get same error:
Invalid data, chunk must be a string or buffer, not object

This is my config.xml:

<?xml version='1.0' encoding='utf-8'?> <widget id="my.com.sains.FCMtest" version="1.0.0" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0"> <name>FCMtest</name> <description> A sample Apache Cordova application that responds to the deviceready event. </description> <author email="dev@cordova.apache.org" href="http://cordova.io"> Apache Cordova Team </author> <content src="index.html" /> <access origin="*" /> <allow-intent href="http://*/*" /> <allow-intent href="https://*/*" /> <allow-intent href="tel:*" /> <allow-intent href="sms:*" /> <allow-intent href="mailto:*" /> <allow-intent href="geo:*" /> <platform name="android"> <allow-intent href="market:*" /> </platform> <platform name="ios"> <allow-intent href="itms:*" /> <allow-intent href="itms-apps:*" /> </platform> <plugin name="cordova-plugin-whitelist" spec="~1.3.1" /> <plugin name="cordova-plugin-fcm" spec="~2.1.0" /> </widget>

Any help?

@szymons91

This comment has been minimized.

szymons91 commented Jan 18, 2017

+1

1 similar comment
@rayan-sta

This comment has been minimized.

rayan-sta commented Jan 18, 2017

+1

@ilman

This comment has been minimized.

ilman commented Jan 18, 2017

I get same problem, i solve this issue by running "ionic platform update android"

@Aries-UA

This comment has been minimized.

Aries-UA commented Jan 20, 2017

+1

@Silberling

This comment has been minimized.

Silberling commented Jan 21, 2017

Same here. Can't do anything until I remove the plugin.

@Silberling

This comment has been minimized.

Silberling commented Jan 21, 2017

Possible solution:

The first google-services.json I got didn't have an API key.

I opened the firebase project settings and re-downloaded the file. Now it contained the API key and it's working.

@nutella

This comment has been minimized.

nutella commented Jan 27, 2017

Same error here, this work for me: open file platforms/andoid/res/values/strings.xml then add 2 rows (that there aren't present):
@string/google_app_id
@string/google_api_key

@cyclonmaster

This comment has been minimized.

cyclonmaster commented Feb 7, 2017

I add the string:
@string/google_app_id
@string/google_api_key

Still; get the same problem. Any help here?

@didinj

This comment has been minimized.

didinj commented Feb 27, 2017

It just platform issue, try to remove and add the platform, also remove and add the plugin.
I write this solution in my tutorial.

@bendspoons

This comment has been minimized.

bendspoons commented Mar 14, 2017

I was able to "fix it" by renaming (e.g. resetup/recreate App) the App from something like

Test App & Test

to

Test App and Test

As far as i can tell the & or &amp; caused those problems on iOS devices

@giumarzo

This comment has been minimized.

giumarzo commented May 30, 2017

I resolve with:

ionic platform rm android
ionic platform rm ios
ionic plugin rm cordova-plugin-firebase
ionic platform add android
ionic platform add ios
ionic plugin add cordova-plugin-firebase

@loveonwheels

This comment has been minimized.

loveonwheels commented Jul 14, 2017

any one still having this error do this
ionic platform rm android
ionic platform rm ios
ionic plugin rm cordova-plugin-firebase
ionic platform add android
ionic platform add ios
ionic plugin add cordova-plugin-firebase

copy the GoogleService-Info.plist into platforms/ios/'app name'/Resources
and make sure no other copy exist within the project..

for me thats what caused the error i had another GoogleService-Info.plist in my root folder

@bendspoons

This comment has been minimized.

bendspoons commented Jul 14, 2017

Weird, @loveonwheels, in my case it ONLY works, if the plist and json files are located in root folder.

@cluny85

This comment has been minimized.

cluny85 commented Jul 19, 2017

In my case, just removing the plist and the json from the root file and having these files on the respective platform/android - ios folder works. Don't need to remove the whole platforms, that's too much!

@circuitrider

This comment has been minimized.

circuitrider commented Jul 25, 2017

Doing these actions in specific order seems to be a reliable solution from my research:

cordova plugin rm cordova-plugin-fcm
cordova platform rm ios
cordova platform rm android
cordova plugin add cordova-plugin-fcm
cordova platform add ios
cordova platform add android

Definitely set me back a day or so of work trying to get this worked out, I hope this can save at least one person the same trouble.

@satryacode

This comment has been minimized.

satryacode commented Aug 2, 2017

I have resolved this problem by doing what @loveonwheels said.
Nevertheless removing platform only to remove the plist / json file from the root folder

@Peeripapo

This comment has been minimized.

Peeripapo commented Aug 14, 2017

@circuitrider solution nailed it for me.

@jaybowman

This comment has been minimized.

jaybowman commented Aug 14, 2017

Resolved issue on IOS by adding 2 Resources folders under platform/ios/app name/Resources/Resources and placing the googleservices-info.plist in first resources folder.

@walfro

This comment has been minimized.

walfro commented Aug 16, 2017

[SOLVED]

In my case I had to edit file "fcm_config_files_process.js" located in folder "plugins/cordova-plugin-fcm/scripts/":

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

For some unknown reason while building the project this line (42) was throwing the error "Invalid data, chunk must be a string or buffer, not object" so what I did was to comment that line and then manually copy the file "GoogleService-Info.plist" to "platforms/ios/" + name + "/Resources/"

Hope this can help.

Thanks

@CoachMorta

This comment has been minimized.

CoachMorta commented Sep 11, 2017

for any one still have the same issue after trying remove and add platform , you should download the google-services.json file and GoogleService-Info.plist from your firebase account from your project there , because it has the API keys and IDs needed ,,

@henrymoews

This comment has been minimized.

henrymoews commented Nov 29, 2017

For me, the issue was solved by removing the ampersands (&) in config.xml as @bendspoons has described in his post.

Edit:
Problem still exists

@skout90

This comment has been minimized.

skout90 commented Dec 20, 2017

+ 1.. Problem still exists...

@jeenn

This comment has been minimized.

jeenn commented Dec 20, 2017

@walfro Does your solution only applies to people trying to run they projects on an iOS device?

@walfridosp

This comment has been minimized.

walfridosp commented Dec 20, 2017

@jeenn Yes, this solution applies for iOS only, I didn't have any issue for Android at that time. Are you having this error in Android? let me know and I might be able to help you.

Thanks,
Walfrido

@jeenn

This comment has been minimized.

jeenn commented Dec 20, 2017

@walfridosp Yes, I'm having this issue when I try to run my ionic project in an Android device.
I used to make my project work by editing the following files

  • FCMPlugin.gradle (located in /plugins/cordova-plugin-fcm/src/android)

  • project.properties (located in /platforms/android/project.properties)

  • build.gradle (located in /platforms/android/build.gradle)
    But after I updated my dependencies, the build.gradle file changed and I can't apply my previous solution.

Do you have any idea what could I be doing wrong?

@walfridosp

This comment has been minimized.

walfridosp commented Dec 20, 2017

@jeenn Did you try installing the plugin in a clean project? My idea of what could be happening is:

  • Missing google-services.json file in root project directory
  • Different IDs in config.xml and project created in console.firebase.google.com
  • Conflict with other plugin(s) (don't really think in this case)
@skout90

This comment has been minimized.

skout90 commented Dec 21, 2017

In android
i solved.

i changd cordova-android ver
^7.0.0 -> ^6.3.0

then error message disappeared

@jeenn

This comment has been minimized.

jeenn commented Dec 26, 2017

@skout90 Did you just downgraded your "cordova-android" package to 6.3.0, from you package.json?

@skout90

This comment has been minimized.

skout90 commented Dec 27, 2017

@jeenn
Then, i followed circuitrider

cordova plugin rm cordova-plugin-fcm
cordova platform rm ios
cordova platform rm android
cordova plugin add cordova-plugin-fcm
cordova platform add ios
cordova platform add android
@maocop

This comment has been minimized.

maocop commented Jan 4, 2018

dont forget delete the plataform folder

@Grohden

This comment has been minimized.

Grohden commented Jan 8, 2018

@fechanique the problem could be here maybe the new cordova version automatically puts the resource file in the resource folder. When i try to install the plugin i end up with a folder structure like this:
image

The Resources\Resources\ file is this one
And the Resources\ file is the one in my project root.

I don't know how to write plugins or how anything works in cordova, so it's just a guess.
I know that cordova 7.0.0 changed some folder struct:

They changed the directory structure of cordova 7.0 so it probably won't work. It should be working with cordova 6.3.0 though. Make sure you clear everything before downgrading.

(crosswalk-project/cordova-plugin-crosswalk-webview/issues/188)
So maybe that is the problem..

Edit:
Nope, that's not the problem, installed the GH-335 branch but no solution. :/

Edit Edit:
I found a solution,
As i said this related to the folders structure changes, but the problem is in plugins/cordova-plugin-fcm/scripts/fcm_config_files_process.js
in line 58, 61 and 78

...
var contents = fs.readFileSync(path).toString();
fs.writeFileSync("platforms/android/google-services.json", contents); // <- here

var json = JSON.parse(contents);
var resourcesPath = "./platforms/android/app/src/main/res/";
var strings = fs.readFileSync(resourcesPath + "values/strings.xml").toString(); // <- here
...
fs.writeFileSync("platforms/android/res/values/strings.xml", strings); // <-- and here

To fix this change the paths on lines 61 and 78 from "platforms/android/res/values/strings.xml" to "./platforms/android/app/src/main/res/values/strings.xml"

And change "platforms/android/google-services.json" to "platforms/android/app/google-services.json" in line 58

Grohden added a commit to Grohden/cordova-plugin-fcm that referenced this issue Jan 8, 2018

Fix for new cordova paths #213
Cordova 7.0+ changed the android paths structure, this should fix that problem and also maintain compatibility with older versions
@ketanyekale

This comment has been minimized.

ketanyekale commented Jan 12, 2018

I got the fix from one of the forked repo. It just needed correct path for strings.xml.

There is no need to downgrade cordova or cordova-android

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

Please consider fixing it quickly and release a new version.

For manually fixing the issue:

  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.
@ferozsho

This comment has been minimized.

ferozsho commented Jan 16, 2018

Please update @ketanyekale changes to master ?
Tested and working perfect.

@chamathpali

This comment has been minimized.

chamathpali commented Jan 28, 2018

@ketanyekale works! 👍 Thanks

@krinakis

This comment has been minimized.

krinakis commented Feb 10, 2018

I tried above all solution but no one works for me for android.Please Help !

bastiendonjon referenced this issue in bastiendonjon/cordova-plugin-fcm Feb 18, 2018

@thenb

This comment has been minimized.

thenb commented Feb 27, 2018

@ketanyekale works!

@ketanyekale

This comment has been minimized.

ketanyekale commented Mar 22, 2018

@krinakis, 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 platform android and add it again.

@cesarcruzm

This comment has been minimized.

cesarcruzm commented Mar 27, 2018

solution from @ketanyekale did the trick
I'm running android@7.0.0 on my project.

@benag

This comment has been minimized.

benag commented Mar 31, 2018

Hi,
I installed the plugin a week ago and run into the same issue fixed per the comment above, should be fixed in the main branch

@himalayaahuja

This comment has been minimized.

himalayaahuja commented Apr 10, 2018

Finally a solution that works @ketanyekale 👍 🥇 although a shame that it still hasn't been fixed in the main branch. Did look like an issue regarding paths as it couldn't read from the json file but didn't know how to fix that. Good work 👍 💯

@darshantejani007

This comment has been minimized.

darshantejani007 commented Apr 10, 2018

In recent versions of cordova-android, location of res/Strings.xml file has changed, which mostly causes this error. So go to /plugins/cordova-plugin-fcm/scripts/fcm_config_files_process.js and wherever you see platforms/android/res/values/strings.xml, replace it with platforms/android/app/src/main/res/values/strings.xml. There are mostly two occurrences.
This would fix it! Hope it helps.

@slzr

This comment has been minimized.

slzr commented Apr 11, 2018

tested the @ketanyekale solution in cordova-android@7.0.0 and works perfectly but, when will be implemented in master?

@SahSantoshh

This comment has been minimized.

SahSantoshh commented Apr 21, 2018

@ketanyekale fix work perfectly in cordova-android@7.0.0. Thanks.

lehtu added a commit to lehtu/cordova-plugin-fcm that referenced this issue May 6, 2018

Fixed problem with android 7.0
Fix copied from here: fechanique#213\#issuecomment-357162384
@fennsaji

This comment has been minimized.

fennsaji commented May 28, 2018

@ketanyekale Thanks your solution fixed the problem (cordova-android@7.0.0)

@samuelaj1

This comment has been minimized.

samuelaj1 commented Jun 6, 2018

@ketanyekale thanks for the solution..its working for me too

@laberg

This comment has been minimized.

laberg commented Sep 4, 2018

I had this problem trying to deploy for android and the solution for me was to remove the ios platform.

After that I could add the android platform and build the apk without any problem.

Hope this helps!

@fuatde

This comment has been minimized.

fuatde commented Nov 9, 2018

I was getting this error duing building Android on Windows.
I had "ios" folder under cordova/platform. I deleteed "ios" folder in my Windows and it worked!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment