Skip to content

Use Functions to send test Messages via v1 method #1598

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

Merged
merged 3 commits into from
May 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion messaging/integration_test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ endif()
# Add the Firebase libraries to the target using the function from the SDK.
add_subdirectory(${FIREBASE_CPP_SDK_DIR} bin/ EXCLUDE_FROM_ALL)
# Note that firebase_app needs to be last in the list.
set(firebase_libs firebase_messaging firebase_app)
set(firebase_libs firebase_functions firebase_messaging firebase_app)
set(gtest_libs gtest gmock)
target_link_libraries(${integration_test_target_name} ${firebase_libs}
${gtest_libs} ${ADDITIONAL_LIBS})
2 changes: 2 additions & 0 deletions messaging/integration_test/Podfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ use_frameworks! :linkage => :static
target 'integration_test' do
platform :ios, '13.0'
pod 'Firebase/Messaging', '10.25.0'
pod 'Firebase/Functions', '10.25.0'
end

target 'integration_test_tvos' do
platform :tvos, '12.0'
pod 'Firebase/Messaging', '10.25.0'
pod 'Firebase/Functions', '10.25.0'
end

post_install do |installer|
Expand Down
5 changes: 5 additions & 0 deletions messaging/integration_test/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,15 @@ android {
proguardFile file('proguard.pro')
}
}

lintOptions {
abortOnError false
}
}

apply from: "$gradle.firebase_cpp_sdk_dir/Android/firebase_dependencies.gradle"
firebaseCpp.dependencies {
functions
messaging
}

Expand Down
4 changes: 4 additions & 0 deletions messaging/integration_test/functions/firebase.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"functions": {
}
}
52 changes: 52 additions & 0 deletions messaging/integration_test/functions/functions/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/**
* Copyright 2024 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

// Import the Firebase SDK for Google Cloud Functions.
const functions = require('firebase-functions');
// Import and initialize the Firebase Admin SDK.
const admin = require('firebase-admin');
admin.initializeApp();

// Import the FCM SDK
const messaging = admin.messaging();

exports.sendMessage = functions.https.onCall(async (data) => {
const {sendTo, isToken, notificationTitle, notificationBody, messageFields} =
data;

const message = {
notification: {
title: notificationTitle,
body: notificationBody,
},
data: messageFields,
};

if (isToken) {
message.token = sendTo;
} else {
message.topic = sendTo;
}

try {
const response = await messaging.send(message);
console.log('Successfully sent message:', response);
return response; // Optionally return the FCM response to the client
} catch (error) {
console.error('Error sending message:', error);
throw new functions.https.HttpsError('internal', error.message);
}
});
13 changes: 13 additions & 0 deletions messaging/integration_test/functions/functions/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "messaging-functions",
"description": "Firebase Functions to send Cloud Messages",
"engines": {
"node": "16"
},
"main": "index.js",
"dependencies": {
"firebase-admin": "^12.1.1",
"firebase-functions": "^5.0.1"
},
"private": true
}
Loading
Loading