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

Test #2

Open
m2faridi opened this issue Sep 1, 2024 · 3 comments
Open

Test #2

m2faridi opened this issue Sep 1, 2024 · 3 comments

Comments

@m2faridi
Copy link
Owner

m2faridi commented Sep 1, 2024

No description provided.

@m2faridi
Copy link
Owner Author

m2faridi commented Sep 3, 2024

const admin = require('firebase-admin');

// مقداردهی اولیه در مرحله قبلی انجام شد

function sendFcmMessage(token, message) {
const payload = {
notification: {
title: message.title,
body: message.body,
},
data: message.data,
};

admin.messaging().sendToDevice(token, payload)
    .then((response) => {
        console.log('Successfully sent message:', response);
        if (response.failureCount > 0) {
            response.results.forEach((result, index) => {
                const error = result.error;
                if (error) {
                    console.error(`Error sending message to ${token}:`, error);
                    handleFcmError(error, token);
                }
            });
        }
    })
    .catch((error) => {
        console.error('Error sending FCM message:', error);
    });

}

function handleFcmError(error, token) {
switch (error.code) {
case 'messaging/invalid-registration-token':
console.warn(Token ${token} is invalid. Please check the token format.);
break;
case 'messaging/registration-token-not-registered':
console.warn(Token ${token} is not registered. Consider removing it.);
break;
case 'messaging/mismatched-credential':
console.warn(Token ${token} mismatch with sender ID. Check your project configuration.);
break;
default:
console.warn(FCM error for token ${token}: ${error.message});
break;
}
}

// استفاده از تابع برای ارسال پیام
const token = 'FCM_DEVICE_TOKEN';
const message = {
title: 'Test Notification',
body: 'This is a test message sent using Firebase Admin SDK.',
data: {
key1: 'value1',
key2: 'value2'
}
};

sendFcmMessage(token, message);

@m2faridi
Copy link
Owner Author

m2faridi commented Sep 3, 2024

برای تبدیل تمامی آیتم‌ها و آرایه‌ها داخل یک آبجکت به رشته در جاوااسکریپت، می‌توانید از یک تابع بازگشتی استفاده کنید که به صورت عمیق (deep) تمامی آیتم‌های داخل آبجکت و آرایه را بررسی کرده و به رشته تبدیل می‌کند. در ادامه یک نمونه کد برای این کار آورده شده است:

function deepConvertToString(obj) {
    // اگر آیتم یک آرایه باشد، آن را به صورت جداگانه پردازش کن
    if (Array.isArray(obj)) {
        return obj.map(item => deepConvertToString(item));
    }
    
    // اگر آیتم یک آبجکت باشد، به صورت بازگشتی تمامی کلیدها را پردازش کن
    else if (typeof obj === 'object' && obj !== null) {
        const result = {};
        for (let key in obj) {
            if (obj.hasOwnProperty(key)) {
                result[key] = deepConvertToString(obj[key]);
            }
        }
        return result;
    }

    // اگر آیتم غیر از آبجکت یا آرایه باشد، آن را به رشته تبدیل کن
    return String(obj);
}

// نمونه آبجکت برای تست
const complexObject = {
    name: "John",
    age: 30,
    isActive: true,
    scores: [10, 20, 30],
    address: {
        city: "New York",
        postalCode: 10001,
        coords: {
            lat: 40.7128,
            long: -74.0060
        }
    }
};

const stringifiedObject = deepConvertToString(complexObject);
console.log(stringifiedObject);

توضیح کد:

  1. تابع deepConvertToString: این تابع به صورت بازگشتی روی آبجکت‌ها و آرایه‌ها عمل می‌کند. اگر آیتم یک آرایه باشد، هر عنصر آرایه را پردازش می‌کند. اگر آیتم یک آبجکت باشد، تمامی کلیدها و مقادیر آن را پردازش کرده و به رشته تبدیل می‌کند.

  2. تبدیل مقادیر به رشته: برای هر نوع داده‌ای که غیر از آبجکت یا آرایه است (مثل اعداد، بولین‌ها، و غیره)، از String() استفاده می‌کنیم تا به رشته تبدیل شود.

  3. استفاده از تابع: در نهایت، تابع deepConvertToString روی آبجکت اصلی اعمال می‌شود و نتیجه نهایی که شامل تمامی مقادیر به صورت رشته است، چاپ می‌شود.

نتیجه:

پس از اجرای کد، آبجکت complexObject به یک آبجکت جدید تبدیل می‌شود که تمامی مقادیر آن به رشته تبدیل شده‌اند. این روش برای آبجکت‌های پیچیده که شامل تو در تویی از آبجکت‌ها و آرایه‌ها هستند نیز به خوبی کار می‌کند.

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

1 participant