Basic c2dm application

This is a discussion on Basic c2dm application within the Droid Development forums, part of the Droid Hacking category; 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 ...

+ Reply to Thread
Results 1 to 3 of 3

Thread: Basic c2dm application

  1. Junior Droid
    MrGlass's Avatar
    Member #
    12425
    Join Date
    Dec 2009
    Posts
    12
    Phone
    Enter Current Phone Here
    #1

    Basic c2dm application

    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 by MrGlass; 08-30-2010 at 06:39 PM.
  2. Sponsor
    DF Advertising
    Join Date
    Nov 2008
    Location
    DroidForums.net
     
     
     
     
  3. Droid Newbie
    devnaught's Avatar
    Member #
    111441
    Join Date
    Aug 2010
    Posts
    1
    Phone
    Enter Current Phone Model Here
    #2

    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
  4. Droid Newbie
    rosebeat's Avatar
    Member #
    122225
    Join Date
    Sep 2010
    Posts
    1
    Phone
    Enter Current Phone Model Here
    #3

    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???

Sponsors

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Similar Threads

  1. Customize application button and application screen!
    By m698322h in forum Motorola Droid
    Replies: 3
    Last Post: 10-13-2010, 12:52 PM
  2. Basic Calendar Application
    By tarheel23 in forum Android App Developers
    Replies: 7
    Last Post: 02-19-2010, 12:39 AM
  3. when basic apps get better
    By lcdret in forum Pending Submissions / Questions
    Replies: 3
    Last Post: 02-06-2010, 03:22 PM
  4. Application to apply security at application level?
    By jk3kx in forum Droid Applications
    Replies: 4
    Last Post: 01-20-2010, 11:27 AM
  5. How do I delete application in the application screen?
    By zinny0753 in forum Droid General Discussions
    Replies: 3
    Last Post: 01-02-2010, 12:21 PM

Search tags for this page

android c2dm
,

android c2dm example

,
android c2dm sample
,
android c2dm tutorial
,
c2dm android example
,
c2dm android tutorial
,
c2dm application
,
c2dm application server
,
c2dm auth token
,

c2dm example

,
c2dm example code
,
c2dm examples
,
c2dm sample
,
c2dm sample application
,
c2dm sample code
,
c2dm server
,
c2dm server example
,
c2dm simple example
,
c2dm tutorial
,
com.google.android.c2dm.intent.register
Click on a term to search our site for related topics.