Skip to content

Commit c8c73fe

Browse files
Jawnnypoorogerhu
authored andcommitted
Update FCM build to pass FCM message to the push receiver
1 parent afb4182 commit c8c73fe

File tree

7 files changed

+123
-24
lines changed

7 files changed

+123
-24
lines changed

fcm/README.md

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,54 @@ Add dependency to the application level `build.gradle` file.
1111
dependencies {
1212
implementation 'com.parse:parse-android:latest.version.here'
1313
implementation 'com.parse:parse-android-fcm:latest.version.here'
14+
15+
implementation 'com.google.firebase:firebase-messaging:12.0.1'
1416
}
1517
```
16-
Then, follow Google's docs for [setting up an FCM app](https://firebase.google.com/docs/cloud-messaging/android/client).
18+
Then, follow Google's docs for [setting up an Firebase app](https://firebase.google.com/docs/cloud-messaging/android/client). 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).
19+
20+
You will then need to register some things in your manifest, specifically:
21+
```xml
22+
<service
23+
android:name="com.parse.fcm.ParseFirebaseInstanceIdService"
24+
android:exported="true">
25+
<intent-filter>
26+
<action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
27+
</intent-filter>
28+
</service>
29+
```
30+
where `MyFirebaseInstanceIdService` is your own custom class which extends `ParseFirebaseInstanceIdService`.
31+
32+
Additional, you will register:
33+
34+
```xml
35+
<service
36+
android:name=".MyFirebaseMessagingService">
37+
<intent-filter>
38+
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
39+
</intent-filter>
40+
</service>
41+
```
42+
where `MyFirebaseMessagingService` extends `ParseFirebaseMessagingService`
43+
44+
After these services are registered in the Manifest, you then need to register your push broadcast receiver:
45+
```xml
46+
<receiver
47+
android:name="com.parse.ParsePushBroadcastReceiver"
48+
android:exported="false">
49+
<intent-filter>
50+
<action android:name="com.parse.push.intent.RECEIVE" />
51+
<action android:name="com.parse.push.intent.DELETE" />
52+
<action android:name="com.parse.push.intent.OPEN" />
53+
</intent-filter>
54+
</receiver>
55+
```
56+
57+
## Custom Notifications
58+
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.
1759

18-
## Note
19-
If you need access to the FCM token, instead of creating a service that extends `FirebaseInstanceIdService`, extend `ParseFirebaseInstanceIDService` to assure the token still gets saved properly within Parse.
60+
## Instance ID Service
61+
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.scheduleTokenUpload(getApplicationContext());` in the `onTokenRefresh` method.
2062

2163
## License
2264
Copyright (c) 2015-present, Parse, LLC.

fcm/build.gradle

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ android {
2323
}
2424

2525
dependencies {
26-
compileOnly project(':Parse')
26+
//TODO figure out how to make this local
27+
api 'com.parse:parse-android:1.16.7'
2728
api 'com.google.firebase:firebase-messaging:12.0.1'
2829
api 'com.firebase:firebase-jobdispatcher:0.8.5'
2930
}

