Last active
October 23, 2015 13:57
-
-
Save evasyuk/ee05ea7e84029a7f25f4 to your computer and use it in GitHub Desktop.
quick cheat-paper for GCM setup
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
<?xml version="1.0" encoding="utf-8"?> | |
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | |
package="com.genyware.whereareyou" | |
android:versionCode="1" | |
android:versionName="1.0"> | |
<uses-sdk android:minSdkVersion="15"/> | |
<uses-permission android:name="android.permission.READ_PHONE_STATE" /> | |
<uses-permission android:name="android.permission.INTERNET"/> | |
<uses-permission android:name="android.permission.WAKE_LOCK"/> | |
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE"/> | |
<permission android:name=".permission.C2D_MESSAGE" | |
android:protectionLevel="signature"/> | |
<uses-permission android:name="android.permission.C2D_MESSAGE"/> | |
<application android:label="@string/app_name" android:icon="@drawable/ic_launcher"> | |
<activity android:name="AcStarter" | |
android:label="@string/app_name"> | |
<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.REGISTRATION" /> | |
<action android:name="com.google.android.c2dm.intent.RECEIVE"/> | |
<category android:name="com.genyware.whereareyou"/> | |
</intent-filter> | |
</receiver> | |
<service android:name=".MyInstanceIDListenerService" android:exported="false"> | |
<intent-filter> | |
<action android:name="com.google.android.gms.iid.InstanceID"/> | |
</intent-filter> | |
</service> | |
</application> | |
</manifest> |
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
1. google-services.json // https://developers.google.com/identity/sign-in/android/start | |
2. AndroidManifest.xml | |
3. RegistrationIntentService.java | |
4. MyInstanceIDListenerService.java | |
5. |
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
{"project_info":{"project_id":"where-are-you","project_number":"<deleted>","name":"where-are-you"},"client":[{"client_info":{"mobilesdk_app_id":"1:<deleted>:android:<deleted>","client_id":"android:com.genyware.whereareyou","client_type":1,"android_client_info":{"package_name":"com.genyware.whereareyou"}},"oauth_client":[],"api_key":[],"services":{"analytics_service":{"status":1},"cloud_messaging_service":{"status":2,"apns_config":[]},"appinvite_service":{"status":1,"other_platform_oauth_client":[]},"google_signin_service":{"status":1},"ads_service":{"status":1}}}],"client_info":[],"ARTIFACT_VERSION":"1"} |
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
package com.genyware.whereareyou; | |
import android.content.Intent; | |
import com.google.android.gms.iid.InstanceIDListenerService; | |
public class MyInstanceIDListenerService extends InstanceIDListenerService { | |
private static final String TAG = "MyInstanceIDLS"; | |
/** | |
* Called if InstanceID token is updated. This may occur if the security of | |
* the previous token had been compromised. This call is initiated by the | |
* InstanceID provider. | |
*/ | |
// [START refresh_token] | |
@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); | |
} | |
// [END refresh_token] | |
} |
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
package com.genyware.whereareyou; | |
import android.app.IntentService; | |
import android.content.Intent; | |
import android.content.SharedPreferences; | |
import android.preference.PreferenceManager; | |
import android.support.v4.content.LocalBroadcastManager; | |
import android.util.Log; | |
import com.google.android.gms.gcm.GoogleCloudMessaging; | |
import com.google.android.gms.iid.InstanceID; | |
/** | |
* Created by user on 23.10.15. | |
*/ | |
public class RegistrationIntentService extends IntentService { | |
public static final String SENT_TOKEN_TO_SERVER = "SENT_TOKEN_TO_SERVER_KEY"; | |
public static final String REGISTRATION_COMPLETE = "REGISTRATION_COMPLETE_KEY"; | |
// give your server registration url here | |
static final String SERVER_URL = "http://10.0.2.2/gcm_server_php/register.php"; | |
// Google project id | |
static final String SENDER_ID = " "; | |
static final String SERVER_API_KEY = " "; | |
private static final String TAG = "RegIntentService"; | |
private static final String[] TOPICS = {"global"}; | |
public RegistrationIntentService() { | |
super(TAG); | |
} | |
@Override | |
protected void onHandleIntent(Intent intent) { | |
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this); | |
try { | |
// [START register_for_gcm] | |
// Initially this call goes out to the network to retrieve the token, subsequent calls | |
// are local. | |
// [START get_token] | |
InstanceID instanceID = InstanceID.getInstance(this); | |
// R.string.gcm_defaultSenderId (the Sender ID) is typically derived from google-services.json. | |
// See https://developers.google.com/cloud-messaging/android/start for details on this file. | |
String token = instanceID.getToken(SENDER_ID, | |
GoogleCloudMessaging.INSTANCE_ID_SCOPE, null); | |
// [END get_token] | |
Log.i(TAG, "GCM Registration Token: " + token); | |
// TODO: Implement this method to send any registration to your app's servers. | |
sendRegistrationToServer(token); | |
// | |
// WUT? | |
// | |
// Subscribe to topic channels | |
//subscribeTopics(token); | |
// You should store a boolean that indicates whether the generated token has been | |
// sent to your server. If the boolean is false, send the token to your server, | |
// otherwise your server should have already received the token. | |
sharedPreferences.edit().putBoolean(SENT_TOKEN_TO_SERVER, true).apply(); | |
// [END register_for_gcm] | |
} catch (Exception e) { | |
Log.d(TAG, "Failed to complete token refresh", e); | |
// If an exception happens while fetching the new token or updating our registration data | |
// on a third-party server, this ensures that we'll attempt the update at a later time. | |
sharedPreferences.edit().putBoolean(SENT_TOKEN_TO_SERVER, false).apply(); | |
} | |
// Notify UI that registration has completed, so the progress indicator can be hidden. | |
Intent registrationComplete = new Intent(REGISTRATION_COMPLETE); | |
LocalBroadcastManager.getInstance(this).sendBroadcast(registrationComplete); | |
} | |
private void sendRegistrationToServer(String token) { | |
//todo: implement | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment