imran
imran

Reputation: 646

android GCM implementation

i want to implement google cloud messaging service CCM. but i am getting forced closed error. i am debugging the code on real device. My Main.java code is as below:

package com.imran.fgcm;

import com.google.android.gcm.GCMRegistrar;
import android.os.Bundle;
import android.app.Activity;
import android.widget.Toast;

public class Main extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        GCMRegistrar.checkDevice(this);
        GCMRegistrar.checkManifest(this);
        final String regId = GCMRegistrar.getRegistrationId(this);
        if (regId.equals("")) {
          GCMRegistrar.register(this, "123450000");
          Toast.makeText(this, regId , Toast.LENGTH_LONG).show();
        } else {
        // Log.v(TAG, "Already registered");
            Toast.makeText(this, "Already registered" , Toast.LENGTH_LONG).show();
        }

and my android manifest file is code is as below:

    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.imran.fgcm"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="15"
        android:targetSdkVersion="15" />
    <permission android:name="com.imran.fgcm.permission.C2D_MESSAGE" android:protectionLevel="signature" />
    <uses-permission android:name="com.imran.fgcm.permission.C2D_MESSAGE" /> 
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".Main"
            android:label="@string/title_activity_main" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

  <receiver android:name="com.google.android.gcm.GCMBroadcastReceiver" android:permission="com.google.android.c2dm.permission.SEND" >
  <intent-filter>
    <action android:name="com.google.android.c2dm.intent.RECEIVE" />
    <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
    <category android:name="com.imran.fgcm" />
  </intent-filter>
</receiver>
<service android:name=".GCMIntentService" />
    </application>

</manifest>

now it had stopped giving forced closed error after i added the GCMIntetservice.java class. but it is still not genrerating the device id. My logcat output is

