Android 设备令牌第一次返回null
我使用以下代码在android设备中获取设备令牌。但是我第一次尝试得到的是空值Android 设备令牌第一次返回null,android,push-notification,google-cloud-messaging,Android,Push Notification,Google Cloud Messaging,我使用以下代码在android设备中获取设备令牌。但是我第一次尝试得到的是空值 GCMRegistrar.checkDevice(this); GCMRegistrar.checkManifest(this); String regId = GCMRegistrar.getRegistrationId(this); if (regId.equals("")) { GCMRegistrar.register(this, UtilsGcm.GCMSend
GCMRegistrar.checkDevice(this);
GCMRegistrar.checkManifest(this);
String regId = GCMRegistrar.getRegistrationId(this);
if (regId.equals("")) {
GCMRegistrar.register(this, UtilsGcm.GCMSenderId);
}
这是我使用的GCM课程
public class GCMIntentService extends GCMBaseIntentService {
public GCMIntentService() {
super(UtilsGcm.GCMSenderId);
}
@Override
protected void onError(Context context, String regId) {
Log.e("", "error registration id : " + regId);
}
@Override
protected void onMessage(Context context, Intent intent) {
handleMessage(context, intent);
}
@Override
protected void onRegistered(Context context, String regId) {
System.out.println("on registered" + regId);
UtilsGcm.registrationId = regId;
handleRegistration(context, regId);
}
@Override
protected void onUnregistered(Context arg0, String arg1) {
}
}
我不知道它会不会成功,无论如何,试试看……让我知道 在你的GCM课程中,如果没有放在那里,请检查是否有
private void handleRegistration(Context context, String regId)
{
// TODO Auto-generated method stub
Utils.registrationId = regId;
SavePreferences("PUSHNOT",regId);
Log.e("", "registration id : "+regId);
//sendDeviceIdFuction(context,UtilsGcm.registrationId);
}
private void SavePreferences(String key, String value)
{
// TODO Auto-generated method stub
SharedPreferences sharedPreferences = getSharedPreferences("myPrefs", MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(key, value);
editor.commit();
}
在这里,在应用程序首选项中注册后保存regid。请尝试检索该首选项值,而不是使用字符串regid 问题是,如果您试图在返回null之前导航,那么生成设备id需要时间,您是否也可以发布您的gcm类。。