|
14 | 14 | package com.google.firebase.messaging.directboot;
|
15 | 15 |
|
16 | 16 | import android.content.Context;
|
17 |
| -import android.content.Intent; |
18 | 17 | import android.util.Log;
|
19 |
| -import androidx.legacy.content.WakefulBroadcastReceiver; |
20 |
| -import com.google.android.gms.common.util.concurrent.NamedThreadFactory; |
21 |
| -import com.google.firebase.iid.FcmBroadcastProcessor; |
22 |
| -import com.google.firebase.iid.ServiceStarter; |
23 |
| -import com.google.firebase.messaging.directboot.threads.PoolableExecutors; |
24 |
| -import com.google.firebase.messaging.directboot.threads.ThreadPriority; |
25 |
| -import java.util.concurrent.ExecutorService; |
| 18 | +import androidx.annotation.NonNull; |
| 19 | +import androidx.annotation.WorkerThread; |
| 20 | +import com.google.android.gms.cloudmessaging.CloudMessage; |
| 21 | +import com.google.android.gms.cloudmessaging.CloudMessagingReceiver; |
| 22 | +import com.google.android.gms.tasks.Tasks; |
| 23 | +import com.google.firebase.messaging.FcmBroadcastProcessor; |
| 24 | +import com.google.firebase.messaging.ServiceStarter; |
| 25 | +import java.util.concurrent.ExecutionException; |
26 | 26 |
|
27 | 27 | /**
|
28 |
| - * WakefulBroadcastReceiver that receives FirebaseMessaging events and delivers them to the |
29 |
| - * application-specific {@link com.google.firebase.iid.FirebaseInstanceIdService} subclass in direct |
30 |
| - * boot mode. |
| 28 | + * BroadcastReceiver that receives FirebaseMessaging events and delivers them to the |
| 29 | + * application-specific {@link com.google.firebase.messaging.FirebaseMessagingService} subclass in |
| 30 | + * direct boot mode. |
31 | 31 | *
|
32 | 32 | * <p>This receiver is automatically added to your application's manifest file via manifest merge.
|
33 | 33 | * If necessary it can be manually declared via:
|
|
49 | 49 | *
|
50 | 50 | * @hide
|
51 | 51 | */
|
52 |
| -public final class FirebaseMessagingDirectBootReceiver extends WakefulBroadcastReceiver { |
| 52 | +public final class FirebaseMessagingDirectBootReceiver extends CloudMessagingReceiver { |
53 | 53 |
|
54 | 54 | /** TAG for log statements coming from FCM */
|
55 | 55 | static final String TAG = "FCM";
|
56 | 56 |
|
57 |
| - /** Action for FCM direct boot message intents */ |
58 |
| - private static final String ACTION_DIRECT_BOOT_REMOTE_INTENT = |
59 |
| - "com.google.firebase.messaging.RECEIVE_DIRECT_BOOT"; |
60 |
| - |
61 |
| - /** All broadcasts get processed on this executor. */ |
62 |
| - private final ExecutorService processorExecutor = |
63 |
| - PoolableExecutors.factory() |
64 |
| - .newSingleThreadExecutor( |
65 |
| - new NamedThreadFactory("fcm-db-intent-handle"), ThreadPriority.LOW_POWER); |
66 |
| - |
| 57 | + /** @hide */ |
67 | 58 | @Override
|
68 |
| - public void onReceive(Context context, Intent intent) { |
69 |
| - if (intent == null) { |
70 |
| - return; |
71 |
| - } |
72 |
| - if (!ACTION_DIRECT_BOOT_REMOTE_INTENT.equals(intent.getAction())) { |
73 |
| - Log.d(TAG, "Unexpected intent: " + intent.getAction()); |
74 |
| - return; |
| 59 | + @WorkerThread |
| 60 | + protected int onMessageReceive(@NonNull Context context, @NonNull CloudMessage message) { |
| 61 | + try { |
| 62 | + return Tasks.await(new FcmBroadcastProcessor(context).process(message.getIntent())); |
| 63 | + } catch (ExecutionException | InterruptedException e) { |
| 64 | + Log.e(TAG, "Failed to send message to service.", e); |
| 65 | + return ServiceStarter.ERROR_UNKNOWN; |
75 | 66 | }
|
76 |
| - |
77 |
| - // Just pass the intent to the service mostly unchanged. |
78 |
| - // Clear the component and ensure package name is set so that the standard dispatching |
79 |
| - // mechanism will find the right service in the app. |
80 |
| - intent.setComponent(null); |
81 |
| - intent.setPackage(context.getPackageName()); |
82 |
| - |
83 |
| - // We don't actually want to process this broadcast on the main thread, so we're going to use |
84 |
| - // goAsync to deal with this in the background. Unfortunately, we need to check whether the |
85 |
| - // broadcast was ordered (and thus needs a result) before calling goAsync, because once we've |
86 |
| - // called goAsync then isOrderedBroadcast will always return false. |
87 |
| - boolean needsResult = isOrderedBroadcast(); |
88 |
| - PendingResult pendingBroadcastResult = goAsync(); |
89 |
| - |
90 |
| - new FcmBroadcastProcessor(context, processorExecutor) |
91 |
| - .process(intent) |
92 |
| - .addOnCompleteListener( |
93 |
| - processorExecutor, |
94 |
| - resultCodeTask -> { |
95 |
| - // If we call setResultCode on a non-ordered broadcast it'll throw, so only set the |
96 |
| - // result if the broadcast was ordered |
97 |
| - if (needsResult) { |
98 |
| - pendingBroadcastResult.setResultCode( |
99 |
| - resultCodeTask.isSuccessful() |
100 |
| - ? resultCodeTask.getResult() |
101 |
| - : ServiceStarter.ERROR_UNKNOWN); |
102 |
| - } |
103 |
| - pendingBroadcastResult.finish(); |
104 |
| - }); |
105 | 67 | }
|
106 | 68 | }
|
0 commit comments