01-03 11:34:01.023: V/GCMRegistrar(6469): Registering receiver
01-03 11:34:01.023: D/GCMRegistrar(6469): resetting backoff for com.imran.fgcm
01-03 11:34:01.058: V/GCMRegistrar(6469): Registering app com.imran.fgcm of senders 732773857047
01-03 11:34:01.288: D/TextLayoutCache(6469): Using debug level: 0 - Debug Enabled: 0
01-03 11:34:01.843: D/CLIPBOARD(6469): Hide Clipboard dialog at Starting input: finished by someone else... !
01-03 11:34:03.138: V/GCMBroadcastReceiver(6469): onReceive: com.google.android.c2dm.intent.REGISTRATION
01-03 11:34:03.138: V/GCMBroadcastReceiver(6469): GCM IntentService class: com.imran.fgcm.GCMIntentService
01-03 11:34:03.138: V/GCMBaseIntentService(6469): Acquiring wakelock
01-03 11:34:03.143: V/GCMBaseIntentService(6469): Intent service name: GCMIntentService-732773857047-1
01-03 11:34:03.148: D/GCMBaseIntentService(6469): handleRegistration: registrationId = null, error = SERVICE_NOT_AVAILABLE, unregistered = null
01-03 11:34:03.148: D/GCMBaseIntentService(6469): Registration error: SERVICE_NOT_AVAILABLE
01-03 11:34:03.148: D/GCMBaseIntentService(6469): Scheduling registration retry, backoff = 4143 (3000)
01-03 11:34:03.213: V/GCMBaseIntentService(6469): Releasing wakelock
01-03 11:34:07.318: V/GCMBroadcastReceiver(6469): onReceive: com.google.android.gcm.intent.RETRY
01-03 11:34:07.318: V/GCMBroadcastReceiver(6469): GCM IntentService class: com.imran.fgcm.GCMIntentService
01-03 11:34:07.318: V/GCMBaseIntentService(6469): Acquiring wakelock
01-03 11:34:07.353: V/GCMBaseIntentService(6469): Intent service name: GCMIntentService-732773857047-2
01-03 11:34:07.368: V/GCMRegistrar(6469): Registering app com.imran.fgcm of senders 732773857047
01-03 11:34:07.398: V/GCMBaseIntentService(6469): Releasing wakelock
01-03 11:34:07.443: V/GCMBroadcastReceiver(6469): onReceive: com.google.android.c2dm.intent.REGISTRATION
01-03 11:34:07.443: V/GCMBroadcastReceiver(6469): GCM IntentService class: com.imran.fgcm.GCMIntentService
01-03 11:34:07.443: V/GCMBaseIntentService(6469): Acquiring wakelock
01-03 11:34:07.453: V/GCMBaseIntentService(6469): Intent service name: GCMIntentService-732773857047-3
01-03 11:34:07.458: D/GCMBaseIntentService(6469): handleRegistration: registrationId = null, error = SERVICE_NOT_AVAILABLE, unregistered = null
01-03 11:34:07.458: D/GCMBaseIntentService(6469): Registration error: SERVICE_NOT_AVAILABLE
01-03 11:34:07.458: D/GCMBaseIntentService(6469): Scheduling registration retry, backoff = 7873 (6000)
01-03 11:34:07.503: V/GCMBaseIntentService(6469): Releasing wakelock
01-03 11:34:07.508: D/dalvikvm(6469): GC_CONCURRENT freed 213K, 4% free 12852K/13319K, paused 3ms+3ms
01-03 11:34:15.343: V/GCMBroadcastReceiver(6469): onReceive: com.google.android.gcm.intent.RETRY
01-03 11:34:15.343: V/GCMBroadcastReceiver(6469): GCM IntentService class: com.imran.fgcm.GCMIntentService
01-03 11:34:15.343: V/GCMBaseIntentService(6469): Acquiring wakelock
01-03 11:34:15.378: V/GCMBaseIntentService(6469): Intent service name: GCMIntentService-732773857047-4
01-03 11:34:15.393: V/GCMRegistrar(6469): Registering app com.imran.fgcm of senders 732773857047
01-03 11:34:15.428: V/GCMBaseIntentService(6469): Releasing wakelock
01-03 11:34:15.498: V/GCMBroadcastReceiver(6469): onReceive: com.google.android.c2dm.intent.REGISTRATION
01-03 11:34:15.498: V/GCMBroadcastReceiver(6469): GCM IntentService class: com.imran.fgcm.GCMIntentService
01-03 11:34:15.498: V/GCMBaseIntentService(6469): Acquiring wakelock
01-03 11:34:15.503: V/GCMBaseIntentService(6469): Intent service name: GCMIntentService-732773857047-5
01-03 11:34:15.508: D/GCMBaseIntentService(6469): handleRegistration: registrationId = null, error = SERVICE_NOT_AVAILABLE, unregistered = null
01-03 11:34:15.508: D/GCMBaseIntentService(6469): Registration error: SERVICE_NOT_AVAILABLE
01-03 11:34:15.508: D/GCMBaseIntentService(6469): Scheduling registration retry, backoff = 7347 (12000)
01-03 11:34:15.538: V/GCMBaseIntentService(6469): Releasing wakelock
01-03 11:34:22.863: V/GCMBroadcastReceiver(6469): onReceive: com.google.android.gcm.intent.RETRY
01-03 11:34:22.863: V/GCMBroadcastReceiver(6469): GCM IntentService class: com.imran.fgcm.GCMIntentService
01-03 11:34:22.863: V/GCMBaseIntentService(6469): Acquiring wakelock
01-03 11:34:22.908: V/GCMBaseIntentService(6469): Intent service name: GCMIntentService-732773857047-6
01-03 11:34:22.923: V/GCMRegistrar(6469): Registering app com.imran.fgcm of senders 732773857047
01-03 11:34:22.958: V/GCMBaseIntentService(6469): Releasing wakelock
01-03 11:34:22.968: V/GCMBroadcastReceiver(6469): onReceive: com.google.android.c2dm.intent.REGISTRATION
01-03 11:34:22.968: V/GCMBroadcastReceiver(6469): GCM IntentService class: com.imran.fgcm.GCMIntentService
01-03 11:34:22.968: V/GCMBaseIntentService(6469): Acquiring wakelock
01-03 11:34:22.978: V/GCMBaseIntentService(6469): Intent service name: GCMIntentService-732773857047-7
01-03 11:34:22.978: D/GCMBaseIntentService(6469): handleRegistration: registrationId = null, error = SERVICE_NOT_AVAILABLE, unregistered = null
01-03 11:34:22.983: D/GCMBaseIntentService(6469): Registration error: SERVICE_NOT_AVAILABLE
01-03 11:34:22.983: D/GCMBaseIntentService(6469): Scheduling registration retry, backoff = 35123 (24000)
01-03 11:34:23.013: V/GCMBaseIntentService(6469): Releasing wakelock
01-03 11:34:58.113: V/GCMBroadcastReceiver(6469): onReceive: com.google.android.gcm.intent.RETRY
01-03 11:34:58.118: V/GCMBroadcastReceiver(6469): GCM IntentService class: com.imran.fgcm.GCMIntentService
01-03 11:34:58.118: V/GCMBaseIntentService(6469): Acquiring wakelock
01-03 11:34:58.153: V/GCMBaseIntentService(6469): Intent service name: GCMIntentService-732773857047-8
01-03 11:34:58.168: V/GCMRegistrar(6469): Registering app com.imran.fgcm of senders 732773857047
01-03 11:34:58.203: V/GCMBaseIntentService(6469): Releasing wakelock
01-03 11:34:58.228: V/GCMBroadcastReceiver(6469): onReceive: com.google.android.c2dm.intent.REGISTRATION
01-03 11:34:58.228: V/GCMBroadcastReceiver(6469): GCM IntentService class: com.imran.fgcm.GCMIntentService
01-03 11:34:58.228: V/GCMBaseIntentService(6469): Acquiring wakelock
01-03 11:34:58.233: V/GCMBaseIntentService(6469): Intent service name: GCMIntentService-732773857047-9
01-03 11:34:58.238: D/GCMBaseIntentService(6469): handleRegistration: registrationId = null, error = SERVICE_NOT_AVAILABLE, unregistered = null
01-03 11:34:58.238: D/GCMBaseIntentService(6469): Registration error: SERVICE_NOT_AVAILABLE
01-03 11:34:58.238: D/GCMBaseIntentService(6469): Scheduling registration retry, backoff = 67761 (48000)
01-03 11:34:58.268: V/GCMBaseIntentService(6469): Releasing wakelock
01-03 11:35:50.863: D/CLIPBOARD(6469): Hide Clipboard dialog at Starting input: finished by someone else... !
01-03 11:35:52.688: E/ActivityThread(6469): Activity com.imran.fgcm.Main has leaked IntentReceiver com.google.android.gcm.GCMBroadcastReceiver@41903ec8 that was originally registered here. Are you missing a call to unregisterReceiver()?
01-03 11:35:52.688: E/ActivityThread(6469): android.app.IntentReceiverLeaked: Activity com.imran.fgcm.Main has leaked IntentReceiver com.google.android.gcm.GCMBroadcastReceiver@41903ec8 that was originally registered here. Are you missing a call to unregisterReceiver()?
01-03 11:35:52.688: E/ActivityThread(6469):     at android.app.LoadedApk$ReceiverDispatcher.<init>(LoadedApk.java:763)
01-03 11:35:52.688: E/ActivityThread(6469):     at android.app.LoadedApk.getReceiverDispatcher(LoadedApk.java:567)
01-03 11:35:52.688: E/ActivityThread(6469):     at android.app.ContextImpl.registerReceiverInternal(ContextImpl.java:1118)
01-03 11:35:52.688: E/ActivityThread(6469):     at android.app.ContextImpl.registerReceiver(ContextImpl.java:1105)
01-03 11:35:52.688: E/ActivityThread(6469):     at android.content.ContextWrapper.registerReceiver(ContextWrapper.java:355)
01-03 11:35:52.688: E/ActivityThread(6469):     at com.google.android.gcm.GCMRegistrar.setRetryBroadcastReceiver(GCMRegistrar.java:293)
01-03 11:35:52.688: E/ActivityThread(6469):     at com.google.android.gcm.GCMRegistrar.register(GCMRegistrar.java:215)
01-03 11:35:52.688: E/ActivityThread(6469):     at com.imran.fgcm.Main.onCreate(Main.java:19)
01-03 11:35:52.688: E/ActivityThread(6469):     at android.app.Activity.performCreate(Activity.java:4470)
01-03 11:35:52.688: E/ActivityThread(6469):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1052)
01-03 11:35:52.688: E/ActivityThread(6469):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1931)
01-03 11:35:52.688: E/ActivityThread(6469):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992)
01-03 11:35:52.688: E/ActivityThread(6469):     at android.app.ActivityThread.access$600(ActivityThread.java:127)
01-03 11:35:52.688: E/ActivityThread(6469):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158)
01-03 11:35:52.688: E/ActivityThread(6469):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-03 11:35:52.688: E/ActivityThread(6469):     at android.os.Looper.loop(Looper.java:137)
01-03 11:35:52.688: E/ActivityThread(6469):     at android.app.ActivityThread.main(ActivityThread.java:4511)
01-03 11:35:52.688: E/ActivityThread(6469):     at java.lang.reflect.Method.invokeNative(Native Method)
01-03 11:35:52.688: E/ActivityThread(6469):     at java.lang.reflect.Method.invoke(Method.java:511)
01-03 11:35:52.688: E/ActivityThread(6469):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:980)
01-03 11:35:52.688: E/ActivityThread(6469):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:747)
01-03 11:35:52.688: E/ActivityThread(6469):     at dalvik.system.NativeStart.main(Native Method)
01-03 11:36:06.028: V/GCMRegistrar(6860): Registering receiver
01-03 11:36:06.033: D/GCMRegistrar(6860): resetting backoff for com.imran.fgcm
01-03 11:36:06.053: V/GCMRegistrar(6860): Registering app com.imran.fgcm of senders 732773857047
01-03 11:36:06.093: D/TextLayoutCache(6860): Using debug level: 0 - Debug Enabled: 0
01-03 11:36:06.178: D/CLIPBOARD(6860): Hide Clipboard dialog at Starting input: finished by someone else... !
01-03 11:36:06.203: V/GCMBroadcastReceiver(6860): onReceive: com.google.android.c2dm.intent.REGISTRATION
01-03 11:36:06.203: V/GCMBroadcastReceiver(6860): GCM IntentService class: com.imran.fgcm.GCMIntentService
01-03 11:36:06.203: V/GCMBaseIntentService(6860): Acquiring wakelock
01-03 11:36:06.218: V/GCMBaseIntentService(6860): Intent service name: GCMIntentService-732773857047-1
01-03 11:36:06.238: D/GCMBaseIntentService(6860): handleRegistration: registrationId = null, error = SERVICE_NOT_AVAILABLE, unregistered = null
01-03 11:36:06.238: D/GCMBaseIntentService(6860): Registration error: SERVICE_NOT_AVAILABLE
01-03 11:36:06.238: D/GCMBaseIntentService(6860): Scheduling registration retry, backoff = 2363 (3000)
01-03 11:36:06.263: V/GCMBaseIntentService(6860): Releasing wakelock
01-03 11:36:08.618: V/GCMBroadcastReceiver(6860): onReceive: com.google.android.gcm.intent.RETRY
01-03 11:36:08.618: V/GCMBroadcastReceiver(6860): GCM IntentService class: com.imran.fgcm.GCMIntentService
01-03 11:36:08.618: V/GCMBaseIntentService(6860): Acquiring wakelock
01-03 11:36:08.653: V/GCMBaseIntentService(6860): Intent service name: GCMIntentService-732773857047-2
01-03 11:36:08.668: E/GCMBaseIntentService(6860): Received invalid token: 1111101101100100100110110001110011001001010011110110001010010110
01-03 11:36:08.668: V/GCMBaseIntentService(6860): Releasing wakelock

