Skip to content

Commit 02fd66d

Browse files
Functions: replace InstanceID by Messaging (master) (#6395)
* FIRMessagingInterop introduced * Typo fix * Use FIRMessagingInterop in functions * Functions tests fix * Delete FUNInstanceIDProxy * InstanceID cleanup * ./scripts/style.sh * Reduce FIRMessagingInterop to required minimum * Cleanup * Functions changelog * Fix FIRMessagingInteropFake.h import * Update integration tests * Fix start.sh script * GHA: add FCM interop to funstions paths
1 parent 3cbfc65 commit 02fd66d

File tree

18 files changed

+131
-163
lines changed

18 files changed

+131
-163
lines changed

.github/workflows/functions.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ on:
66
- 'Functions**'
77
- '.github/workflows/functions.yml'
88
- 'Interop/Auth/Public/*.h'
9+
- 'FirebaseMessaging/Sources/Interop/*.h'
910
- 'Gemfile'
1011
schedule:
1112
# Run every day at 11pm (PST) - cron uses UTC times

FirebaseFunctions.podspec

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ Cloud Functions for Firebase.
2727
'Functions/FirebaseFunctions/**/*',
2828
'Interop/Auth/Public/*.h',
2929
'FirebaseCore/Sources/Private/*.h',
30+
'FirebaseMessaging/Sources/Interop/FIRMessagingInterop.h',
3031
]
3132
s.public_header_files = 'Functions/FirebaseFunctions/Public/FirebaseFunctions/*.h'
3233

@@ -40,13 +41,18 @@ Cloud Functions for Firebase.
4041
}
4142

4243
s.test_spec 'unit' do |unit_tests|
43-
unit_tests.source_files = 'Functions/Example/Test*/*.[mh]', 'SharedTestUtilities/FIRAuthInteropFake*'
44+
unit_tests.source_files = [
45+
'Functions/Example/Test*/*.[mh]',
46+
'SharedTestUtilities/FIRAuthInteropFake*',
47+
'SharedTestUtilities/FIRMessagingInteropFake*',
48+
]
4449
end
4550

4651
s.test_spec 'integration' do |int_tests|
4752
int_tests.source_files = 'Functions/Example/IntegrationTests/*.[mh]',
4853
'Functions/Example/TestUtils/*.[mh]',
4954
'SharedTestUtilities/FIRAuthInteropFake*',
55+
'SharedTestUtilities/FIRMessagingInteropFake*',
5056
'Functions/Example/GoogleService-Info.plist'
5157
end
5258
end

FirebaseMessaging/Sources/FIRMessaging.m

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#import <FirebaseMessaging/FIRMessaging.h>
2424
#import <FirebaseMessaging/FIRMessagingExtensionHelper.h>
2525
#import "FirebaseCore/Sources/Private/FirebaseCoreInternal.h"
26+
#import "FirebaseMessaging/Sources/Interop/FIRMessagingInterop.h"
2627
#import "GoogleUtilities/AppDelegateSwizzler/Private/GULAppDelegateSwizzler.h"
2728
#import "GoogleUtilities/Reachability/Private/GULReachabilityChecker.h"
2829
#import "GoogleUtilities/UserDefaults/Private/GULUserDefaults.h"
@@ -161,21 +162,14 @@ @interface FIRMessaging () <FIRMessagingClientDelegate,
161162

162163
@end
163164

164-
// Messaging doesn't provide any functionality to other components,
165-
// so it provides a private, empty protocol that it conforms to and use it for registration.
166-
167-
@protocol FIRMessagingInstanceProvider
168-
@end
169-
170-
@interface FIRMessaging () <FIRMessagingInstanceProvider, FIRLibrary>
165+
@interface FIRMessaging () <FIRMessagingInterop, FIRLibrary>
171166
@end
172167

173168
@implementation FIRMessaging
174169

175170
+ (FIRMessaging *)messaging {
176171
FIRApp *defaultApp = [FIRApp defaultApp]; // Missing configure will be logged here.
177-
id<FIRMessagingInstanceProvider> instance =
178-
FIR_COMPONENT(FIRMessagingInstanceProvider, defaultApp.container);
172+
id<FIRMessagingInterop> instance = FIR_COMPONENT(FIRMessagingInterop, defaultApp.container);
179173

180174
// We know the instance coming from the container is a FIRMessaging instance, cast it and move on.
181175
return (FIRMessaging *)instance;
@@ -241,7 +235,7 @@ + (void)load {
241235
return messaging;
242236
};
243237
FIRComponent *messagingProvider =
244-
[FIRComponent componentWithProtocol:@protocol(FIRMessagingInstanceProvider)
238+
[FIRComponent componentWithProtocol:@protocol(FIRMessagingInterop)
245239
instantiationTiming:FIRInstantiationTimingEagerInDefaultApp
246240
dependencies:@[ analyticsDep ]
247241
creationBlock:creationBlock];
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright 2020 Google LLC
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 <Foundation/Foundation.h>
18+
19+
NS_ASSUME_NONNULL_BEGIN
20+
21+
/** Connector for bridging communication between Firebase SDKs and FIRMessaging API. */
22+
@protocol FIRMessagingInterop <NSObject>
23+
24+
/**
25+
* The FCM registration token is used to identify this device so that FCM can send notifications to
26+
* it. It is associated with your APNs token when the APNs token is supplied, so messages sent to
27+
* the FCM token will be delivered over APNs.
28+
*
29+
* The FCM registration token is sometimes refreshed automatically. In your FIRMessaging delegate,
30+
* the delegate method `messaging:didReceiveRegistrationToken:` will be called once a token is
31+
* available, or has been refreshed. Typically it should be called once per app start, but
32+
* may be called more often if the token is invalidated or updated.
33+
*
34+
* Once you have an FCM registration token, you should send it to your application server, so it can
35+
* use the FCM token to send notifications to your device.
36+
*/
37+
@property(nonatomic, readonly, nullable) NSString *FCMToken NS_SWIFT_NAME(fcmToken);
38+
39+
@end
40+
41+
NS_ASSUME_NONNULL_END

Functions/Backend/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ exports.tokenTest = functions.https.onRequest((request, response) => {
5252
response.send({ data: {} });
5353
});
5454

55-
exports.instanceIdTest = functions.https.onRequest((request, response) => {
56-
assert.equal(request.get('Firebase-Instance-ID-Token'), 'iid');
55+
exports.FCMTokenTest = functions.https.onRequest((request, response) => {
56+
assert.equal(request.get('Firebase-Instance-ID-Token'), 'fakeFCMToken');
5757
assert.deepEqual(request.body, { data: {} });
5858
response.send({ data: {} });
5959
});

Functions/Backend/start.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ FUNCTIONS_BIN="./node_modules/.bin/functions"
4646
"${FUNCTIONS_BIN}" deploy dataTest --trigger-http
4747
"${FUNCTIONS_BIN}" deploy scalarTest --trigger-http
4848
"${FUNCTIONS_BIN}" deploy tokenTest --trigger-http
49-
"${FUNCTIONS_BIN}" deploy instanceIdTest --trigger-http
49+
"${FUNCTIONS_BIN}" deploy FCMTokenTest --trigger-http
5050
"${FUNCTIONS_BIN}" deploy nullTest --trigger-http
5151
"${FUNCTIONS_BIN}" deploy missingResultTest --trigger-http
5252
"${FUNCTIONS_BIN}" deploy unhandledErrorTest --trigger-http

Functions/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# v2.8.0
2+
- [changed] Weak dependency on Instance ID replaced by Firebase Messaging. (#6395)
3+
14
# v2.7.0
25
- [changed] Functionally neutral source reorganization. (#5858)
36

Functions/Example/IntegrationTests/FIRIntegrationTests.m

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@
1717
#import "FirebaseCore/Sources/Private/FirebaseCoreInternal.h"
1818

1919
#import "FIRAuthInteropFake.h"
20-
#import "Functions/Example/TestUtils/FUNFakeInstanceID.h"
2120
#import "Functions/FirebaseFunctions/FIRFunctions+Internal.h"
2221
#import "Functions/FirebaseFunctions/Public/FirebaseFunctions/FIRError.h"
2322
#import "Functions/FirebaseFunctions/Public/FirebaseFunctions/FIRFunctions.h"
2423
#import "Functions/FirebaseFunctions/Public/FirebaseFunctions/FIRHTTPSCallable.h"
24+
#import "SharedTestUtilities/FIRMessagingInteropFake.h"
2525

2626
// Project ID used by these tests.
2727
static NSString *const kDefaultProjectID = @"functions-integration-test";
@@ -30,6 +30,7 @@ @interface FIRIntegrationTests : XCTestCase {
3030
FIRFunctions *_functions;
3131
NSString *_projectID;
3232
BOOL _useLocalhost;
33+
FIRMessagingInteropFake *_messagingFake;
3334
}
3435
@end
3536

@@ -38,6 +39,8 @@ @implementation FIRIntegrationTests
3839
- (void)setUp {
3940
[super setUp];
4041

42+
_messagingFake = [[FIRMessagingInteropFake alloc] init];
43+
4144
_projectID = kDefaultProjectID;
4245
_useLocalhost = YES;
4346

@@ -51,7 +54,8 @@ - (void)setUp {
5154
_functions = [[FIRFunctions alloc]
5255
initWithProjectID:_projectID
5356
region:@"us-central1"
54-
auth:[[FIRAuthInteropFake alloc] initWithToken:nil userID:nil error:nil]];
57+
auth:[[FIRAuthInteropFake alloc] initWithToken:nil userID:nil error:nil]
58+
messaging:_messagingFake];
5559
if (_useLocalhost) {
5660
[_functions useLocalhost];
5761
}
@@ -101,7 +105,8 @@ - (void)testToken {
101105
FIRFunctions *functions = [[FIRFunctions alloc]
102106
initWithProjectID:_projectID
103107
region:@"us-central1"
104-
auth:[[FIRAuthInteropFake alloc] initWithToken:@"token" userID:nil error:nil]];
108+
auth:[[FIRAuthInteropFake alloc] initWithToken:@"token" userID:nil error:nil]
109+
messaging:_messagingFake];
105110
if (_useLocalhost) {
106111
[functions useLocalhost];
107112
}
@@ -117,9 +122,9 @@ - (void)testToken {
117122
[self waitForExpectations:@[ expectation ] timeout:10];
118123
}
119124

120-
- (void)testInstanceID {
125+
- (void)testFCMToken {
121126
XCTestExpectation *expectation = [[XCTestExpectation alloc] init];
122-
FIRHTTPSCallable *function = [_functions HTTPSCallableWithName:@"instanceIdTest"];
127+
FIRHTTPSCallable *function = [_functions HTTPSCallableWithName:@"FCMTokenTest"];
123128
[function callWithObject:@{}
124129
completion:^(FIRHTTPSCallableResult *_Nullable result, NSError *_Nullable error) {
125130
XCTAssertNil(error);

Functions/Example/Tests/FIRFunctionsTests.m

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ - (void)tearDown {
3333
- (void)testURLWithName {
3434
FIRFunctions *functions = [[FIRFunctions alloc] initWithProjectID:@"my-project"
3535
region:@"my-region"
36-
auth:nil];
36+
auth:nil
37+
messaging:nil];
3738
NSString *url = [functions URLWithName:@"my-endpoint"];
3839
XCTAssertEqualObjects(@"https://my-region-my-project.cloudfunctions.net/my-endpoint", url);
3940
}

Functions/Example/Tests/FUNContextProviderTests.m

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,29 @@
1616

1717
#import "FIRAuthInteropFake.h"
1818
#import "Functions/FirebaseFunctions/FUNContext.h"
19+
#import "SharedTestUtilities/FIRMessagingInteropFake.h"
1920

2021
@interface FUNContextProviderTests : XCTestCase
22+
@property(nonatomic) FIRMessagingInteropFake *messagingFake;
2123
@end
2224

2325
@implementation FUNContextProviderTests
2426

27+
- (void)setUp {
28+
self.messagingFake = [[FIRMessagingInteropFake alloc] init];
29+
}
30+
2531
- (void)testContextWithAuth {
2632
FIRAuthInteropFake *auth = [[FIRAuthInteropFake alloc] initWithToken:@"token"
2733
userID:@"userID"
2834
error:nil];
29-
FUNContextProvider *provider = [[FUNContextProvider alloc] initWithAuth:(id<FIRAuthInterop>)auth];
35+
FUNContextProvider *provider = [[FUNContextProvider alloc] initWithAuth:(id<FIRAuthInterop>)auth
36+
messaging:self.messagingFake];
3037
XCTestExpectation *expectation =
3138
[self expectationWithDescription:@"Context should have auth keys."];
3239
[provider getContext:^(FUNContext *_Nullable context, NSError *_Nullable error) {
3340
XCTAssert([context.authToken isEqualToString:@"token"]);
34-
XCTAssert([context.instanceIDToken isEqualToString:@"iid"]);
41+
XCTAssert([context.FCMToken isEqualToString:self.messagingFake.FCMToken]);
3542
XCTAssertNil(error);
3643
[expectation fulfill];
3744
}];
@@ -44,7 +51,8 @@ - (void)testContextWithAuthError {
4451
FIRAuthInteropFake *auth = [[FIRAuthInteropFake alloc] initWithToken:nil
4552
userID:nil
4653
error:authError];
47-
FUNContextProvider *provider = [[FUNContextProvider alloc] initWithAuth:(id<FIRAuthInterop>)auth];
54+
FUNContextProvider *provider = [[FUNContextProvider alloc] initWithAuth:(id<FIRAuthInterop>)auth
55+
messaging:self.messagingFake];
4856
XCTestExpectation *expectation =
4957
[self expectationWithDescription:@"Completion handler should fail with Auth error."];
5058
[provider getContext:^(FUNContext *_Nullable context, NSError *_Nullable error) {
@@ -57,13 +65,13 @@ - (void)testContextWithAuthError {
5765
}
5866

5967
- (void)testContextWithoutAuth {
60-
FUNContextProvider *provider = [[FUNContextProvider alloc] initWithAuth:nil];
68+
FUNContextProvider *provider = [[FUNContextProvider alloc] initWithAuth:nil messaging:nil];
6169
XCTestExpectation *expectation =
6270
[self expectationWithDescription:@"Completion handler should succeed without Auth."];
6371
[provider getContext:^(FUNContext *_Nullable context, NSError *_Nullable error) {
6472
XCTAssertNil(error);
6573
XCTAssertNil(context.authToken);
66-
XCTAssert([context.instanceIDToken isEqualToString:@"iid"]);
74+
XCTAssertNil(context.FCMToken);
6775
[expectation fulfill];
6876
}];
6977

0 commit comments

Comments
 (0)