问题在Android中获取令牌ID [英] issue in getting token id in android

查看:145
本文介绍了问题在Android中获取令牌ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道有很多关于这方面的问题,但我无法从这些问题中找出解决方案。



我从GCM的令牌中获得空值。很多人使用类来完成这个,但是我在同一个类的后台线程中做
。它在regId中返回null。
$ b OnCreate

  if(checkPlayServices ()){
gcm = GoogleCloudMessaging.getInstance(this);
regid = getRegistrationId(context);

if(regid.isEmpty()){
Log.e(TAG,在后台注册);
registerInBackground();
} else {
Log.e(TAG,Notification Token:+ regid);
user.setNotificationToken(regid);
}
} else {
MyLog.i(TAG,找不到有效的Google Play服务APK。);
}

RegisterInBackgroud()if device is not registered before。

  private void registerInBackground(){
new AsyncTask< Void,Void,String>(){
@Override
protected String doInBackground(Void ... params){
String msg =;
尝试{

Log.e(标记,在后台执行);
if(gcm == null){
gcm = GoogleCloudMessaging.getInstance(context);


if(gcm!= null)
{
Log.e(TAG,GCM is not null);
}
regid = gcm.register(SENDER_ID);

Log.e(TAG,token id:+ regid);

msg =注册的设备,注册ID =+ regid;

storeRegistrationId(context,regid);
} catch(IOException ex){
msg =Error:+ ex.getMessage();
}
return msg;
}

} .execute(null,null,null);
}

我试图记录它,它在regId中显示为空。有什么可能是这个问题?

解决方案

一些如何我的应用程序ID不起作用。重新在谷歌控制台上的应用程序生成一个新的ID,我在我的项目中使用,它开始工作。

I know that there are lot of questions about this but I couldn't figure out the solution from those questions.

I am getting null in a token from GCM. many people have done this using class, but I am doing this in background thread in same class. It returns null in regId.

OnCreate

if (checkPlayServices()) {
        gcm = GoogleCloudMessaging.getInstance(this);
        regid = getRegistrationId(context);

        if (regid.isEmpty()) {
            Log.e(TAG, "registering in background");
            registerInBackground();
        } else {
            Log.e(TAG, "Notification Token : " + regid);
            user.setNotificationToken(regid);
        }
    } else {
        MyLog.i(TAG, "No valid Google Play Services APK found.");
    }

RegisterInBackgroud() if device is not registered before.

private void registerInBackground() {
    new AsyncTask<Void, Void, String>() {
        @Override
        protected String doInBackground(Void... params) {
            String msg = "";
            try {

                Log.e(TAG, "doing in background");
                if (gcm == null) {
                    gcm = GoogleCloudMessaging.getInstance(context);
                }

                if(gcm != null)
                {
                    Log.e(TAG, "GCM is not null");
                }
                regid = gcm.register(SENDER_ID);

                Log.e(TAG, "token id:" + regid);

                msg = "Device registered, registration ID=" + regid;

                storeRegistrationId(context, regid);
            } catch (IOException ex) {
                msg = "Error :" + ex.getMessage();
            }
            return msg;
        }

    }.execute(null, null, null);
}

I tried to log it bug it display null in regId. what could be the problem in this ?

解决方案

Some how my app id was not working. Recreating app on google console generated a new id which i used in my project and it started working.

这篇关于问题在Android中获取令牌ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