Upvotes: 2

Views: 9061

Answers (3)

Neha
Neha

Reputation: 424

TO receive the Notification you need to override the onMessage method from GCMIntentService class as follows

@Override
    protected void onMessage(Context arg0, Intent arg1) {

        Log.d("GCM", "RECIEVED A MESSAGE");
        // Get the data from intent and send to notificaion bar
        generateNotification(arg0, arg1.getStringExtra("**mymsg**"));
    }

here "mymsg" should me match with your php message attribute. Please Note that both name should be same in php and in android otherwise you will not receive the notification.

Upvotes: 1

Neha
Neha

Reputation: 424

For GCM implementation. Create a new project and add GCM.jar file to its buildpath. Generate the Manifiest file as per the below.

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

    <uses-sdk android:minSdkVersion="8" />

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />

    <permission
        android:name="YOUR PACKAGE NAME.permission.C2D_MESSAGE"
        android:protectionLevel="signature" />

    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
    <uses-permission android:name="YOUR PACKAGE NAME.permission.C2D_MESSAGE" />
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
   <uses-permission android:name="android.permission.USE_CREDENTIALS" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".DemoGCMPush"
            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="com.google.android.gcm.GCMBroadcastReceiver"
            android:permission="com.google.android.c2dm.permission.SEND" >
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
                <action android:name="com.google.android.c2dm.intent.REGISTRATION" />

                <category android:name="YOUR PACKAGE NAME" />
            </intent-filter>
        </receiver>

        <service android:name="YOUR PACKAGE NAME.GCMIntentService" />
    </application>

