Skip to content

Commit c517451

Browse files
authored
Use Functions to send test Messages via v1 method (#1598)
* Use Functions to send test Messages via v1 method * Fix lint_commenter issue, and possible lint issue * Update Podfile
1 parent 93bd9be commit c517451

File tree

11 files changed

+149
-204
lines changed

11 files changed

+149
-204
lines changed

messaging/integration_test/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ endif()
237237
# Add the Firebase libraries to the target using the function from the SDK.
238238
add_subdirectory(${FIREBASE_CPP_SDK_DIR} bin/ EXCLUDE_FROM_ALL)
239239
# Note that firebase_app needs to be last in the list.
240-
set(firebase_libs firebase_messaging firebase_app)
240+
set(firebase_libs firebase_functions firebase_messaging firebase_app)
241241
set(gtest_libs gtest gmock)
242242
target_link_libraries(${integration_test_target_name} ${firebase_libs}
243243
${gtest_libs} ${ADDITIONAL_LIBS})

messaging/integration_test/Podfile

+2
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@ use_frameworks! :linkage => :static
55
target 'integration_test' do
66
platform :ios, '13.0'
77
pod 'Firebase/Messaging', '10.25.0'
8+
pod 'Firebase/Functions', '10.25.0'
89
end
910

1011
target 'integration_test_tvos' do
1112
platform :tvos, '12.0'
1213
pod 'Firebase/Messaging', '10.25.0'
14+
pod 'Firebase/Functions', '10.25.0'
1315
end
1416

1517
post_install do |installer|

messaging/integration_test/build.gradle

+5
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,15 @@ android {
7373
proguardFile file('proguard.pro')
7474
}
7575
}
76+
77+
lintOptions {
78+
abortOnError false
79+
}
7680
}
7781

7882
apply from: "$gradle.firebase_cpp_sdk_dir/Android/firebase_dependencies.gradle"
7983
firebaseCpp.dependencies {
84+
functions
8085
messaging
8186
}
8287

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"functions": {
3+
}
4+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/**
2+
* Copyright 2024 Google Inc. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
// Import the Firebase SDK for Google Cloud Functions.
18+
const functions = require('firebase-functions');
19+
// Import and initialize the Firebase Admin SDK.
20+
const admin = require('firebase-admin');
21+
admin.initializeApp();
22+
23+
// Import the FCM SDK
24+
const messaging = admin.messaging();
25+
26+
exports.sendMessage = functions.https.onCall(async (data) => {
27+
const {sendTo, isToken, notificationTitle, notificationBody, messageFields} =
28+
data;
29+
30+
const message = {
31+
notification: {
32+
title: notificationTitle,
33+
body: notificationBody,
34+
},
35+
data: messageFields,
36+
};
37+
38+
if (isToken) {
39+
message.token = sendTo;
40+
} else {
41+
message.topic = sendTo;
42+
}
43+
44+
try {
45+
const response = await messaging.send(message);
46+
console.log('Successfully sent message:', response);
47+
return response; // Optionally return the FCM response to the client
48+
} catch (error) {
49+
console.error('Error sending message:', error);
50+
throw new functions.https.HttpsError('internal', error.message);
51+
}
52+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"name": "messaging-functions",
3+
"description": "Firebase Functions to send Cloud Messages",
4+
"engines": {
5+
"node": "16"
6+
},
7+
"main": "index.js",
8+
"dependencies": {
9+
"firebase-admin": "^12.1.1",
10+
"firebase-functions": "^5.0.1"
11+
},
12+
"private": true
13+
}

0 commit comments

Comments
 (0)