Skip to content

Commit 21a9f91

Browse files
VinayGuthalGoogler
andauthored
Move fcm to github (#2971)
* Project import generated by Copybara. FolderOrigin-RevId: /google/src/cloud/vguthal/fcm_migration/. * update * update * update * update * add resourcrs * move fcm * update * update * update * update tests * update * update * update * update * update * update * update * update * update Co-authored-by: Googler <[email protected]>
1 parent b9f0ab6 commit 21a9f91

File tree

61 files changed

+6167
-1144
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+6167
-1144
lines changed

firebase-messaging/api.txt

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,19 @@
22
package com.google.firebase.messaging {
33

44
public class FirebaseMessaging {
5-
method @Deprecated public boolean deliveryMetricsExportToBigQueryEnabled();
5+
method @NonNull public com.google.android.gms.tasks.Task<java.lang.Void> deleteToken();
6+
method @NonNull public boolean deliveryMetricsExportToBigQueryEnabled();
67
method @NonNull public static com.google.firebase.messaging.FirebaseMessaging getInstance();
8+
method @NonNull public com.google.android.gms.tasks.Task<java.lang.String> getToken();
79
method public boolean isAutoInitEnabled();
8-
method public boolean isDeliveryMetricsExportToBigQueryEnabled();
10+
method public boolean isNotificationDelegationEnabled();
911
method public void send(@NonNull com.google.firebase.messaging.RemoteMessage);
1012
method public void setAutoInitEnabled(boolean);
1113
method public void setDeliveryMetricsExportToBigQuery(boolean);
14+
method public com.google.android.gms.tasks.Task<java.lang.Void> setNotificationDelegationEnabled(boolean);
1215
method @NonNull public com.google.android.gms.tasks.Task<java.lang.Void> subscribeToTopic(@NonNull String);
1316
method @NonNull public com.google.android.gms.tasks.Task<java.lang.Void> unsubscribeFromTopic(@NonNull String);
14-
field public static final String INSTANCE_ID_SCOPE = "FCM";
17+
field @Deprecated public static final String INSTANCE_ID_SCOPE = "FCM";
1518
}
1619

1720
public class FirebaseMessagingService extends android.app.Service {
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# code_gen_cfg.textproto
2+
# proto-file: google3/third_party/firebase/android/encoders/protoc_gen/proto/config.proto
3+
# proto-message: CodeGenConfig
4+
5+
# all types will be vendored in this package
6+
vendor_package: "com.google.firebase.messaging"
7+
8+
# marks a type as a "root" message
9+
include: "reporting.MessagingClientEventExtension"

firebase-messaging/firebase-messaging.gradle

Lines changed: 51 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,40 @@
1414

1515
plugins {
1616
id 'firebase-library'
17+
id 'com.google.protobuf'
1718
}
1819

20+
configurations.create("protobuild")
21+
dependencies {
22+
protobuild project(path: ":encoders:protoc-gen-firebase-encoders", configuration: "shadow")
23+
}
24+
25+
protobuf {
26+
protoc {
27+
artifact = "com.google.protobuf:protoc:$protocVersion"
28+
}
29+
plugins {
30+
firebaseEncoders {
31+
path = configurations.protobuild.asPath
32+
}
33+
}
34+
generateProtoTasks {
35+
all().each { task ->
36+
task.dependsOn configurations.protobuild
37+
task.inputs.file 'code_gen_cfg.textproto'
38+
task.plugins {
39+
firebaseEncoders {
40+
option file('code_gen_cfg.textproto').path
41+
}
42+
}
43+
task.builtins {
44+
remove java
45+
}
46+
}
47+
}
48+
}
49+
50+
1951
firebaseLibrary {
2052
testLab.enabled = false
2153
}
@@ -25,6 +57,7 @@ android {
2557
timeOutInMs 60 * 1000
2658
}
2759

60+
2861
lintOptions {
2962
abortOnError false
3063
}
@@ -55,18 +88,18 @@ dependencies {
5588
implementation project(':transport:transport-backend-cct')
5689
implementation project(':encoders:firebase-encoders')
5790
implementation project(':encoders:firebase-encoders-json')
91+
implementation project(":encoders:firebase-encoders-proto")
92+
implementation project(":firebase-installations")
93+
annotationProcessor project(":encoders:firebase-encoders-processor")
5894

59-
implementation 'androidx.annotation:annotation:1.1.0'
60-
implementation "com.google.android.gms:play-services-tasks:17.0.2"
61-
implementation "com.google.android.gms:play-services-basement:17.2.1"
62-
implementation 'com.google.android.gms:play-services-base:17.2.1'
63-
implementation 'com.google.firebase:firebase-measurement-connector:18.0.0'
64-
implementation "com.google.firebase:firebase-iid-interop:17.0.0"
65-
implementation ("com.google.firebase:firebase-iid:20.2.3") {
66-
exclude group: "com.google.firebase", module: "firebase-common"
67-
exclude group: "com.google.firebase", module: "firebase-components"
68-
exclude group: "com.google.firebase", module: "firebase-installations-interop"
69-
}
95+
implementation 'androidx.annotation:annotation:1.2.0'
96+
implementation "com.google.android.gms:play-services-tasks:17.2.1"
97+
implementation "com.google.android.gms:play-services-basement:17.6.0"
98+
implementation 'com.google.android.gms:play-services-base:17.6.0'
99+
implementation 'com.google.android.gms:play-services-stats:17.0.1'
100+
implementation 'com.google.firebase:firebase-measurement-connector:19.0.0'
101+
implementation "com.google.firebase:firebase-iid-interop:17.1.0"
102+
implementation 'com.google.android.gms:play-services-cloud-messaging:17.0.0'
70103
implementation ("com.google.api-client:google-api-client:1.30.9") {
71104
exclude group: "org.apache.httpcomponents", module: "httpclient"
72105
}
@@ -76,13 +109,20 @@ dependencies {
76109
javadocClasspath 'com.google.auto.value:auto-value-annotations:1.6.6'
77110

78111
testImplementation 'androidx.test:core:1.2.0'
112+
testImplementation 'com.google.android.gms:play-services-cloud-messaging:17.0.0'
79113
testImplementation 'androidx.test:rules:1.2.0'
80114
testImplementation 'androidx.test:runner:1.2.0'
81115
testImplementation "org.robolectric:robolectric:$robolectricVersion"
82116
testImplementation 'junit:junit:4.13-beta-2'
83117
testImplementation 'org.mockito:mockito-core:2.25.0'
84118
testImplementation "com.google.truth:truth:$googleTruthVersion"
85119
testImplementation 'com.google.android.gms:play-services-vision:19.0.0'
120+
testImplementation ("com.google.firebase:firebase-iid:21.1.0") {
121+
exclude group: "com.google.firebase", module: "firebase-common"
122+
exclude group: "com.google.firebase", module: "firebase-components"
123+
exclude group: "com.google.firebase", module: "firebase-installations-interop"
124+
exclude group: "com.google.firebase", module: "firebase-installations"
125+
}
86126

87127
testImplementation 'com.android.support.test:runner:1.0.2'
88128
testImplementation 'com.fasterxml.jackson.core:jackson-databind:2.9.8'
@@ -91,6 +131,4 @@ dependencies {
91131
testImplementation 'androidx.test:rules:1.2.0'
92132
testImplementation 'androidx.test.ext:truth:1.2.0'
93133
testImplementation 'androidx.test.services:test-services:1.2.0'
94-
95-
96134
}

firebase-messaging/src/main/AndroidManifest.xml

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,21 @@
1515
-->
1616
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
1717
package="com.google.firebase.messaging">
18-
<!--<uses-sdk android:minSdkVersion="16"/>-->
18+
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
19+
<uses-permission android:name="android.permission.WAKE_LOCK" />
20+
<!-- Required by older versions of Google Play services to create IID tokens -->
21+
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
1922
<application>
2023

24+
<receiver
25+
android:name="com.google.firebase.iid.FirebaseInstanceIdReceiver"
26+
android:exported="true"
27+
android:permission="com.google.android.c2dm.permission.SEND" >
28+
<intent-filter>
29+
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
30+
</intent-filter>
31+
</receiver>
32+
2133
<!-- FirebaseMessagingService performs security checks at runtime,
2234
but set to not exported to explicitly avoid allowing another app to call it. -->
2335
<service
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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.iid;
16+
17+
import android.content.Context;
18+
import android.content.Intent;
19+
import android.os.Bundle;
20+
import android.util.Log;
21+
import androidx.annotation.NonNull;
22+
import androidx.annotation.WorkerThread;
23+
import com.google.android.gms.cloudmessaging.CloudMessage;
24+
import com.google.android.gms.cloudmessaging.CloudMessagingReceiver;
25+
import com.google.android.gms.cloudmessaging.CloudMessagingReceiver.IntentActionKeys;
26+
import com.google.android.gms.tasks.Tasks;
27+
import com.google.firebase.messaging.FcmBroadcastProcessor;
28+
import com.google.firebase.messaging.MessagingAnalytics;
29+
import com.google.firebase.messaging.ServiceStarter;
30+
import java.util.concurrent.ExecutionException;
31+
32+
/**
33+
* Implementation of {@code CloudMessagingReceiver} that passes Intents to the {@code
34+
* FirebaseMessagingService}.
35+
*
36+
* @hide
37+
*/
38+
public final class FirebaseInstanceIdReceiver extends CloudMessagingReceiver {
39+
40+
private static final String TAG = "FirebaseMessaging";
41+
42+
private static Intent createServiceIntent(
43+
@NonNull Context context, @NonNull String action, @NonNull Bundle data) {
44+
return new Intent(action).putExtras(data);
45+
}
46+
47+
/** @hide */
48+
@Override
49+
@WorkerThread
50+
protected int onMessageReceive(@NonNull Context context, @NonNull CloudMessage message) {
51+
try {
52+
return Tasks.await(new FcmBroadcastProcessor(context).process(message.getIntent()));
53+
} catch (ExecutionException | InterruptedException e) {
54+
Log.e(TAG, "Failed to send message to service.", e);
55+
return ServiceStarter.ERROR_UNKNOWN;
56+
}
57+
}
58+
59+
/** @hide */
60+
@Override
61+
@WorkerThread
62+
protected void onNotificationDismissed(@NonNull Context context, @NonNull Bundle data) {
63+
// Reconstruct the service intent from the data.
64+
Intent notificationDismissedIntent =
65+
createServiceIntent(context, IntentActionKeys.NOTIFICATION_DISMISS, data);
66+
if (MessagingAnalytics.shouldUploadScionMetrics(notificationDismissedIntent)) {
67+
MessagingAnalytics.logNotificationDismiss(notificationDismissedIntent);
68+
}
69+
}
70+
}

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

Lines changed: 47 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,11 @@
4141
import android.util.Log;
4242
import androidx.annotation.NonNull;
4343
import androidx.annotation.Nullable;
44+
import androidx.annotation.VisibleForTesting;
4445
import androidx.core.app.NotificationCompat;
4546
import androidx.core.content.ContextCompat;
46-
import com.google.firebase.messaging.Constants.IntentActionKeys;
47-
import com.google.firebase.messaging.Constants.IntentKeys;
47+
import com.google.android.gms.cloudmessaging.CloudMessagingReceiver.IntentActionKeys;
48+
import com.google.android.gms.cloudmessaging.CloudMessagingReceiver.IntentKeys;
4849
import com.google.firebase.messaging.Constants.MessageNotificationKeys;
4950
import java.util.concurrent.atomic.AtomicInteger;
5051

@@ -66,6 +67,16 @@ public final class CommonNotificationBuilder {
6667
public static final String FCM_FALLBACK_NOTIFICATION_CHANNEL_LABEL =
6768
"fcm_fallback_notification_channel_label";
6869

70+
// FM's fallback channel name when neither channel name is explicitly provided nor FM's string
71+
// resource file exists (possibly stripped by Progurad). Note this name is set purposefully
72+
// different from FM's default resource channel name "Miscellaneous" for debugability.
73+
private static final String FCM_FALLBACK_NOTIFICATION_CHANNEL_NAME_NO_RESOURCE = "Misc";
74+
private static final String ACTION_MESSAGING_EVENT = "com.google.firebase.MESSAGING_EVENT";
75+
76+
// Getting illegal resouce id from context will throw NotFoundException.
77+
// See: https://developer.android.com/reference/android/content/res/Resources#ID_NULL
78+
private static final int ILLEGAL_RESOURCE_ID = 0;
79+
6980
/**
7081
* Request code used by display notification pending intents.
7182
*
@@ -369,16 +380,16 @@ private static PendingIntent createContentIntent(
369380
// and google.n.).
370381
intent.putExtras(params.paramsWithReservedKeysRemoved());
371382

372-
PendingIntent contentIntent =
373-
PendingIntent.getActivity(
374-
context, generatePendingIntentRequestCode(), intent, PendingIntent.FLAG_ONE_SHOT);
375-
376-
// We need to check metric options against the messageData bundle because we stripped the metric
377-
// options from the clientVisibleData version
378383
if (shouldUploadMetrics(params)) {
379-
contentIntent = wrapContentIntent(context, params, contentIntent);
384+
// store the analytics data in a sub-bundle
385+
intent.putExtra(MessageNotificationKeys.ANALYTICS_DATA, params.paramsForAnalyticsIntent());
380386
}
381-
return contentIntent;
387+
388+
return PendingIntent.getActivity(
389+
context,
390+
generatePendingIntentRequestCode(),
391+
intent,
392+
getPendingIntentFlags(PendingIntent.FLAG_ONE_SHOT));
382393
}
383394

384395
private static Intent createTargetIntent(
@@ -422,7 +433,8 @@ private static Bundle getManifestMetadata(PackageManager pm, String packageName)
422433
}
423434

424435
@TargetApi(VERSION_CODES.O)
425-
private static String getOrCreateChannel(
436+
@VisibleForTesting
437+
public static String getOrCreateChannel(
426438
Context context, String msgChannel, Bundle manifestMetadata) {
427439
if (Build.VERSION.SDK_INT < VERSION_CODES.O) {
428440
return null;
@@ -483,12 +495,24 @@ private static String getOrCreateChannel(
483495
.getIdentifier(
484496
FCM_FALLBACK_NOTIFICATION_CHANNEL_LABEL, "string", context.getPackageName());
485497

498+
String defaultChannelName;
499+
if (channelLabelResourceId == ILLEGAL_RESOURCE_ID) {
500+
Log.e(
501+
TAG,
502+
"String resource \"fcm_fallback_notification_channel_label\" is not found. Using"
503+
+ " default string channel name.");
504+
505+
defaultChannelName = FCM_FALLBACK_NOTIFICATION_CHANNEL_NAME_NO_RESOURCE;
506+
} else {
507+
defaultChannelName = context.getString(channelLabelResourceId);
508+
}
509+
486510
notificationManager.createNotificationChannel(
487511
new NotificationChannel(
488512
// channel id
489513
FCM_FALLBACK_NOTIFICATION_CHANNEL,
490514
// user visible name of the channel
491-
context.getString(channelLabelResourceId),
515+
defaultChannelName,
492516
// shows everywhere, makes noise, but does not visually intrude.
493517
NotificationManager.IMPORTANCE_DEFAULT));
494518
}
@@ -505,16 +529,15 @@ private static int generatePendingIntentRequestCode() {
505529
return requestCodeProvider.incrementAndGet();
506530
}
507531

508-
private static PendingIntent wrapContentIntent(
509-
Context context, NotificationParams params, PendingIntent pi) {
510-
// Need to send analytics, so wrap the activity intent in a PendingIntent that starts the
511-
// FirebaseMessagingService. The service will launch the activity and send the analytics.
512-
Intent openIntent =
513-
new Intent(IntentActionKeys.NOTIFICATION_OPEN)
514-
.putExtras(params.paramsForAnalyticsIntent())
515-
.putExtra(IntentKeys.PENDING_INTENT, pi);
516-
517-
return createMessagingPendingIntent(context, openIntent);
532+
/**
533+
* Adds {@link PendingIntent#FLAG_IMMUTABLE} to a PendingIntent's flags since any PendingIntents
534+
* used here don't need to be modified.
535+
*/
536+
private static int getPendingIntentFlags(int baseFlags) {
537+
// Only add on platform levels that support FLAG_IMMUTABLE.
538+
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
539+
? baseFlags | PendingIntent.FLAG_IMMUTABLE
540+
: baseFlags;
518541
}
519542

520543
@Nullable
@@ -535,11 +558,11 @@ private static PendingIntent createMessagingPendingIntent(Context context, Inten
535558
return PendingIntent.getBroadcast(
536559
context,
537560
generatePendingIntentRequestCode(),
538-
new Intent(IntentActionKeys.MESSAGING_EVENT)
561+
new Intent(ACTION_MESSAGING_EVENT)
539562
.setComponent(
540563
new ComponentName(context, "com.google.firebase.iid.FirebaseInstanceIdReceiver"))
541564
.putExtra(IntentKeys.WRAPPED_INTENT, intent),
542-
PendingIntent.FLAG_ONE_SHOT);
565+
getPendingIntentFlags(PendingIntent.FLAG_ONE_SHOT));
543566
}
544567

545568
/** Check whether we should upload metrics data. */

0 commit comments

Comments
 (0)