</manifest>

Then in your main activity add the following code to register. replace the SENDER_ID with the 12 digit project Id from the URL.

GCMRegistrar.checkDevice(this);
GCMRegistrar.checkManifest(this);
final String regId = GCMRegistrar.getRegistrationId(this);
if (regId.equals("")) {
  GCMRegistrar.register(this, SENDER_ID);
} else {
  Log.v(TAG, "Already registered");
}

and finally add second class of GCMIntentService.java for handling GCM messages.

import android.app.IntentService;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import com.google.android.gcm.GCMBaseIntentService;

/**
 * {@link IntentService} responsible for handling GCM messages.
 */
public class GCMIntentService extends GCMBaseIntentService {

    @SuppressWarnings("hiding")
    private static final String TAG = "GCMIntentService";

    public GCMIntentService() {
        super("SENDER ID");
    }

    /**
     * Issues a notification to inform the user that server has sent a message.
     */
    private static void generateNotification(Context context, String message) {
        long when = System.currentTimeMillis();
        NotificationManager notificationManager = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);
        Notification notification = new Notification(R.drawable.ic_launcher,
                message, when);
        String title = context.getString(R.string.app_name);
        Intent notificationIntent = new Intent(context,
                SamplePushActivity.class);
        // set intent so it does not start a new activity
        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
                | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        PendingIntent intent = PendingIntent.getActivity(context, 0,
                notificationIntent, 0);
        notification.setLatestEventInfo(context, title, message, intent);
        notification.flags |= Notification.FLAG_AUTO_CANCEL;
        notificationManager.notify(0, notification);
    }

    @Override
    protected void onError(Context arg0, String arg1) {
        // TODO Auto-generated method stub

    }

    @Override
    protected void onMessage(Context arg0, Intent arg1) {

        Log.d("GCM", "RECIEVED A MESSAGE");
        // Get the data from intent and send to notificaion bar
        generateNotification(arg0, arg1.getStringExtra("message"));
    }

    @Override
    protected void onRegistered(Context arg0, String arg1) {
        // TODO Auto-generated method stub

    }

    @Override
    protected void onUnregistered(Context arg0, String arg1) {
        // TODO Auto-generated method stub

    }

}

Upvotes: 2

Neha
Neha

Reputation: 424

Have you add your project Id in GCMIntentService class properly. Also make sure that the device is having Android Market/Play Store installed and signed in using a Google Account.

Upvotes: 1

Related Questions