Created
August 14, 2013 02:42
-
-
Save Mrono/6227584 to your computer and use it in GitHub Desktop.
Code needed to do basic C2DM receiving on android
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" | |
<permission | |
android:name="net.mrono.android.cloudnotify.permission.C2D_MESSAGE" | |
android:protectionLevel="signature" /> | |
<uses-permission | |
android:name="net.mrono.android.cloudnotify.permission.C2D_MESSAGE" /> | |
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" /> | |
<uses-permission android:name="android.permission.INTERNET" /> | |
<application | |
android:icon="@drawable/icon" | |
android:label="@string/app_name" > | |
<activity | |
android:label="@string/app_name" | |
android:name=".CloudNotifyActivity" > | |
<intent-filter > | |
<action android:name="android.intent.action.MAIN" /> | |
<category android:name="android.intent.category.LAUNCHER" /> | |
</intent-filter> | |
</activity> | |
<receiver | |
android:name=".C2DMRegistrationReceiver" | |
android:permission="com.google.android.c2dm.permission.SEND" > | |
<intent-filter > | |
<action android:name="com.google.android.c2dm.intent.REGISTRATION" > | |
</action> | |
<category android:name="net.mrono.android.cloudnotify" /> | |
</intent-filter> | |
</receiver> | |
<receiver | |
android:name=".C2DMMessageReceiver" | |
android:permission="com.google.android.c2dm.permission.SEND" > | |
<intent-filter > | |
<action android:name="com.google.android.c2dm.intent.RECEIVE" > | |
</action> | |
<category android:name="net.mrono.android.cloudnotify" /> | |
</intent-filter> | |
</receiver> | |
</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
public class C2DMMessageReceiver extends BroadcastReceiver { | |
@Override | |
public void onReceive(Context context, Intent intent) { | |
String action = intent.getAction(); | |
String jsonReturn = intent.getStringExtra("payload"); | |
if ("com.google.android.c2dm.intent.RECEIVE".equals(action)) { | |
try { | |
JSONObject data = new JSONObject(jsonReturn); | |
Log.w("JSON Decode", "payload = " + data.toString()); | |
//Do what you will with it | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
} |
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 C2DMRegistrationReceiver extends BroadcastReceiver { | |
@Override | |
public void onReceive(Context context, Intent intent) { | |
String action = intent.getAction(); | |
if ("com.google.android.c2dm.intent.REGISTRATION".equals(action)) { | |
final String registrationId = intent | |
.getStringExtra("registration_id"); | |
if (registrationId == null) { | |
String error = intent.getStringExtra("error"); | |
Toast.makeText(context, "Google Error: " + error, Toast.LENGTH_LONG).show(); | |
return; | |
} | |
// Also save it in the preference to be able to show it later | |
saveRegistrationId(context, registrationId); | |
} | |
} | |
private void saveRegistrationId(Context context, String registrationId) { | |
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context); | |
Editor edit = preferences.edit(); | |
edit.putString("registration_id", registrationId); | |
edit.commit(); | |
} | |
} |
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
Intent intent = new Intent("com.google.android.c2dm.intent.REGISTER"); | |
intent.putExtra("app", | |
PendingIntent.getBroadcast(this, 0, new Intent(), 0)); | |
// Sender currently not used | |
intent.putExtra("sender", getString(R.string.c2dmEmail)); | |
startService(intent); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment