无法在GCM中获取注册ID

时间:2014-02-28 06:28:33

标签: android

我正在开发一个Android应用程序,其中我需要实现GCM服务,以便在服务器上发生任何更改时向每个用户移动设备发送通知。但是我坚持使用initail步骤来获取注册ID。所以请帮助我。我写了一些代码。

bt1=(Button) findViewById(R.id.button1);
    bt2=(Button) findViewById(R.id.button2);
    bt1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Log.d("msg", "Button clicked");

            Intent it=new Intent("com.google.android.c2dm.intent.REGISTER");
            Toast.makeText(MainActivity.this, "Intent created", Toast.LENGTH_LONG).show();
            it.putExtra("app", PendingIntent.getBroadcast(v.getContext(), 0, new Intent(), 0));
            it.putExtra("senderid","senderid");
            startService(it);
            Toast.makeText(MainActivity.this, "Intent Register", Toast.LENGTH_LONG).show();

        }
    });
    bt2.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent unregister=new Intent("com.google.android.c2dm.intent.UNREGISTER");
            unregister.putExtra("app", PendingIntent.getBroadcast(v.getContext(), 0, new Intent(), 0));
            startService(unregister);
            Toast.makeText(MainActivity.this, "Intent unregister", Toast.LENGTH_LONG).show();

        }
    });

GCM Broadcastreciever类代码。

try
        {
            String action=intent.getAction();
            Toast.makeText(context, "getaction", Toast.LENGTH_LONG).show();
            if(action.equals("com.google.android.c2dm.intent.REGISTRATION"))
            {
                String registrationid=intent.getStringExtra("Registration_id");
                Toast.makeText(context, "registration id is"+registrationid, Toast.LENGTH_LONG).show();
                Log.d("registrationid", registrationid);
                String error=intent.getStringExtra("error");
                Toast.makeText(context, "Error:"+error, Toast.LENGTH_LONG).show();
                String unregistered=intent.getStringExtra("unregistered");
                Toast.makeText(context, "Unregistered:"+unregistered, Toast.LENGTH_LONG).show();
            }
            else if(action.equals("com.google.android.c2dm.intent.RECEIVE"))
            {
                String data1=intent.getStringExtra("data1");
                Toast.makeText(context, data1, Toast.LENGTH_LONG).show();
            //  Log.i("data1::",data1);
                String data2=intent.getStringExtra("data2");
                Log.i("data2::",data2);
            }


        }
finally
{

}

1 个答案:

答案 0 :(得分:0)

        // Make sure the device has the proper dependencies.
        GCMRegistrar.checkDevice(getApplicationContext());
//        GCMRegistrar.checkManifest(this);

        final String regId = GCMRegistrar.getRegistrationId(getApplicationContext());
        System.out.println("GCM Registration ID : "+regId);
        if (regId.equals("")) {
            // Automatically registers application on startup.
            GCMRegistrar.register(getApplicationContext(), SENDER_ID);
        } else {
            myGcmId = regId;
            // Device is already registered on GCM, check server.
            if (!GCMRegistrar.isRegisteredOnServer(getApplicationContext())) {
                GCMRegistrar.setRegisteredOnServer(getApplicationContext(), true);
            }
        }

在下面的课程中接收注册ID。我从代码中提取了一些部分。因此,剪切中可能存在一些无关的变量。

public class GCMIntentService extends GCMBaseIntentService {

    private static final String TAG = "GCMIntentService";

    public GCMIntentService() {
        super("YOUR_SENDER_ID");
    }

    @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, "Ignoring unregister callback");
    }


    @Override
    protected void onMessage(Context context, Intent intent) {
        Log.i(TAG, "Received message");
        System.out.println("GCM Message Received : "+ getPayload(intent));

        try {
            String payload = getPayload(intent);

            if(payload != null)
            {
                String data = payload.split(",,")[0];
                printGCMMessage(context, data);


                System.out.println("OnMessageREceive - can send to pebble : "+canSendPebble(context));
                if(canSendPebble(context))
                {
                    sendPebble("Notification", data);
                }

                vibrateWatch(context);


            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    String getPayload( Intent intent)
    {
        String data = null;

        try {
            data = intent.getStringExtra("payload");
        } catch (NullPointerException e) {
            e.printStackTrace();
        }

        if((intent.getAction().equals("com.google.android.c2dm.intent.RECEIVE")) && intent.getStringExtra("payload") != null && data != null) {
            System.out.println("GCM Message Data : "+data);
        }

        return data;
    }
}