fechanique / cordova-plugin-fcm

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

cordova-plugin-fcm Error: Invalid data, chunk must be a string or buffer, not object #501

Open ARK-JANI opened 6 years ago

ARK-JANI commented 6 years ago

when i add this plugin cordova-plugin-fcm then arise this error in cordova

cordova plugin add cordova-plugin-fcm Installing "cordova-plugin-fcm" for android Android Studio project detected Subproject Path: CordovaLib Subproject Path: app

            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 Saved plugin info for "cordova-plugin-fcm" to config.xml Error: Invalid data, chunk must be a string or buffer, not object

plz help

ragulra commented 6 years ago

plz remove the platform and add the platform , remove the fcm plugin and add the plugin ,

if it is not working kindly add the below code in your

plugins\cordova-plugin-fcm\scripts\fcm_config_files_process.js

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

robyroad commented 6 years ago

@ragulra pasting the code it works! All the pathnames were wrong!

LordDraagonLive commented 6 years ago

@robyroad yah it worked for me as well. Thank you @ragulra. However, it's better if this problem got fixed ASAP. I'm not sure how does this error occurs, is it a bug ? Better mark this issue as a bug then.

marcbru92 commented 6 years ago

I also have this problem. cordova-plugin-fcm/scripts/fcm_config_files_process.js:80:24 I will try to use that fix.

yakitimor commented 6 years ago

Very helpful, it made it work, finally after spending few hours - thanks a lot! This should be fixed...

mhbt commented 6 years ago

Not working for me still.

robyroad commented 6 years ago

By the way i suggest to use this plugin https://ionicframework.com/docs/native/firebase/. I've switched to firebase plugin instead of fcm and it works better!

mhbt commented 6 years ago

I am going to give it a try right now.

trinity31 commented 5 years ago

I have solved this issue by changing the code in line 80 of fcm_config_files_process.js before: process.stdout.write(err); after: process.stdout.write(err.toString());

langaro commented 5 years ago

I have solved this issue by changing the code in line 80 of fcm_config_files_process.js before: process.stdout.write(err); after: process.stdout.write(err.toString());

Thank you so much!

DerrickLePro commented 5 years ago

thanks @robyroad your solution my help to find a solution to a similar error

sureshthite commented 5 years ago

I have solved this issue by changing the code in line 80 of fcm_config_files_process.js before: process.stdout.write(err); after: process.stdout.write(err.toString());

Yes, It is absolutely working fine. But, when we are getting registered token using cordova-plugin-fcm then it fails. I am unable to get registered token for sending push notification. Any idea about this?

mydoal commented 4 years ago

For me it was an empty platform\ios folder that caused this error. Use --verbose when running the command e.g.

cordova build browser --verbose

to get a more detailed error message.

notalijaved commented 3 years ago

I have solved this issue by changing the code in line 80 of fcm_config_files_process.js before: process.stdout.write(err); after: process.stdout.write(err.toString());

Thank you so much

mike-nelson commented 3 years ago

Yeah, that code pasted in seems to fix this issue. Quite odd error message that you would not think would be fixed by changing the target paths.

I have forked and updated this file. Feel free to use this one: https://github.com/mike-nelson/cordova-plugin-fcm.git

cordova plugin add https://github.com/mike-nelson/cordova-plugin-fcm.git

pierrot10 commented 3 years ago

plz remove the platform and add the platform , remove the fcm plugin and add the plugin ,

if it is not working kindly add the below code in your

plugins\cordova-plugin-fcm\scripts\fcm_config_files_process.js

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

Thanks regula, but I do not understand what I have to past. The are formated code and code as text. Or should I clear the content of fcm_config_files_process.js and copy your code from

#!/usr/bin/env node

to

function directoryExists(path) {
try {
return fs.statSync(path).isDirectory();
} catch (e) {
return false;
}
}

Why the plugin is not updated? Thanks

pierrot10 commented 3 years ago

the cordova-plugin-fcm look to be very annoying. I added the plugin and I modfied the line

process.stdout.write(err.toString());

As @alijaved59 wrote.

However, now when I run android to my devise

cordova run android --devise

I have go so many errors: I installed the last version of gradle 6.8.3

WARNING: Configuration 'compile' is obsolete and has been replaced with 'implementation' and 'api'.
It will be removed in version 5.0 of the Android [Gradle](https://gradle.org/install/) 

and

> Task :app:processDebugGoogleServices FAILED

FAILURE: Build failed with an exception.

and

BUILD FAILED in 1s
4 actionable tasks: 1 executed, 3 up-to-date
Command failed with exit code 1: /Users/pamey/Documents/Cordova/smartbud/platforms/android/gradlew cdvBuildDebug -b /Users/pamey/Documents/Cordova/smartbud/platforms/android/build.gradle

Note: In the build.gradle file, I have the classes:

buildscript {
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath "com.android.tools.build:gradle:4.1.2"
        classpath "com.google.gms:google-services:4.3.5"

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

If I remove cordova-plugin-fcm, I can run the command

cordova run android --devise

What hapen with gradle? I hope some can help me Thanks a lot