我正在尝试使用模拟器实现谷歌云消息传递。 但在服务器端设备令牌是必需的。 如何获取此令牌。 如何获取变量"设备"
import com.google.android.gcm.server.*;
Sender sender = new Sender(myApiKey);
Message message = new Message.Builder().build();
MulticastResult result = sender.send(message, devices, 5);
您可以
像这样获取设备 ID
import android.provider.Settings.Secure;
String android_id=Secure.getString(getContext().getContentResolver(),Secure.ANDROID_ID);
它向GCM注册应用程序。作为回报,它将给出一个字母数字字符串,该字符串将发送到我们的服务器进行通知。要获取项目的GCM ID,请遵循[开发人员页面]:http://developer.android.com/guide/google/gcm/gs.html
public String registerGCM(Context context)
{
String TAG = "GCM Already register";
String SENDER_ID =<Your Gcm ID>;
GCMRegistrar.checkDevice(context);
GCMRegistrar.checkManifest(context);
String gcmRegId = GCMRegistrar.getRegistrationId(context);
System.out.println("GCM Reg id is ======>"+gcmRegId);
if (gcmRegId.equals(""))
{
GCMRegistrar.register(context, SENDER_ID);
System.out.println("GCM Reg id is ======>blank");
String gcmregID = GCMRegistrar.getRegistrationId(context);
System.out.println("GCM Reg id is ======>"+gcmregID);
return gcmregID;
}
else
{
Log.v(TAG, "Already registered");
}
return gcmRegId;
}
通过以下方式获取设备 ID
import android.provider.Settings.Secure;
String android_id=Secure.getString(getContext().getContentResolver(),Secure.ANDROID_ID);