br0k3nc0d3
br0k3nc0d3

Reputation: 1

Google Cloud Messaging developer android code crashing

Im trying to make an application with Google Cloud Messaging and i followd all the exact steps for implementing GCMclient on developer page, code just seems to crash on launch. Is there any fault in activity main? or some logical flaw of how android sets up its application on creation? im new to android programming.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.iotproj.clandestine"
android:versionCode="1"
android:versionName="1.0" >

<meta-data android:name="com.google.android.gms.version"
       android:value="@integer/google_play_services_version" />

<uses-sdk
    android:minSdkVersion="19"
    android:targetSdkVersion="19" />

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />


<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.iotproj.clandestine.MainActivity"
        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="GcmBroadcastReceiver"
        android:permission="com.google.android.c2dm.permission.SEND" >
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            <category android:name="com.iotproj.clandestine" />
        </intent-filter>
    </receiver>
    <service android:name="GcmIntentService" />

  </application>


  </manifest>



          public class MainActivity extends Activity {

private final static int PLAY_SERVICES_RESOLUTION_REQUEST = 9000;TextView mDisplay;
Button mButton;

GoogleCloudMessaging gcm;
Context appContext = getApplicationContext();

// this is quite important dude
String registrationId = null;


String SENDER_ID = "xxxxxxxxx";

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mDisplay = (TextView) findViewById(R.id.pool);
    mButton  = (Button) findViewById(R.id.getid);

    // Check device for Play Services APK.
    if (!checkPlayServices()) {

        mDisplay.setText("device not supproted !");   button.setOnClickListener(new  View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            gcm = GoogleCloudMessaging.getInstance(appContext);
            try{
                registrationId = gcm.register(SENDER_ID);   
            }
            catch(IOException e){
                mDisplay.setText(e.getMessage());
            }               
        }
    });
}`

Upvotes: 0

Views: 447

Answers (1)

Piyush
Piyush

Reputation: 18933

Register your broadcast receiver like this way.

  <receiver
        android:name="com.google.android.gcm.GCMBroadcastReceiver"
        android:permission="com.google.android.c2dm.permission.SEND" >
        <intent-filter>

            <!-- Receives the actual messages. -->
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            <!-- Receives the registration id. -->
            <action android:name="com.google.android.c2dm.intent.REGISTRATION" />

            <category android:name="com.iotproj.clandestine" />
        </intent-filter>
 </receiver>

for Service

 <service android:name=".GCMIntentService" />

Upvotes: 1

Related Questions