Skip to content

Commit bb0476d

Browse files
Jawnnypoorogerhu
authored andcommitted
Add notes about calling register
1 parent e0a07f9 commit bb0476d

File tree

3 files changed

+55
-18
lines changed

3 files changed

+55
-18
lines changed

gcm/README.md

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,15 @@ dependencies {
1515
implementation "com.google.android.gms:play-services-gcm:latest.version.here"
1616
}
1717
```
18-
You will then need to register some things in your manifest, specifically:
18+
You will then need to register some things in your manifest, firstly, the GCM sender ID:
19+
```xml
20+
<meta-data
21+
android:name="com.parse.push.gcm_sender_id"
22+
android:value="id:YOUR_SENDER_ID_HERE" />
23+
```
24+
The sender ID should be all numbers. Make sure you are keeping the `id:` in the front
25+
26+
Next:
1927
```xml
2028
<receiver
2129
android:name="com.google.android.gms.gcm.GcmReceiver"
@@ -60,7 +68,20 @@ After these services are registered in the Manifest, you then need to register y
6068
</intent-filter>
6169
</receiver>
6270
```
63-
And from there, you should be good to go.
71+
After all this, you will need to register GCM in your `Application.onCreate()` like so:
72+
```java
73+
@Override
74+
public void onCreate() {
75+
super.onCreate();
76+
Parse.Configuration configuration = new Parse.Configuration.Builder(this)
77+
//...
78+
.build();
79+
Parse.initialize(configuration);
80+
ParseGCM.register(this);
81+
}
82+
```
83+
84+
After this, you are all set.
6485

6586
## Custom Notifications
6687
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.

gcm/src/main/java/com/parse/gcm/ParseGCM.java

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@
1212
import android.os.Bundle;
1313
import android.support.annotation.Nullable;
1414

15+
import com.firebase.jobdispatcher.Constraint;
16+
import com.firebase.jobdispatcher.FirebaseJobDispatcher;
17+
import com.firebase.jobdispatcher.GooglePlayDriver;
18+
import com.firebase.jobdispatcher.Job;
19+
import com.firebase.jobdispatcher.RetryStrategy;
1520
import com.parse.ManifestInfo;
1621
import com.parse.PLog;
1722

@@ -24,6 +29,31 @@ public class ParseGCM {
2429

2530
static final String TAG = "ParseGCM";
2631

32+
/**
33+
* Register your app to start receiving GCM pushes
34+
*
35+
* @param context context
36+
*/
37+
public static void register(Context context) {
38+
//kicks off the background job
39+
PLog.v(TAG, "Scheduling job to register Parse GCM");
40+
FirebaseJobDispatcher dispatcher = new FirebaseJobDispatcher(new GooglePlayDriver(context.getApplicationContext()));
41+
Job job = dispatcher.newJobBuilder()
42+
.setRecurring(false)
43+
.setReplaceCurrent(true)
44+
// retry with exponential backoff
45+
.setRetryStrategy(RetryStrategy.DEFAULT_EXPONENTIAL)
46+
.setConstraints(
47+
// only run on a network
48+
Constraint.ON_ANY_NETWORK
49+
)
50+
.setService(ParseGCMJobService.class) // the JobService that will be called
51+
.setTag("initialize") // uniquely identifies the job
52+
.build();
53+
54+
dispatcher.mustSchedule(job);
55+
}
56+
2757
@Nullable
2858
static String gcmSenderFromManifest(Context context) {
2959
// Look for an element like this as a child of the <application> element:
@@ -70,7 +100,7 @@ private static String actualSenderIDFromExtra(Object senderIDExtra) {
70100
return null;
71101
}
72102

73-
String senderID = (String)senderIDExtra;
103+
String senderID = (String) senderIDExtra;
74104
if (!senderID.startsWith("id:")) {
75105
return null;
76106
}

gcm/src/main/java/com/parse/gcm/ParseGCMInstanceIDListenerService.java

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,6 @@ public class ParseGCMInstanceIDListenerService extends InstanceIDListenerService
1616
public void onTokenRefresh() {
1717
super.onTokenRefresh();
1818

19-
FirebaseJobDispatcher dispatcher = new FirebaseJobDispatcher(new GooglePlayDriver(getApplicationContext()));
20-
Job job = dispatcher.newJobBuilder()
21-
.setRecurring(false)
22-
.setReplaceCurrent(true)
23-
// retry with exponential backoff
24-
.setRetryStrategy(RetryStrategy.DEFAULT_EXPONENTIAL)
25-
.setConstraints(
26-
// only run on a network
27-
Constraint.ON_ANY_NETWORK
28-
)
29-
.setService(ParseGCMJobService.class) // the JobService that will be called
30-
.setTag("initialize") // uniquely identifies the job
31-
.build();
32-
33-
dispatcher.mustSchedule(job);
19+
ParseGCM.register(getApplicationContext());
3420
}
3521
}

0 commit comments

Comments
 (0)