Skip to content

Commit 812285a

Browse files
Jawnnypoorogerhu
authored andcommitted
Initial FCM setup
1 parent 87ecbff commit 812285a

File tree

9 files changed

+213
-1
lines changed

9 files changed

+213
-1
lines changed

fcm/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build

fcm/README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Parse SDK Android FCM
2+
FCM support for Parse Android apps
3+
4+
## Setup
5+
6+
### Installation
7+
8+
Add dependency to the application level `build.gradle` file.
9+
10+
```groovy
11+
dependencies {
12+
implementation 'com.parse:parse-android:latest.version.here'
13+
implementation 'com.parse:parse-android-fcm:latest.version.here'
14+
}
15+
```
16+
Then, follow Google's docs for [setting up an FCM app](https://firebase.google.com/docs/cloud-messaging/android/client).
17+
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.
20+
21+
## License
22+
Copyright (c) 2015-present, Parse, LLC.
23+
All rights reserved.
24+
25+
This source code is licensed under the BSD-style license found in the
26+
LICENSE file in the root directory of this source tree. An additional grant
27+
of patent rights can be found in the PATENTS file in the same directory.

fcm/build.gradle

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
apply plugin: 'com.android.library'
2+
3+
android {
4+
compileSdkVersion rootProject.ext.compileSdkVersion
5+
6+
defaultConfig {
7+
minSdkVersion rootProject.ext.minSdkVersion
8+
targetSdkVersion rootProject.ext.targetSdkVersion
9+
versionCode 1
10+
versionName project.version
11+
12+
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
13+
14+
}
15+
16+
buildTypes {
17+
release {
18+
minifyEnabled false
19+
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
20+
}
21+
}
22+
23+
}
24+
25+
dependencies {
26+
implementation project(':Parse')
27+
api 'com.google.firebase:firebase-messaging:12.0.1'
28+
api 'com.firebase:firebase-jobdispatcher:0.8.5'
29+
}

fcm/proguard-rules.pro

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Add project specific ProGuard rules here.
2+
# You can control the set of applied configuration files using the
3+
# proguardFiles setting in build.gradle.
4+
#
5+
# For more details, see
6+
# http://developer.android.com/guide/developing/tools/proguard.html
7+
8+
# If your project uses WebView with JS, uncomment the following
9+
# and specify the fully qualified class name to the JavaScript interface
10+
# class:
11+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12+
# public *;
13+
#}
14+
15+
# Uncomment this to preserve the line number information for
16+
# debugging stack traces.
17+
#-keepattributes SourceFile,LineNumberTable
18+
19+
# If you keep the line number information, uncomment this to
20+
# hide the original source file name.
21+
#-renamesourcefileattribute SourceFile

fcm/src/main/AndroidManifest.xml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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;
10+
11+
/**
12+
* shhh don't talk about this
13+
*/
14+
public class ParseFCMParseAccess {
15+
16+
public static void setToken(ParseInstallation installation, String token) {
17+
installation.setDeviceToken(token);
18+
//Yes, we are actually FCM, but its the same for our purposes
19+
installation.setPushType(PushType.GCM);
20+
}
21+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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.firebase.jobdispatcher.Constraint;
14+
import com.firebase.jobdispatcher.FirebaseJobDispatcher;
15+
import com.firebase.jobdispatcher.GooglePlayDriver;
16+
import com.firebase.jobdispatcher.Job;
17+
import com.firebase.jobdispatcher.RetryStrategy;
18+
import com.google.firebase.iid.FirebaseInstanceIdService;
19+
import com.parse.ParseInstallation;
20+
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 {
26+
27+
@CallSuper
28+
@Override
29+
public void onTokenRefresh() {
30+
super.onTokenRefresh();
31+
FirebaseJobDispatcher dispatcher = new FirebaseJobDispatcher(new GooglePlayDriver(getApplicationContext()));
32+
Job job = dispatcher.newJobBuilder()
33+
.setRecurring(false)
34+
.setReplaceCurrent(true)
35+
// retry with exponential backoff
36+
.setRetryStrategy(RetryStrategy.DEFAULT_EXPONENTIAL)
37+
.setConstraints(
38+
// only run on a network
39+
Constraint.ON_ANY_NETWORK
40+
)
41+
.setService(ParseFirebaseJobService.class) // the JobService that will be called
42+
.setTag("upload-token") // uniquely identifies the job
43+
.build();
44+
45+
dispatcher.mustSchedule(job);
46+
}
47+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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 com.firebase.jobdispatcher.JobParameters;
12+
import com.firebase.jobdispatcher.JobService;
13+
import com.google.firebase.iid.FirebaseInstanceId;
14+
import com.parse.ParseException;
15+
import com.parse.ParseInstallation;
16+
import com.parse.SaveCallback;
17+
import com.parse.ParseFCMParseAccess;
18+
19+
/**
20+
* Handles saving the FCM token to the {@link ParseInstallation}
21+
*/
22+
public class ParseFirebaseJobService extends JobService {
23+
24+
@Override
25+
public boolean onStartJob(final JobParameters job) {
26+
ParseInstallation installation = ParseInstallation.getCurrentInstallation();
27+
String token = FirebaseInstanceId.getInstance().getToken();
28+
if (installation != null && token != null) {
29+
ParseFCMParseAccess.setToken(installation, token);
30+
installation.saveInBackground(new SaveCallback() {
31+
@Override
32+
public void done(ParseException e) {
33+
if (e == null) {
34+
jobFinished(job, false);
35+
} else {
36+
jobFinished(job, true);
37+
}
38+
}
39+
});
40+
return true;
41+
}
42+
return false; // Answers the question: "Is there still work going on?"
43+
}
44+
45+
@Override
46+
public boolean onStopJob(JobParameters job) {
47+
return true; // Answers the question: "Should this job be retried?"
48+
}
49+
}

settings.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
include ':Parse'
1+
include ':Parse', ':fcm'
22

0 commit comments

Comments
 (0)