Forked from grahamsmith/GcmRegistrationIntentService.class
Created
August 8, 2016 03:53
-
-
Save pyadav/ad23ef4c1d348aebec6fef6c300ce4c1 to your computer and use it in GitHub Desktop.
GCM RxJava IntentService
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class GcmRegistrationIntentService extends IntentService { | |
| private static final String TAG = "RegIntentService"; | |
| private static final String[] TOPICS = {"global"}; | |
| private PushNotificationManager pushNotificationManager; | |
| private GcmRequestManager gcmRequestManager; | |
| private InstanceID instanceID; | |
| public GcmRegistrationIntentService() { | |
| super(TAG); | |
| } | |
| public GcmRegistrationIntentService(String name) { | |
| super(TAG); | |
| } | |
| @Override | |
| protected void onHandleIntent(Intent intent) { | |
| pushNotificationManager = PushNotificationManager.getInstance(this.getApplicationContext()); | |
| gcmRequestManager = GcmRequestManager.getInstance(this.getApplicationContext()); | |
| instanceID = InstanceID.getInstance(this.getApplicationContext()); | |
| //update flag to say we need to register in case of failure | |
| pushNotificationManager.setHasCompletedGcmRegistration(false); | |
| setupPushNotifications(); | |
| } | |
| private void setupPushNotifications() { | |
| getToken().subscribeOn(Schedulers.io()) | |
| .flatMap(this::sendRegistrationToServer) | |
| .subscribe(new GcmRegistrationSubscriber()); | |
| } | |
| private Single<String> getToken() { | |
| return Single.defer(() -> { | |
| try { | |
| String token = instanceID.getToken(getString(R.string.gcm_sender_id), GoogleCloudMessaging.INSTANCE_ID_SCOPE, null); | |
| pushNotificationManager.setGcmPushToken(token); | |
| return Single.just(token); | |
| } catch (Exception exception) { | |
| return Single.error(exception); | |
| } | |
| }); | |
| } | |
| private Single<Object> sendRegistrationToServer(String token) { | |
| pushNotificationManager.setGcmPushToken(token); | |
| return gcmRequestManager.register() | |
| .toSingle() | |
| .flatMap(this::registerTopics); | |
| } | |
| private Single<Void> registerTopics(Object object) { | |
| return Single.defer(() -> { | |
| try { | |
| GcmPubSub pubSub = GcmPubSub.getInstance(getApplicationContext()); | |
| for (String topic : TOPICS) { | |
| pubSub.subscribe(pushNotificationManager.getGcmPushToken(), "/topics/" + topic, null); | |
| } | |
| return Single.just(null); | |
| } catch (Exception exception) { | |
| return Single.error(exception); | |
| } | |
| }); | |
| } | |
| private class GcmRegistrationSubscriber extends SingleSubscriber<Object> { | |
| @Override | |
| public void onSuccess(Object value) { | |
| pushNotificationManager.setHasCompletedGcmRegistration(true); | |
| } | |
| @Override | |
| public void onError(Throwable e) { | |
| pushNotificationManager.setHasCompletedGcmRegistration(false); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment