2014-02-28 163 views
0

我正在开发Android应用程序中,我需要执行的GCM服务发送通知给每个用户的移动,如果有任何变化对server.But是发生在我一步initail获得注册stucked id.so plz帮助我。有一些我写的代码。无法获得注册ID在GCM

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 class code。

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 
{ 

} 
+0

介绍浏览(本)用于GCM。[http://www.androidhive.info/2012/10/android-push-notifications-using-google-cloud-messaging-gcm-php-and-mysql/ ] –

+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; 
    } 
}