egydeveloper
egydeveloper

Reputation: 585

how to display device registration id on message window

I had Android app and I used the GCM and I want to display the device registration ID window or on log cat to test the app , I had ameesage on log cat that the emulator had regiseration ID but I need to see it How???

Also when I tested the app on my mobile the stopped project ... message appeared although it didnot appear on emulator and when I commented these permissions

<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" />
    <uses-permission android:name="android.permission.WRITE_SETTINGS" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />

this message disappeared

class

package com.example.elarabygroup;

import com.google.android.gcm.GCMBaseIntentService;
import com.google.android.gcm.GCMRegistrar;

import android.content.Context;
import android.content.Intent;
import android.os.PowerManager;
import android.util.Log;

public class GCMIntenetService extends GCMBaseIntentService {
    private static final String GCM_SENDER_ID = "1111111111";

    public GCMIntenetService() {
        super();
    }

    @Override
    protected void onRegistered(Context context, String registrationId) {
        Log.i(TAG, "Device registered: regId = " + registrationId);
        GCMRegistrar.setRegisteredOnServer(context, true);
    }

    @Override
    protected void onUnregistered(Context context, String registrationId) {
        Log.i(TAG, "Device unregistered");
        if (GCMRegistrar.isRegisteredOnServer(context)) {
            String regId = "";
            Log.i(TAG, "unregistering device (regId = " + regId + ")");
            GCMRegistrar.setRegisteredOnServer(context, false);
        } else {
            // This callback results from the call to unregister made on
            // ServerUtilities when the registration to the server failed.
            Log.i(TAG, "Ignoring unregister callback");
        }
    }

    @Override
    protected void onError(Context context, String errorId) {
        // push error processing
    }

    @Override
    protected void onMessage(Context arg0, Intent arg1) {
        Log.i(TAG, "Received message");
        Log.i(TAG, "EXTRAS" + arg1.getExtras());
        //String message = getString(R.string.gcm_message);
        generateNotification(arg0, arg1.getStringExtra("Please download our new updates"));
        // notifies user about message

    }

    private void generateNotification(Context arg0, String stringExtra) {
        // TODO Auto-generated method stub

    }

    public static void registerInGCMService(Context context) {
        if (!checkIsGCMServiceAvailable(context)) {
            return;
        }
        final String regId = GCMRegistrar.getRegistrationId(context);
        if (regId.equals("")) {
            try {
                GCMRegistrar.register(context, GCM_SENDER_ID);
            } catch (Exception ex) {
            }
        } else {
            // Already registered
        }
    }

    public static boolean checkIsGCMServiceAvailable(Context context) {
        try {
            GCMRegistrar.checkDevice(context);
            GCMRegistrar.checkManifest(context);
            return true;
        } catch (Throwable th) {
            return false;
        }
    }

}

Upvotes: 0

Views: 1093

Answers (2)

javid piprani
javid piprani

Reputation: 1965

You can call Broadcast receiver in activity where u want to Display or pop up message like toast .below code do same

1) Modify onRegistered () method below

protected void onRegistered(Context context, String registrationId) 
{
        Log.i(TAG, "Device registered: regId = " + registrationId);
        GCMRegistrar.setRegisteredOnServer(context, true);
        CommonUtilities.displayMessage(context,registrationId);
}

2) Change displayMessage() of common util like below

static void displayMessage (Context context, String message,String senderID,String recieverId,String Type) 

{
            Intent intent = new Intent(DISPLAY_MESSAGE_ACTION);
            intent.putExtra(EXTRA_MESSAGE, message);
            context.sendBroadcast(intent);
}

3) on Main activity write we make Boradcaste receiver which notify user when application registered with gcm put below code in mainactivity or where u want to display popup

    @Override
        protected void onResume() {

            registerReceiver(mHandleMessageReceiver, new IntentFilter(
                    CommonUtilities.DISPLAY_MESSAGE_ACTION));

        }

    private final BroadcastReceiver mHandleMessageReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) 
            {
                        // you pop message display here when app. will get registered
Toast.makeText(context,getIntent().getExtras().getString(CommonUtilities.EXTRA_MESSAGE), 2).show();

            }
        };

    @Override
        protected void onStop() {
            // TODO Auto-generated method stub
            super.onPause();
            relaseMemory();
        }

Upvotes: 0

Arun Padmanabhan
Arun Padmanabhan

Reputation: 119

Settings.Secure#ANDROID_ID returns the Android ID as an unique 64-bit hex string.

import android.provider.Settings.Secure;

private String android_id = Secure.getString(getContext().getContentResolver(),
                                                        Secure.ANDROID_ID); 

android_id contains the device id.

As a sample I have added the change only to the registerInGCMService() method.

public class GCMIntenetService extends GCMBaseIntentService {
    private static String GCM_SENDER_ID ;

    public GCMIntenetService() {
        super();
    }



    public static void registerInGCMService(Context context) {


         GCM_SENDER_ID =  Secure.getString(getContext().getContentResolver(),
                                                            Secure.ANDROID_ID); 

        if (!checkIsGCMServiceAvailable(context)) {
            return;
        }
        final String regId = GCMRegistrar.getRegistrationId(context);
        if (regId.equals("")) {
            try {
                GCMRegistrar.register(context, GCM_SENDER_ID);
            } catch (Exception ex) {
            }
        } else {
            // Already registered
        }
    }



}

Upvotes: 1

Related Questions