So, I'm trying to develop a very basic c2dm application. This requires registering an intent, receiving an auth token from google, sending that auth token to your app server, and then sending API calls to google using said auth token.
Unfortunately, my app doesn't seem to be getting an auth token back from google, and I cant figure out why. Anyone have any ideas? Code samples below:
app.java
just in case, manifest.xmlCode:package android.push.alet; import android.app.Activity; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.widget.TextView; public class alert extends Activity { /** Called when the activity is first created. */ private TextView mytext; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mytext = (TextView) findViewById(R.id.mytext); mytext.setText("app started"); Intent registrationIntent = new Intent("com.google.android.c2dm.intent.REGISTER"); registrationIntent.putExtra("app", PendingIntent.getBroadcast(this, 0, new Intent(), 0)); // boilerplate registrationIntent.putExtra("sender", "mr.glass@gmail.com"); startService(registrationIntent); mytext.setText("Grabbing your device registration ID....."); } public void onReceive(Context context, Intent intent) { mytext.setText("Intent Received!"); if (intent.getAction().equals("com.google.android.c2dm.intent.REGISTRATION")) { handleRegistration(context, intent); } else if (intent.getAction().equals("com.google.android.c2dm.intent.RECEIVE")) { handleMessage(context, intent); } } private void handleRegistration(Context context, Intent intent) { String registration = intent.getStringExtra("registration_id"); if (intent.getStringExtra("error") != null) { mytext.setText("There was an error with your device registration!"); // Registration failed, should try again later. } else if (intent.getStringExtra("unregistered") != null) { // unregistration done, new messages from the authorized sender will be rejected mytext.setText("You have been unregistered!"); } else if (registration != null) { // Send the registration ID to the 3rd party site that is sending the messages. // This should be done in a separate thread. // When done, remember that all registration is done. mytext.setText("Your registration code is: " + registration); } } private void handleMessage(Context context, Intent intent) { mytext.setText("You have been alerted!"); } }
Code:<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="android.push.alet" android:versionCode="1" android:versionName="0.1"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".alert" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> <uses-sdk android:minSdkVersion="8" /> <!-- Only this application can receive the messages and registration result --> <permission android:name="com.example.myapp.permission.C2D_MESSAGE" android:protectionLevel="signature" /> <uses-permission android:name="com.example.myapp.permission.C2D_MESSAGE" /> <!-- This app has permission to register and receive message --> <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" /> <!-- Send the registration id to the server --> <uses-permission android:name="android.permission.INTERNET" /> <!-- Only C2DM servers can send messages for the app. If permission is not set - any other app can generate it --> <receiver android:name=".C2DMReceiver" android:permission="com.google.android.c2dm.permission.SEND"> <!-- Receive the actual message --> <intent-filter> <action android:name="com.google.android.c2dm.intent.RECEIVE" /> <category android:name="com.example.myapp" /> </intent-filter> <!-- Receive the registration id --> <intent-filter> <action android:name="com.google.android.c2dm.intent.REGISTRATION" /> <category android:name="com.example.myapp" /> </intent-filter> </receiver> </manifest>


LinkBack URL
About LinkBacks
Reply With Quote
