Skip to content

Commit dc90128

Browse files
authored
Add FCM ktx library. (#1899)
* Add FCM ktx library. * Update api.txt * Disable ftl
1 parent 70bc56c commit dc90128

File tree

11 files changed

+275
-3
lines changed

11 files changed

+275
-3
lines changed

firebase-messaging-directboot/src/main/AndroidManifest.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
-->
1616
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
1717
package="com.google.firebase.messaging.directboot">
18-
<uses-sdk android:minSdkVersion="16"/>
18+
<!--Although the *SdkVersion is captured in gradle build files, this is required for non gradle builds-->
19+
<!--<uses-sdk android:minSdkVersion="16"/>-->
1920

2021
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
2122
<uses-permission android:name="android.permission.INTERNET" />

firebase-messaging/api.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22
package com.google.firebase.messaging {
33

44
public class FirebaseMessaging {
5-
method @NonNull public boolean deliveryMetricsExportToBigQueryEnabled();
5+
method @Deprecated public boolean deliveryMetricsExportToBigQueryEnabled();
66
method @NonNull public static com.google.firebase.messaging.FirebaseMessaging getInstance();
77
method public boolean isAutoInitEnabled();
8+
method public boolean isDeliveryMetricsExportToBigQueryEnabled();
89
method public void send(@NonNull com.google.firebase.messaging.RemoteMessage);
910
method public void setAutoInitEnabled(boolean);
1011
method public void setDeliveryMetricsExportToBigQuery(boolean);

firebase-messaging/ktx/api.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Signature format: 2.0
2+
package com.google.firebase.messaging.ktx {
3+
4+
public final class MessagingKt {
5+
ctor public MessagingKt();
6+
method @NonNull public static com.google.firebase.messaging.FirebaseMessaging getMessaging(@NonNull com.google.firebase.ktx.Firebase);
7+
method @NonNull public static inline com.google.firebase.messaging.RemoteMessage remoteMessage(@NonNull String to, @NonNull kotlin.jvm.functions.Function1<? super com.google.firebase.messaging.RemoteMessage.Builder,kotlin.Unit> init);
8+
}
9+
10+
}
11+
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
android.enableUnitTestBinaryResources=true
2+

firebase-messaging/ktx/ktx.gradle

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Copyright 2020 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
plugins {
16+
id 'firebase-library'
17+
id 'kotlin-android'
18+
}
19+
20+
firebaseLibrary {
21+
releaseWith project(':firebase-messaging')
22+
publishJavadoc = true
23+
publishSources = true
24+
}
25+
26+
android {
27+
compileSdkVersion project.targetSdkVersion
28+
defaultConfig {
29+
minSdkVersion 16
30+
multiDexEnabled true
31+
targetSdkVersion project.targetSdkVersion
32+
versionName version
33+
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
34+
}
35+
sourceSets {
36+
main.java.srcDirs += 'src/main/kotlin'
37+
test.java.srcDirs += 'src/test/kotlin'
38+
androidTest.java.srcDirs += 'src/androidTest/kotlin'
39+
}
40+
testOptions.unitTests.includeAndroidResources = true
41+
}
42+
43+
dependencies {
44+
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlinVersion"
45+
46+
implementation project(':firebase-common')
47+
implementation project(':firebase-common:ktx')
48+
implementation project(':firebase-components')
49+
implementation project(':firebase-messaging')
50+
implementation "com.google.android.gms:play-services-basement:17.2.1"
51+
52+
testImplementation "org.robolectric:robolectric:$robolectricVersion"
53+
testImplementation 'junit:junit:4.12'
54+
testImplementation "com.google.truth:truth:$googleTruthVersion"
55+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!-- Copyright (C) 2012 The Android Open Source Project
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+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
17+
package="com.google.firebase.messaging.ktx">
18+
<!--<uses-sdk android:minSdkVersion="16"/>-->
19+
<application>
20+
<service android:exported="false"
21+
android:name="com.google.firebase.components.ComponentDiscoveryService">
22+
<meta-data
23+
android:name="com.google.firebase.components:com.google.firebase.messaging.ktx.FirebaseMessagingKtxRegistrar"
24+
android:value="com.google.firebase.components.ComponentRegistrar" />
25+
</service>
26+
</application>
27+
</manifest>
28+
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Copyright 2020 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package com.google.firebase.messaging.ktx
16+
17+
import com.google.firebase.FirebaseApp
18+
import com.google.firebase.components.Component
19+
import com.google.firebase.components.ComponentRegistrar
20+
import com.google.firebase.ktx.Firebase
21+
import com.google.firebase.messaging.FirebaseMessaging
22+
import com.google.firebase.messaging.RemoteMessage
23+
import com.google.firebase.platforminfo.LibraryVersionComponent
24+
25+
internal const val LIBRARY_NAME: String = "fire-fcm-ktx"
26+
27+
/** Returns the [FirebaseMessaging] instance of the default [FirebaseApp]. */
28+
val Firebase.messaging: FirebaseMessaging
29+
get() = FirebaseMessaging.getInstance()
30+
31+
/** Returns a [RemoteMessage] instance initialized using the [init] function. */
32+
inline fun remoteMessage(to: String, crossinline init: RemoteMessage.Builder.() -> Unit): RemoteMessage {
33+
val builder = RemoteMessage.Builder(to)
34+
builder.init()
35+
return builder.build()
36+
}
37+
38+
/** @suppress */
39+
class FirebaseMessagingKtxRegistrar : ComponentRegistrar {
40+
override fun getComponents(): List<Component<*>> =
41+
listOf(LibraryVersionComponent.create(LIBRARY_NAME, BuildConfig.VERSION_NAME))
42+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
// Copyright 2020 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package com.google.firebase.messaging.ktx
16+
17+
import com.google.common.truth.Truth.assertThat
18+
import com.google.firebase.FirebaseApp
19+
import com.google.firebase.FirebaseOptions
20+
import com.google.firebase.ktx.Firebase
21+
import com.google.firebase.ktx.app
22+
import com.google.firebase.ktx.initialize
23+
import com.google.firebase.messaging.FirebaseMessaging
24+
import com.google.firebase.platforminfo.UserAgentPublisher
25+
import org.junit.After
26+
import org.junit.Before
27+
import org.junit.Test
28+
import org.junit.runner.RunWith
29+
import org.robolectric.RobolectricTestRunner
30+
import org.robolectric.RuntimeEnvironment
31+
32+
const val APP_ID = "APP_ID"
33+
const val API_KEY = "API_KEY"
34+
35+
abstract class BaseTestCase {
36+
@Before
37+
fun setUp() {
38+
Firebase.initialize(
39+
RuntimeEnvironment.application,
40+
FirebaseOptions.Builder()
41+
.setApplicationId(APP_ID)
42+
.setApiKey(API_KEY)
43+
.setProjectId("123")
44+
.build()
45+
)
46+
}
47+
48+
@After
49+
fun cleanUp() {
50+
FirebaseApp.clearInstancesForTest()
51+
}
52+
}
53+
54+
@RunWith(RobolectricTestRunner::class)
55+
class MessagingTests : BaseTestCase() {
56+
57+
@Test
58+
fun `messaging should delegate to FirebaseMessaging#getInstance()`() {
59+
assertThat(Firebase.messaging).isSameInstanceAs(FirebaseMessaging.getInstance())
60+
}
61+
62+
@Test
63+
fun `remoteMessage() should produce correct RemoteMessages`() {
64+
val recipient = "recipient"
65+
val expectedCollapseKey = "collapse"
66+
val msgId = "id"
67+
val msgType = "type"
68+
val timeToLive = 100
69+
val remoteMessage = remoteMessage(recipient) {
70+
collapseKey = expectedCollapseKey
71+
messageId = msgId
72+
messageType = msgType
73+
ttl = timeToLive
74+
addData("hello", "world")
75+
}
76+
assertThat(remoteMessage.to).isEqualTo(recipient)
77+
assertThat(remoteMessage.collapseKey).isEqualTo(expectedCollapseKey)
78+
assertThat(remoteMessage.messageId).isEqualTo(msgId)
79+
assertThat(remoteMessage.messageType).isEqualTo(msgType)
80+
assertThat(remoteMessage.data).isEqualTo(mapOf("hello" to "world"))
81+
}
82+
}
83+
84+
@RunWith(RobolectricTestRunner::class)
85+
class LibraryVersionTest : BaseTestCase() {
86+
@Test
87+
fun `library version should be registered with runtime`() {
88+
val publisher = Firebase.app.get(UserAgentPublisher::class.java)
89+
assertThat(publisher.userAgent).contains(LIBRARY_NAME)
90+
}
91+
}

firebase-messaging/src/main/java/com/google/firebase/messaging/FirebaseMessaging.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,9 +162,19 @@ public void setAutoInitEnabled(boolean enable) {
162162
* Determines whether Firebase Cloud Messaging exports message delivery metrics to BigQuery.
163163
*
164164
* @return true if Firebase Cloud Messaging exports message delivery metrics to BigQuery.
165+
* @deprecated Use {@link #isDeliveryMetricsExportToBigQueryEnabled()} instead.
165166
*/
166-
@NonNull
167+
@Deprecated
167168
public boolean deliveryMetricsExportToBigQueryEnabled() {
169+
return isDeliveryMetricsExportToBigQueryEnabled();
170+
}
171+
172+
/**
173+
* Determines whether Firebase Cloud Messaging exports message delivery metrics to BigQuery.
174+
*
175+
* @return true if Firebase Cloud Messaging exports message delivery metrics to BigQuery.
176+
*/
177+
public boolean isDeliveryMetricsExportToBigQueryEnabled() {
168178
return MessagingAnalytics.deliveryMetricsExportToBigQueryEnabled();
169179
}
170180

firebase-messaging/src/main/java/com/google/firebase/messaging/RemoteMessage.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,12 @@ public Builder setData(@NonNull Map<String, String> data) {
346346
return this;
347347
}
348348

349+
/** @hide */
350+
@NonNull
351+
public Map<String, String> getData() {
352+
return this.data;
353+
}
354+
349355
/** Clears the message data. */
350356
@NonNull
351357
public Builder clearData() {
@@ -373,13 +379,25 @@ public Builder setMessageId(@NonNull String messageId) {
373379
return this;
374380
}
375381

382+
/** @hide */
383+
@NonNull
384+
public String getMessageId() {
385+
return bundle.getString(MessagePayloadKeys.MSGID, "");
386+
}
387+
376388
/** Sets the type of message. */
377389
@NonNull
378390
public Builder setMessageType(@Nullable String messageType) {
379391
bundle.putString(MessagePayloadKeys.MESSAGE_TYPE, messageType);
380392
return this;
381393
}
382394

395+
/** @hide */
396+
@Nullable
397+
public String getMessageType() {
398+
return bundle.getString(MessagePayloadKeys.MESSAGE_TYPE);
399+
}
400+
383401
/**
384402
* Sets the message time to live in seconds.
385403
*
@@ -392,6 +410,12 @@ public Builder setTtl(@IntRange(from = 0, to = 86400) int ttl) {
392410
return this;
393411
}
394412

413+
/** @hide */
414+
@IntRange(from = 0, to = 86400)
415+
public int getTtl() {
416+
return Integer.parseInt(bundle.getString(MessagePayloadKeys.MESSAGE_TYPE, "0"));
417+
}
418+
395419
/**
396420
* Sets the collapse key of the message.
397421
*
@@ -403,6 +427,12 @@ public Builder setCollapseKey(@Nullable String collapseKey) {
403427
bundle.putString(MessagePayloadKeys.COLLAPSE_KEY, collapseKey);
404428
return this;
405429
}
430+
431+
/** @hide */
432+
@Nullable
433+
public String getCollapseKey() {
434+
return bundle.getString(MessagePayloadKeys.MESSAGE_TYPE);
435+
}
406436
}
407437

408438
/**

subprojects.cfg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ firebase-firestore:ktx
2020
firebase-functions
2121
firebase-functions:ktx
2222
firebase-messaging
23+
firebase-messaging:ktx
2324
firebase-messaging-directboot
2425
firebase-inappmessaging
2526
firebase-inappmessaging:ktx

0 commit comments

Comments
 (0)