This is supposed to pay notifications tutorial which uses Android Studio as IDE, with target device running Android 4.0.4 or higher.
Add the following to your application's manifest:
AndroidManifest.xml
Create a Registration Service
Registration must be done in the background thread as the process can take some time depending on the network connection. Because the record does not need input from the user, with IntentService experience ideal for this task.
RegistrationService.java
MyInstanceIDListenerService.java
Add TokenRefrshListenerService.java to AndroidManifest.xml
Google Cloud Messaging (GCM) The services offered by Google, which allows developers to implement their program Push messages. Using GCM, developers do not need to apply their method of data sent from the server to the client program.
Configuring the Project in Google Developers Console
- Create a project in the Google developer Console
- Enable Api Google Cloud Messaging
Configure your project
Add the dependency to your project-level build.gradle:classpath 'com.google.gms:google-services:3.0.0'
apply plugin: 'com.google.gms.google-services'
dependencies {
compile "com.google.android.gms:play-services-gcm:10.2.0"
}
Add the following to your application's manifest:
AndroidManifest.xml
<manifest package="com.example.gcm" ...>
<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17"/>
<uses-permission android:name="android.permission.WAKE_LOCK" />
<application ...>
<receiver
android:name="com.google.android.gms.gcm.GcmReceiver"
android:exported="true"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="com.example.gcm" />
</intent-filter>
</receiver>
<service
android:name="com.example.MyGcmListenerService"
android:exported="false" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
</intent-filter>
</service>
<service
android:name="com.example.MyInstanceIDListenerService"
android:exported="false">
<intent-filter>
<action android:name="com.google.android.gms.iid.InstanceID" />
</intent-filter>
</service>
<service
android:name="gcm.play.android.samples.com.gcmquickstart.RegistrationIntentService"
android:exported="false">
</service>
</application>
</manifest>
Create a Registration Service
Registration must be done in the background thread as the process can take some time depending on the network connection. Because the record does not need input from the user, with IntentService experience ideal for this task.
RegistrationService.java
public class RegistrationIntentService extends IntentService {
@Override
public void onHandleIntent(Intent intent) {
InstanceID instanceID = InstanceID.getInstance(this);
String token = instanceID.getToken(getString(R.string.gcm_defaultSenderId),
GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);
}
}
Registration tokens are refreshed periodically. As a result, each Android app that uses the GCM can handle a InstanceIDListenerService refresh it should be. Therefore, TokenRefreshListenerService.java create a new Java file and make it a subclass of InstanceIDListenerService. Inside the classroom onTokenRefresh method, we all need to do is simply intent and begin the process of re-registration from the registration service startService method.
MyInstanceIDListenerService.java
@Override
public void onTokenRefresh() {
// Fetch updated Instance ID token and notify our app's server of any changes (if applicable).
Intent intent = new Intent(this, RegistrationIntentService.class);
startService(intent);
}
Add TokenRefrshListenerService.java to AndroidManifest.xml
<service
android:name=".MyInstanceIDListenerService" android:exported="false">
<intent-filter>
<action android:name="com.google.android.gms.iid.InstanceID" />
</intent-filter>
</service>