Android GCM Library
A library which helps you register Google Cloud Messaging tokens and listen for GCM messages. It can be used to attach multiple providers and seamlessly deliver data and token to all the listeners. This library is very helpful for those you use multiple push notification provider.
Salient Features
- Takes care of GCM Registration
- Token rotation, updates
- Provides callbacks for listening to registration changes
- Provides callbacks for GCM message callbacks
For example, A lot of apps use multiple push providers. This library helps them to maintain a clean code and also loosely couple the providers still providing support for all
Adding the library as a dependency
Add the following your build.gradle
file
compile 'com.nandi:gcm-lib:1.0.3'
Supports Play Services GCM 10.0.1
How to use ?
Implementing a Registration Listener
public class CustomRegistrationListener implements GcmHelper.GcmRegistrationListener{
@Override public void onTokenAvailable(Context context, String token, boolean updated) {
//TODO do something with the token
}
@Override public void onTokenDeleted(Context context) {
//TODO delete the token
}
}
Implementing the Message Listener
public class CustomMessageReceivedListener implements GcmHelper.GcmMessageListener {
@Override public void onMessageReceived(Context context, String from, Bundle data) {
//TODO start your service or do something else
}
@Override public boolean canHandleMessage(Bundle data){
//TODO check the bundle to see if the listener can handle payload
return false;
}
}
Now in your application class onCreate
method following:
GcmHelper.getInstance()
.setAuthorizedEntity("889908101771")
.addRegistrationCallback(getApplicationContext(), new CustomRegistrationListener(), true)
.addOnMessageReceivedCallback(new CustomMessageReceivedListener())
.init(getApplication());
setAuthorizedEntity
is the sender id or the Google Project number
Minimum Requirements Google Play Services GCM library 7.5
As seen here
GCM register() is deprecated starting May 28, 2015. New app development should use the Instance ID API to handle the creation, rotation, and updating of registration tokens.
License
GCMLib is Apache Licence.
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.