fechanique / cordova-plugin-fcm

Google FCM Push Notifications Cordova Plugin
624 stars 990 forks source link

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

Open cyclonmaster-zz opened 7 years ago

cyclonmaster-zz commented 7 years ago

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'?>

FCMtest A sample Apache Cordova application that responds to the deviceready event. Apache Cordova Team

`

Any help?

ssarnot commented 7 years ago

+1

rayan-sta commented 7 years ago

+1

ilman commented 7 years ago

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

Aries-UA commented 7 years ago

+1

Silberling commented 7 years ago

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

Silberling commented 7 years ago

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 commented 7 years ago

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 commented 7 years ago

I add the string:

@string/google_app_id @string/google_api_key

Still; get the same problem. Any help here?

didinj commented 7 years ago

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 commented 7 years ago

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 commented 7 years ago

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 commented 6 years ago

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 commented 6 years ago

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

cluny85 commented 6 years ago

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 commented 6 years ago

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 commented 6 years ago

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 commented 6 years ago

@circuitrider solution nailed it for me.

jaybowman commented 6 years ago

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 commented 6 years ago

[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 commented 6 years ago

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 commented 6 years ago

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 commented 6 years ago

+ 1.. Problem still exists...

jeenn commented 6 years ago

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

walfridosp commented 6 years ago

@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 commented 6 years ago

@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

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

walfridosp commented 6 years ago

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

skout90 commented 6 years ago

In android i solved.

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

then error message disappeared

jeenn commented 6 years ago

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

skout90 commented 6 years ago

@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 commented 6 years ago

dont forget delete the plataform folder

Grohden commented 6 years ago

@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

ketanyekale commented 6 years ago

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 commented 6 years ago

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

chamathpali commented 6 years ago

@ketanyekale works! 👍 Thanks

krinakis commented 6 years ago

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

thenb commented 6 years ago

@ketanyekale works!

ketanyekale commented 6 years ago

@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 commented 6 years ago

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

benag commented 6 years ago

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 commented 6 years ago

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 commented 6 years ago

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 commented 6 years ago

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

SahSantoshh commented 6 years ago

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

fennsaji commented 6 years ago

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

samuelaj1 commented 5 years ago

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

laberg commented 5 years ago

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 commented 5 years ago

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 commented 5 years ago

pull request made to fix this issue https://github.com/fechanique/cordova-plugin-fcm/pull/575

eugenioghio commented 5 years ago

I am still getting this error with cordova-android@8.0.0 I got:

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 commented 2 years ago

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