fcm/src/main/java/com/parse/ParseFCMParseAccess.java renamed to fcm/src/main/java/com/parse/InternalParseFCMParseAccess.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
/**
1212
* shhh don't talk about this
1313
*/
14-
public class ParseFCMParseAccess {
14+
public class InternalParseFCMParseAccess {
1515

1616
private static final String TAG = "ParseFCM";
1717

fcm/src/main/java/com/parse/fcm/ParseFirebaseInstanceIDService.java renamed to fcm/src/main/java/com/parse/fcm/ParseFCM.java

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

11-
import android.support.annotation.CallSuper;
11+
import android.content.Context;
1212

1313
import com.firebase.jobdispatcher.Constraint;
1414
import com.firebase.jobdispatcher.FirebaseJobDispatcher;
1515
import com.firebase.jobdispatcher.GooglePlayDriver;
1616
import com.firebase.jobdispatcher.Job;
1717
import com.firebase.jobdispatcher.RetryStrategy;
18-
import com.google.firebase.iid.FirebaseInstanceIdService;
19-
import com.parse.ParseInstallation;
2018

21-
/**
22-
* Assures the {@link ParseInstallation#getDeviceToken()} stays up to date. If you need to do custom things with the token, make sure you extend this
23-
* class and call super.
24-
*/
25-
public class ParseFirebaseInstanceIDService extends FirebaseInstanceIdService {
19+
public class ParseFCM {
2620

27-
@CallSuper
28-
@Override
29-
public void onTokenRefresh() {
30-
super.onTokenRefresh();
31-
FirebaseJobDispatcher dispatcher = new FirebaseJobDispatcher(new GooglePlayDriver(getApplicationContext()));
21+
public static void scheduleTokenUpload(Context context) {
22+
FirebaseJobDispatcher dispatcher = new FirebaseJobDispatcher(new GooglePlayDriver(context.getApplicationContext()));
3223
Job job = dispatcher.newJobBuilder()
3324
.setRecurring(false)
3425
.setReplaceCurrent(true)
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright (c) 2015-present, Parse, LLC.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*/
9+
package com.parse.fcm;
10+
11+
import android.support.annotation.CallSuper;
12+
13+
import com.google.firebase.iid.FirebaseInstanceIdService;
14+
import com.parse.ParseInstallation;
15+
16+
/**
17+
* Assures the {@link ParseInstallation#getDeviceToken()} stays up to date. If you need to do custom things with the token, make sure you extend this
18+
* class and call super.
19+
*/
20+
public class ParseFirebaseInstanceIdService extends FirebaseInstanceIdService {
21+
22+
@CallSuper
23+
@Override
24+
public void onTokenRefresh() {
25+
super.onTokenRefresh();
26+
ParseFCM.scheduleTokenUpload(getApplicationContext());
27+
}
28+
}

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,31 +11,31 @@
1111
import com.firebase.jobdispatcher.JobParameters;
1212
import com.firebase.jobdispatcher.JobService;
1313
import com.google.firebase.iid.FirebaseInstanceId;
14+
import com.parse.InternalParseFCMParseAccess;
1415
import com.parse.ParseException;
1516
import com.parse.ParseInstallation;
1617
import com.parse.SaveCallback;
17-
import com.parse.ParseFCMParseAccess;
1818

1919
/**
20-
* Handles saving the FCM token to the {@link ParseInstallation}
20+
* Handles saving the FCM token to the {@link ParseInstallation} in the background
2121
*/
2222
public class ParseFirebaseJobService extends JobService {
2323

2424
@Override
2525
public boolean onStartJob(final JobParameters job) {
26-
ParseFCMParseAccess.logVerbose("Updating FCM token");
26+
InternalParseFCMParseAccess.logVerbose("Updating FCM token");
2727
ParseInstallation installation = ParseInstallation.getCurrentInstallation();
2828
String token = FirebaseInstanceId.getInstance().getToken();
2929
if (installation != null && token != null) {
30-
ParseFCMParseAccess.setToken(installation, token);
30+
InternalParseFCMParseAccess.setToken(installation, token);
3131
installation.saveInBackground(new SaveCallback() {
3232
@Override
3333
public void done(ParseException e) {
3434
if (e == null) {
35-
ParseFCMParseAccess.logVerbose("FCM token saved to installation");
35+
InternalParseFCMParseAccess.logVerbose("FCM token saved to installation");
3636
jobFinished(job, false);
3737
} else {
38-
ParseFCMParseAccess.logError("FCM token upload failed", e);
38+
InternalParseFCMParseAccess.logError("FCM token upload failed", e);
3939
jobFinished(job, true);
4040
}
4141
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.parse.fcm;
2+
3+
import android.content.Intent;
4+
import android.os.Bundle;
5+
6+
import com.google.firebase.messaging.FirebaseMessagingService;
7+
import com.google.firebase.messaging.RemoteMessage;
8+
import com.parse.InternalParseFCMParseAccess;
9+
import com.parse.ParsePushBroadcastReceiver;
10+
11+
public class ParseFirebaseMessagingService extends FirebaseMessagingService {
12+
13+
@Override
14+
public void onMessageReceived(RemoteMessage remoteMessage) {
15+
super.onMessageReceived(remoteMessage);
16+
InternalParseFCMParseAccess.logVerbose("onMessageReceived");
17+
//example of json data:
18+
//{"alert":"this is to everyone","sound":"default","title":"Everyone","content-available":1}
19+
String channel = remoteMessage.getData().get("channel");
20+
String data = remoteMessage.getData().get("data");
21+
Bundle extras = new Bundle();
22+
extras.putString(ParsePushBroadcastReceiver.KEY_PUSH_CHANNEL, channel);
23+
if (data == null) {
24+
extras.putString(ParsePushBroadcastReceiver.KEY_PUSH_DATA, "{}");
25+
} else {
26+
extras.putString(ParsePushBroadcastReceiver.KEY_PUSH_DATA, data);
27+
}
28+
29+
Intent intent = new Intent(ParsePushBroadcastReceiver.ACTION_PUSH_RECEIVE);
30+
intent.putExtras(extras);
31+
32+
// Set the package name to keep this intent within the given package.
33+
intent.setPackage(getApplicationContext().getPackageName());
34+
getApplicationContext().sendBroadcast(intent);
35+
36+
}
37+
}

0 commit comments

Comments
 (0)