Skip to content

Commit 966a740

Browse files
authored
Change to use non-deprecated methods for registering FCM push token (#927)
1 parent 29c45f4 commit 966a740

File tree

9 files changed

+39
-155
lines changed

9 files changed

+39
-155
lines changed

build.gradle

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@ task clean(type: Delete) {
3030
ext {
3131
compileSdkVersion = 28
3232

33-
firebaseJobdispatcherVersion = '0.8.5'
34-
3533
minSdkVersion = 14
3634
targetSdkVersion = 28
3735
}

fcm/README.md

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,7 @@ dependencies {
1414
```
1515
Then, follow Google's docs for [setting up an Firebase app](https://firebase.google.com/docs/android/setup). Although the steps are different for setting up FCM with Parse, it is also a good idea to read over the [Firebase FCM Setup](https://firebase.google.com/docs/cloud-messaging/android/client).
1616

17-
You will then need to register some things in your manifest, specifically:
18-
```xml
19-
<service
20-
android:name="com.parse.fcm.ParseFirebaseInstanceIdService"
21-
android:exported="true">
22-
<intent-filter>
23-
<action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
24-
</intent-filter>
25-
</service>
26-
```
27-
28-
Additional, you will register:
29-
17+
You will then need to register the messaging service in your manifest, specifically:
3018
```xml
3119
<service
3220
android:name="com.parse.fcm.ParseFirebaseMessagingService">
@@ -49,17 +37,17 @@ After these services are registered in the Manifest, you then need to register y
4937
</receiver>
5038
```
5139

52-
After this, you are all set. Adding the `parse-fcm-android` package will include a [ParseFirebaseJobService](https://github.com/parse-community/Parse-SDK-Android/blob/master/fcm/src/main/java/com/parse/fcm/ParseFirebaseJobService.java) in the `AndroidManifest.xml` file that will register for a FCM token when the app starts. You should see `ParseFCM: FCM registration success` messages assuming you have enabled logging:
53-
40+
After this, you are all set. You should see `ParseFCM: FCM registration success` messages, assuming you have enabled logging via:
5441
```java
42+
// be sure to disable this in the release apk
5543
Parse.setLogLevel(Parse.LOG_LEVEL_DEBUG);
5644
```
5745

5846
## Custom Notifications
5947
If you need to customize the notification that is sent out from a push, you can do so easily by extending `ParsePushBroadcastReceiver` with your own class and registering it instead in the Manifest.
6048

61-
## Instance ID Service
62-
If you need to store the FCM token elsewhere outside of Parse, you can create your own implementation of the `FirebaseInstanceIdService`, just make sure you are either extending `ParseFirebaseInstanceIdService` or are calling `ParseFCM.register(getApplicationContext());` in the `onTokenRefresh` method.
49+
## Messaging Service
50+
If you need to store the FCM token elsewhere outside of Parse, you can create your own implementation of the `FirebaseMessagingService`, just make sure you are either extending `ParseFirebaseMessagingService` or are calling `ParseFCM.register(token);` in the `onNewToken` method.
6351

6452
## License
6553
Copyright (c) 2015-present, Parse, LLC.

fcm/build.gradle

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ android {
3232

3333
dependencies {
3434
api "com.google.firebase:firebase-messaging:17.3.4"
35-
api "com.firebase:firebase-jobdispatcher:$firebaseJobdispatcherVersion"
3635
implementation project(':parse')
3736
}
3837

fcm/src/main/AndroidManifest.xml

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1 @@
1-
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
2-
package="com.parse.fcm">
3-
4-
<application>
5-
6-
<service
7-
android:name=".ParseFirebaseJobService"
8-
android:exported="false">
9-
<intent-filter>
10-
<action android:name="com.firebase.jobdispatcher.ACTION_EXECUTE" />
11-
</intent-filter>
12-
</service>
13-
14-
</application>
15-
16-
17-
</manifest>
1+
<manifest package="com.parse.fcm"/>

fcm/src/main/java/com/parse/fcm/ParseFCM.java

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,40 @@
88
*/
99
package com.parse.fcm;
1010

11-
import android.content.Context;
12-
13-
import com.firebase.jobdispatcher.FirebaseJobDispatcher;
14-
import com.firebase.jobdispatcher.GooglePlayDriver;
15-
import com.firebase.jobdispatcher.Job;
1611
import com.parse.PLog;
12+
import com.parse.ParseException;
13+
import com.parse.ParseInstallation;
14+
import com.parse.SaveCallback;
1715

1816
public class ParseFCM {
1917

2018
static final String TAG = "ParseFCM";
2119

20+
private static final String PUSH_TYPE = "gcm"; // Backwards compatability with Parse servers
21+
2222
/**
23-
* You can call this manually if you are overriding the {@link com.google.firebase.iid.FirebaseInstanceIdService}
23+
* You can call this manually if you are overriding the {@link com.google.firebase.messaging.FirebaseMessagingService}
24+
* fetching the token via {@link com.google.firebase.messaging.FirebaseMessagingService#onNewToken(String)}
2425
*
25-
* @param context context
26+
* @param token the token
2627
*/
27-
public static void register(Context context) {
28-
//kicks off the background job
29-
PLog.d(TAG, "Scheduling job to register Parse FCM");
30-
FirebaseJobDispatcher dispatcher = new FirebaseJobDispatcher(new GooglePlayDriver(context.getApplicationContext()));
31-
Job job = ParseFirebaseJobService.createJob(dispatcher);
32-
dispatcher.mustSchedule(job);
28+
public static void register(String token) {
29+
PLog.d(ParseFCM.TAG, "Updating FCM token");
30+
ParseInstallation installation = ParseInstallation.getCurrentInstallation();
31+
if (installation != null && token != null) {
32+
installation.setDeviceToken(token);
33+
//even though this is FCM, calling it gcm will work on the backend
34+
installation.setPushType(PUSH_TYPE);
35+
installation.saveInBackground(new SaveCallback() {
36+
@Override
37+
public void done(ParseException e) {
38+
if (e == null) {
39+
PLog.d(ParseFCM.TAG, "FCM token saved to installation");
40+
} else {
41+
PLog.e(ParseFCM.TAG, "FCM token upload failed", e);
42+
}
43+
}
44+
});
45+
}
3346
}
3447
}

fcm/src/main/java/com/parse/fcm/ParseFirebaseInstanceIdService.java

Lines changed: 0 additions & 28 deletions
This file was deleted.

fcm/src/main/java/com/parse/fcm/ParseFirebaseJobService.java

Lines changed: 0 additions & 76 deletions
This file was deleted.

fcm/src/main/java/com/parse/fcm/ParseFirebaseMessagingService.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@
1010

1111
public class ParseFirebaseMessagingService extends FirebaseMessagingService {
1212

13+
@Override
14+
public void onNewToken(String token) {
15+
super.onNewToken(token);
16+
ParseFCM.register(token);
17+
}
18+
1319
@Override
1420
public void onMessageReceived(RemoteMessage remoteMessage) {
1521
super.onMessageReceived(remoteMessage);

gcm/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ android {
3333
dependencies {
3434
// last version for GCM to be supported
3535
api "com.google.android.gms:play-services-gcm:12.0.1"
36-
api "com.firebase:firebase-jobdispatcher:$firebaseJobdispatcherVersion"
36+
api "com.firebase:firebase-jobdispatcher:0.8.5"
3737
implementation project(':parse')
3838
}
3939

0 commit comments

Comments
 (0)