Skip to content

Commit 69dbef9

Browse files
Googlerjonsimantov
Googler
authored andcommitted
Using the recommended backwards compatible way of scheduling jobs introduced in Android O.
We moved from using `IntentService` to `JobIntentService` in preparation for this change but the call to schedule jobs remained the same which meant that our custom IntentService was not doing any work at all. Using `JobIntentService.enqueueWork`, when running on Android O or later, the work will be dispatched as a job via JobScheduler.enqueue. When running on older versions of the platform, it will use Context.startService. https://developer.android.com/reference/androidx/core/app/JobIntentService Fixes firebase/quickstart-unity#877 firebase/quickstart-unity#883 PiperOrigin-RevId: 350176964
1 parent 9b04246 commit 69dbef9

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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;
16+
17+
/** Unique ids for JobIntentServices. */
18+
public final class JobIds {
19+
20+
private JobIds() {}
21+
22+
public static final int MESSAGE_FORWARDING_SERVICE = 1000;
23+
}

messaging/src/android/java/com/google/firebase/messaging/MessageForwardingService.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@
1616

1717
import android.content.Context;
1818
import android.content.Intent;
19+
import android.os.Bundle;
1920
import androidx.core.app.JobIntentService;
2021
import com.google.firebase.messaging.cpp.DebugLogging;
2122
import com.google.firebase.messaging.cpp.MessageWriter;
2223

24+
2325
/**
2426
* Listens for Message intents from the application and sends them to the C++ app via the
2527
* ListenerService.
@@ -28,6 +30,14 @@ public class MessageForwardingService extends JobIntentService {
2830
private static final String TAG = "FIREBASE_MSG_FWDR";
2931
public static final String ACTION_REMOTE_INTENT = "com.google.android.c2dm.intent.RECEIVE";
3032

33+
/**
34+
* Convenience wrapper over enqueueWork to either directly start the service (when running on
35+
* pre-O platforms) or enqueue work for it as a job (when running on Android O and later).
36+
*/
37+
public static void enqueueWork(Context context, Intent intent) {
38+
enqueueWork(context, MessageForwardingService.class, JobIds.MESSAGE_FORWARDING_SERVICE, intent);
39+
}
40+
3141
// Handle message intents sent from the ListenerService.
3242
@Override
3343
protected void onHandleWork(Intent intent) {
@@ -46,7 +56,7 @@ static void handleIntent(Context context, Intent intent, MessageWriter messageWr
4656
if (intent != null
4757
&& intent.getAction() != null
4858
&& intent.getAction().equals(ACTION_REMOTE_INTENT)) {
49-
android.os.Bundle extras = intent.getExtras();
59+
Bundle extras = intent.getExtras();
5060
DebugLogging.log(TAG, "extras: " + (extras == null ? "(null)" : extras.toString()));
5161
if (extras != null) {
5262
RemoteMessage message = new RemoteMessage(extras);

0 commit comments

Comments
 (0)