Created
April 25, 2016 06:12
-
-
Save Evin1-/225885e2c91d0c24515692c7a953fe1c to your computer and use it in GitHub Desktop.
Simple GCM
This file contains 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
<?xml version="1.0" encoding="utf-8"?> | |
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | |
package="com.example.simplegcm"> | |
<permission | |
android:name="com.example.simplegcm.permission.C2D_MESSAGE" | |
android:protectionLevel="signature" /> | |
<uses-permission android:name="com.example.simplegcm.permission.C2D_MESSAGE" /> | |
<uses-permission android:name="android.permission.WAKE_LOCK" /> | |
<application | |
android:allowBackup="true" | |
android:icon="@mipmap/ic_launcher" | |
android:label="@string/app_name" | |
android:supportsRtl="true" | |
android:theme="@style/AppTheme"> | |
<activity android:name=".MainActivity"> | |
<intent-filter> | |
<action android:name="android.intent.action.MAIN" /> | |
<category android:name="android.intent.category.LAUNCHER" /> | |
</intent-filter> | |
</activity> | |
<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.simplegcm" /> | |
</intent-filter> | |
</receiver> | |
<service | |
android:name=".MyGcmListenerService" | |
android:exported="false"> | |
<intent-filter> | |
<action android:name="com.google.android.c2dm.intent.RECEIVE" /> | |
</intent-filter> | |
</service> | |
<service | |
android:name=".MyInstanceIDListenerService" | |
android:exported="false"> | |
<intent-filter> | |
<action android:name="com.google.android.gms.iid.InstanceID" /> | |
</intent-filter> | |
</service> | |
<service | |
android:name=".RegistrationIntentService" | |
android:exported="false"> | |
</service> | |
</application> | |
</manifest> |
This file contains 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
// Top-level build file where you can add configuration options common to all sub-projects/modules. | |
dependencies { | |
classpath 'com.android.tools.build:gradle:2.0.0' | |
classpath 'com.google.gms:google-services:2.0.0' | |
// NOTE: Do not place your application dependencies here; they belong | |
// in the individual module build.gradle files | |
} |
This file contains 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
// App-level build file | |
dependencies { | |
compile fileTree(dir: 'libs', include: ['*.jar']) | |
testCompile 'junit:junit:4.12' | |
compile 'com.android.support:appcompat-v7:23.3.0' | |
compile "com.google.android.gms:play-services-gcm:8.4.0" | |
} | |
apply plugin: 'com.google.gms.google-services' |
This file contains 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 MainActivity extends AppCompatActivity { | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
Intent intent = new Intent(this, RegistrationIntentService.class); | |
startService(intent); | |
} | |
} |
This file contains 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 MyGcmListenerService extends GcmListenerService { | |
private static final String TAG = "MyGcmListenerTAG_"; | |
@Override | |
public void onMessageReceived(String from, Bundle data) { | |
String message = data.getString("message"); | |
Log.d(TAG, "From: " + from); | |
Log.d(TAG, "Message: " + message); | |
} | |
} |
This file contains 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 MyInstanceIDListenerService extends InstanceIDListenerService { | |
@Override | |
public void onTokenRefresh() { | |
Intent intent = new Intent(this, RegistrationIntentService.class); | |
startService(intent); | |
} | |
} |
This file contains 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 RegistrationIntentService extends IntentService { | |
private static final String TAG = "RegistrationServiceTAG_"; | |
private static final String[] TOPICS = {"global"}; | |
public RegistrationIntentService() { | |
super("RegistrationIntentService"); | |
} | |
@Override | |
protected void onHandleIntent(Intent intent) { | |
try { | |
InstanceID instanceID = InstanceID.getInstance(this); | |
String token = instanceID.getToken(getString(R.string.gcm_defaultSenderId), | |
GoogleCloudMessaging.INSTANCE_ID_SCOPE, null); | |
Log.i(TAG, "GCM Registration Token: " + token); | |
subscribeTopics(token); | |
} catch (Exception e) { | |
Log.d(TAG, "Failed to complete token refresh", e); | |
} | |
} | |
private void subscribeTopics(String token) throws IOException { | |
GcmPubSub pubSub = GcmPubSub.getInstance(this); | |
for (String topic : TOPICS) { | |
pubSub.subscribe(token, "/topics/" + topic, null); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment