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
{
}
介绍浏览(本)用于GCM。[http://www.androidhive.info/2012/10/android-push-notifications-using-google-cloud-messaging-gcm-php-and-mysql/ ] –
使用最新的谷歌云消息库 –