Basic c2dm application

Discussion in 'Android Hacks and Help' started by MrGlass, Aug 29, 2010.

  1. MrGlass

    MrGlass New Member

    Joined:
    Dec 6, 2009
    Messages:
    12
    Likes Received:
    0
    Trophy Points:
    1
    Ratings:
    +0
    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
    Code:
    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!");
        }
    }
    
    just in case, manifest.xml
    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> 
    

     
    Last edited: Aug 30, 2010
  2. devnaught

    devnaught New Member

    Joined:
    Aug 30, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    1
    Ratings:
    +0
    package name

    You are mixing your package names in the manifest. You created the project as "android.push.alet"

    Then you try to wire up permissions and info with "com.example.myapp." You should replace com.example.myapp with your actual package name.

    Also, the package name for a quick try out doesn't matter too much but for a real app you want to pick something that is a solid namespace. If you own a domain like "mrglass.com" you could make this app: com.mrglass.pushalert

    It uniquely identifies your app to Google in the market as well as on the handheld. All of your internal (and in Froyo SD card) paths will reflect this. Just a good idea all around.

    This may not get you 100% running but like all free advice you get what you pay for ;)
     
  3. rosebeat

    rosebeat New Member

    Joined:
    Sep 28, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    1
    Ratings:
    +0
    Push API using C2DM

    Hey, I have tried your code. I get application close error on the emulator. Can you please tell me how you made this code work???
     
Search tags for this page
c2dm c sample
,

com.google.android.c2dm.intent.register