【问题标题】:Invalid data, chunk must be a string or buffer, not object - ionic and firebase无效数据,块必须是字符串或缓冲区,而不是对象 - 离子和火力
【发布时间】:2018-08-20 23:21:28
【问题描述】:

我目前正在我的 MAC 上设置我的 ionic 应用程序,但我一直遇到一个问题,当我使用 Cordova 版本 8.0.0 和 android 版本 6.4.0 添加 cordova-plugin-fcm 时,我得到以下错误:

无效数据,chunk必须是字符串或缓冲区,而不是对象

请注意,我的应用上没有安装 ios 平台。另外,由于我安装了 FCM 插件,我已将 google-services.json 文件添加到我的项目的根目录中。

最后,最奇怪的是,当我使用 PC 时,我的项目运行良好。但是,在我的 MAC(我一直使用它)上,不适用于我的项目。

知道为什么这不起作用。仅供参考 - 我在网上尝试了很多解决方案

  • 卸载并重新安装cordova
  • cordova 版本 7.1.0
  • 从 cordova-plugin-fcm 文件夹 fcm_config_files_process.js 文件中注释掉以下行:

-

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

任何想法我做错了什么?

【问题讨论】:

  • 有人有什么想法吗?

标签: android cordova npm ionic2 firebase-cloud-messaging


【解决方案1】:

通过 cordova-plugin-fcm Github 问题提供的解决方案根据 100 多个用户的支持和 cmets 起作用。

感谢 Cesar Cruz 提及。 +点赞

它适用于 Ionic,因为它是我的 Ionic 项目的解决方案。

我的解决方案摘录:https://github.com/fechanique/cordova-plugin-fcm/issues/213#issuecomment-357162384

修复方法是替换 /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;
    }
}
  1. 确保您已将“plugins”文件夹中的上述文件复制为 cordova 从 node_modules 目录复制所有的cordova-plugins 到插件目录。
  2. 如果您在修改文件之前已经添加了平台 插件/cordova-plugin-fcm/scripts/fcm_config_files_process.js,你 需要删除平台并重新添加。

【讨论】:

    【解决方案2】:

    看看@ketanyekale 的解决方案https://github.com/fechanique/cordova-plugin-fcm/issues/213#issuecomment-357162384 它可能有用,但不适用于离子。

    也许你能找到解决问题的线索。

    它在 cordova/phonegap 项目中为我工作。

    【讨论】:

    • 鼓励链接到外部资源,但请在链接周围添加上下文,以便您的其他用户了解它是什么以及为什么存在。始终引用重要链接中最相关的部分,以防目标站点无法访问或永久离线。参考stackoverflow.com/help/how-to-answer
    • 是的,谢谢@cesar-cruz,该解决方案确实对我有用。或者,您可以使用以下链接提供解决方案的直接链接:github.com/fechanique/cordova-plugin-fcm/issues/…
    猜你喜欢
    • 2023-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-17
    • 2022-07-14
    • 2015-02-28
    • 1970-01-01
    相关资源
    最近更新 更多