Skip to content
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-zz opened this issue Jan 18, 2017 · 50 comments
Open

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

cyclonmaster-zz opened this issue Jan 18, 2017 · 50 comments

Comments

@cyclonmaster-zz
Copy link

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="[email protected]" 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?

@ssarnot
Copy link

ssarnot commented Jan 18, 2017

+1

1 similar comment
@rayan-sta
Copy link

+1

@ilman
Copy link

ilman commented Jan 18, 2017

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

@Aries-UA
Copy link

+1

@Silberling
Copy link

Silberling commented Jan 21, 2017

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

@Silberling
Copy link

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
Copy link

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-zz
Copy link
Author

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

Still; get the same problem. Any help here?

@didinj
Copy link

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
Copy link

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
Copy link

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
Copy link

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
Copy link

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

@cluny85
Copy link

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
Copy link

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
Copy link

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
Copy link

@circuitrider solution nailed it for me.

@jaybowman
Copy link

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
Copy link

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
Copy link

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
Copy link

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
Copy link

skout90 commented Dec 20, 2017

+ 1.. Problem still exists...

@jeenn
Copy link

jeenn commented Dec 20, 2017

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

@walfridosp
Copy link

@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
Copy link

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
Copy link

@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
Copy link

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
Copy link

jeenn commented Dec 26, 2017

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

@skout90
Copy link

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
Copy link

maocop commented Jan 4, 2018

dont forget delete the plataform folder

@Grohden
Copy link

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
Cordova 7.0+ changed the android paths structure, this should fix that problem and also maintain compatibility with older versions
@ketanyekale
Copy link

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
Copy link

ferozsho commented Jan 16, 2018

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

@chamathpali
Copy link

@ketanyekale works! 👍 Thanks

@krinakis
Copy link

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
Copy link

thenb commented Feb 27, 2018

@ketanyekale works!

@ketanyekale
Copy link

@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
Copy link

solution from @ketanyekale did the trick
I'm running [email protected] on my project.

@benag
Copy link

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
Copy link

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
Copy link

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.

@salazarr-js
Copy link

tested the @ketanyekale solution in [email protected] and works perfectly but, when will be implemented in master?

@SahSantoshh
Copy link

@ketanyekale fix work perfectly in [email protected]. Thanks.

@fennsaji
Copy link

@ketanyekale Thanks your solution fixed the problem ([email protected])

@samuelaj1
Copy link

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

@laberg
Copy link

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
Copy link

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!

aryeharmon added a commit to aryeharmon/cordova-plugin-fcm that referenced this issue Mar 19, 2019
fixed issue fechanique#213
strings.xml path has changed in later version
@aryeharmon
Copy link

pull request made to fix this issue #575

@eugenioghio
Copy link

eugenioghio commented May 14, 2019

I am still getting this error with [email protected]
I got:

  • Ionic CLI 4.12.0
  • Cordova FCM plugin v2.1.2
  • Cordova 9.0.0 ([email protected])
  • Gradle 5.2.1
  • Kotlin DSL: 1.1.3
  • Kotlin: 1.3.20
  • Groovy: 2.5.4
  • Ant: Apache Ant(TM) version 1.9.13 compiled on July 10 2018
  • JVM: 1.8.0_201 (Oracle Corporation 25.201-b09)
  • OS: Windows 10 10.0 amd64
Cordova FCM plugin v2.1.2 installed
		For more details visit https://github.com/fechanique/cordova-plugin-fcm
	
Adding cordova-plugin-fcm to package.json
Failed to restore plugin "cordova-plugin-fcm" from config.xml. You might need to try adding it again. Error: TypeError: Invalid data, chunk must be a string or buffer, not object
Discovered saved plugin "cordova-plugin-firebase". Adding it to the project
Installing "cordova-plugin-firebase" for android
Subproject Path: CordovaLib
Subproject Path: app
Adding cordova-plugin-firebase to package.json
Failed to restore plugin "cordova-plugin-firebase" from config.xml. You might need to try adding it again. Error: TypeError: Invalid data, chunk must be a string or buffer, not object
cordova-android-support-gradle-release: Android platform: V7+
cordova-android-support-gradle-release: Wrote custom version '27.+' to /builds/project-0/platforms/android/app/build.gradle
cordova-android-support-gradle-release: Wrote custom version '27.+' to /builds/project-0/platforms/android/cordova-android-support-gradle-release/progestNowApp-cordova-android-support-gradle-release.gradle
Invalid data, chunk must be a string or buffer, not object

@johnsonfash
Copy link

for those still facing this issue

  1. create new firebase app on firebase website
  2. cordova create myapp app.android.myapp myapp
  3. cordova plugin add cordova-plugin-firebasex
  4. cordova platform add android
  5. cordova build android (if error, open the gradle.properties and change androidx to true and rebuild or change it on android studio and rebuild

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests