Giter Club home page Giter Club logo

gcm-push-client's Introduction

GCM Push Client

GCM Push Client is an Android library that allows receiving Push Messages, without dealing with the hassle of making your Receiver, GCM Registration Service and all of that fun stuff.

Overview

Using GCM Push Client is very simple. You can import GCM Push Client on your build.gradle file (using jCenter or Maven Central):

    // Remember to add your GCM Play Services dependency!
    compile 'com.google.android.gms:play-services-gcm:8.4.0'
    compile 'com.devsu:pushclient:1.1.0'

Then, initialize the library on your Application, and that's it!

public class MyApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        ...
        PushClient.initialize(this, "MY_GCM_ID");
    }
}

InitCallback

Customize your client's behavior when retrieving your GCM registration ID.

public class MyApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        ...
        PushClient.initialize(this, "MY_GCM_ID", new InitCallback() {
            @Override
            public void onSuccess(String registrationId, boolean hasBeenUpdated) {
                Log.i(TAG, "This is my registrationId: " + registrationId);
            }

            @Override
            public void onError(Throwable throwable) {
                Log.e(TAG, "An error occurred :(");
            }
        });
    }
}

Customizing

Using the delegation pattern, GCM Push Client is easy to customize. Simply create your own class that implements PushDelegate, or change the default settings on SimpleNotificationDelegate.

Custom Delegate

Create your own Delegate Class:

public class ToastDelegate implements PushDelegate {

    private String mMessageKey;
    private Context mContext;

    public ToastDelegate(Context context) {
        this.mContext = context;
    }
    ...

    @Override
    public void handleNotification(Context context, Bundle extras) {
        String message = extras.getString(mMessageKey, null);
        if (message == null) {
            return;
        }
        Toast.makeText(mContext, message, Toast.LENGTH_LONG).show();
    }
}

Initialize it!

public class MyApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        ...
        PushClient.initialize(this, "MY_GCM_ID", new ToastDelegate(this));
    }
}

Customizing SimpleNotificationDelegate

You can change every aspect of your Push Notification!

public class MyApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        ...
        PushClient.initialize(this, "MY_GCM_ID");
        SimpleNotificationDelegate delegate = (SimpleNotificationDelegate) PushClient.getDelegate();

        // Change the large icon
        delegate.setLargeIconDrawableResId(R.drawable.ic_icon_large);

        // Change the small icon
        delegate.setSmallIconDrawableResId(R.drawable.ic_icon_small);

        // Change the Activity that opens on Notification click
        delegate.setDefaultActivity(MainActivity.class);

        // Change title and message keys
        delegate.setTitleKey("TITLE_KEY");
        delegate.setMessageKey("MESSAGE_KEY");

        // Change the vibration pattern
        delegate.setVibrationPattern(new long[] {0, 500, 200, 1000});

        // AND MUCH MORE!
    }
}

Authors

Feel free to contact Alvaro López at [email protected]!

License

Copyright 2015 Devsu Software

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

gcm-push-client's People

Contributors

rion18 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.