Android GCM-未获取注册令牌
我正在实施GCM。我已经在Google开发者控制台上注册了我的应用程序,启用了GCM API服务,将Google-services.json放在app文件夹中,并将以下内容放入清单中:Android GCM-未获取注册令牌,android,google-cloud-messaging,Android,Google Cloud Messaging,我正在实施GCM。我已经在Google开发者控制台上注册了我的应用程序,启用了GCM API服务,将Google-services.json放在app文件夹中,并将以下内容放入清单中: <uses-permission android:name="android.permission.WAKE_LOCK" /> <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="<my-package-name>.permission.C2D_MESSAGE" />
<application>..
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<!-- GCM -->
<receiver
android:name="com.google.android.gms.gcm.GcmReceiver"
android:exported="true"
android:permission="com.google.android.c2dm.permission.SEND">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<action android:name="android.net.conn." />
<category android:name="<my-pakage-name>" />
</intent-filter>
</receiver>
<service
android:name=".notifications.GCMListenerService"
android:exported="false">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
</intent-filter>
</service>
<service
android:name=".notifications.InstanceIDListenerService"
android:exported="false">
<intent-filter>
<action android:name="com.google.android.gms.iid.InstanceID" />
</intent-filter>
</service>
<service
android:name=".notifications.RegistrationIntentService"
android:exported="false"></service>
</application>
项目级build.gradle
所以我的问题是,我不知道上面的问题是什么,我没有得到注册令牌。请帮助我获取注册令牌您需要创建自己的接收器和服务,并像这样使用它 接收器:
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.support.v4.content.WakefulBroadcastReceiver;
public class GCMMessageReciever extends WakefulBroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
ComponentName comp = new ComponentName(context.getPackageName(),
GCMIntentService.class.getName());
startWakefulService(context, (intent.setComponent(comp)));
setResultCode(Activity.RESULT_OK);
}
}
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.preference.PreferenceManager;
import android.support.v4.app.NotificationCompat;
import android.support.v4.content.LocalBroadcastManager;
import com.appdupe.uberforxserviceprovider.R;
import com.google.android.gcm.GCMBaseIntentService;
import com.uber.driver.MyMainFragmentActivity;
import com.uber.driver.constants.Constants;
import com.uber.driver.gcm.ServerUtilities;
import com.uber.driver.util.Utils;
public class GCMIntentService extends GCMBaseIntentService {
private NotificationManager mNotificationManager;
public GCMIntentService() {
super(Constants.SENDER_ID);
}
@Override
protected void onRegistered(Context context, String registrationId) {
Intent i = new Intent(Constants.PUSHNOTIFICATION);
i.putExtra(Constants.DB_PHONE_GCM_ID, registrationId);
LocalBroadcastManager.getInstance(context).sendBroadcast(i);
ServerUtilities.register(context, "name", "email", registrationId);
}
@Override
protected void onMessage(Context context, Intent intent) {
String jsonString = intent.getExtras().getString("message");
try {
SharedPreferences preferences = PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
Editor editor = preferences.edit();
JSONObject jsonObject = new JSONObject(jsonString);
if (jsonObject.getString("id").equals("1")) {
editor.putFloat(Constants.USER_LATTITUDE,
Float.parseFloat(jsonObject.getString("lattitude")));
editor.putFloat(Constants.USER_LOGITUDE,
Float.parseFloat(jsonObject.getString("logitude")));
editor.putString(Constants.USER_RANDOM_ID,
jsonObject.getString("random_id"));
editor.putBoolean(Constants.IS_USER_SET, true);
editor.putString(Constants.PHONE_CLIENT,
jsonObject.getString("client_contact"));
editor.putString(Constants.PHONE_OPEARTOR,
jsonObject.getString("operator_contact"));
// editor.putBoolean(Constants.IS_USER_SET, true);
editor.putString(Constants.CLIENT_NAME,
jsonObject.getString("client_name"));
sendNotification(jsonString);
} else {
editor.putBoolean(Constants.DB_IS_JOB_DONE, true);
editor.putInt(Constants.FRAGMENT_POSITION,
Constants.FRAGMENT_MAP);
editor.putString(Constants.USER_RANDOM_ID, "");
}
editor.commit();
} catch (JSONException e) {
e.printStackTrace();
}
Utils.log("Received message.. " + jsonString);
Intent i = new Intent(Constants.PUSHNOTIFICATION);
i.putExtra("json", jsonString);
LocalBroadcastManager.getInstance(context).sendBroadcast(i);
}
@Override
protected void onDeletedMessages(Context context, int total) {
}
@Override
public void onError(Context context, String errorId) {
}
@Override
protected boolean onRecoverableError(Context context, String errorId) {
return super.onRecoverableError(context, errorId);
}
@Override
protected void onUnregistered(Context arg0, String arg1) {
}
private void sendNotification(String msg) {
mNotificationManager = (NotificationManager) this
.getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, MyMainFragmentActivity.class),
PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(
getResources().getString(R.string.text_uber_driver))
.setStyle(
new NotificationCompat.BigTextStyle()
.bigText(getResources().getString(
R.string.text_job_assigned)))
.setDefaults(
Notification.DEFAULT_SOUND
| Notification.DEFAULT_VIBRATE);
mBuilder.setContentIntent(contentIntent);
mBuilder.setAutoCancel(true);
Notification notification = mBuilder.build();
notification.flags |= Notification.FLAG_AUTO_CANCEL;
mNotificationManager
.notify(Constants.NOTIFICATION_ID, mBuilder.build());
}
}
<application>..
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<!-- GCM -->
<receiver
android:name=".GCMMessageReciever"
android:exported="true"
android:permission="com.google.android.c2dm.permission.SEND">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<action android:name="android.net.conn." />
<category android:name="<my-pakage-name>" />
</intent-filter>
</receiver>
<service
android:name=".GCMIntentService"
android:exported="false">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
</intent-filter>
</service>
<service
android:name=".notifications.InstanceIDListenerService"
android:exported="false">
<intent-filter>
<action android:name="com.google.android.gms.iid.InstanceID" />
</intent-filter>
</service>
<service
android:name=".notifications.RegistrationIntentService"
android:exported="false"></service>
</application>
服务:
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.support.v4.content.WakefulBroadcastReceiver;
public class GCMMessageReciever extends WakefulBroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
ComponentName comp = new ComponentName(context.getPackageName(),
GCMIntentService.class.getName());
startWakefulService(context, (intent.setComponent(comp)));
setResultCode(Activity.RESULT_OK);
}
}
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.preference.PreferenceManager;
import android.support.v4.app.NotificationCompat;
import android.support.v4.content.LocalBroadcastManager;
import com.appdupe.uberforxserviceprovider.R;
import com.google.android.gcm.GCMBaseIntentService;
import com.uber.driver.MyMainFragmentActivity;
import com.uber.driver.constants.Constants;
import com.uber.driver.gcm.ServerUtilities;
import com.uber.driver.util.Utils;
public class GCMIntentService extends GCMBaseIntentService {
private NotificationManager mNotificationManager;
public GCMIntentService() {
super(Constants.SENDER_ID);
}
@Override
protected void onRegistered(Context context, String registrationId) {
Intent i = new Intent(Constants.PUSHNOTIFICATION);
i.putExtra(Constants.DB_PHONE_GCM_ID, registrationId);
LocalBroadcastManager.getInstance(context).sendBroadcast(i);
ServerUtilities.register(context, "name", "email", registrationId);
}
@Override
protected void onMessage(Context context, Intent intent) {
String jsonString = intent.getExtras().getString("message");
try {
SharedPreferences preferences = PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
Editor editor = preferences.edit();
JSONObject jsonObject = new JSONObject(jsonString);
if (jsonObject.getString("id").equals("1")) {
editor.putFloat(Constants.USER_LATTITUDE,
Float.parseFloat(jsonObject.getString("lattitude")));
editor.putFloat(Constants.USER_LOGITUDE,
Float.parseFloat(jsonObject.getString("logitude")));
editor.putString(Constants.USER_RANDOM_ID,
jsonObject.getString("random_id"));
editor.putBoolean(Constants.IS_USER_SET, true);
editor.putString(Constants.PHONE_CLIENT,
jsonObject.getString("client_contact"));
editor.putString(Constants.PHONE_OPEARTOR,
jsonObject.getString("operator_contact"));
// editor.putBoolean(Constants.IS_USER_SET, true);
editor.putString(Constants.CLIENT_NAME,
jsonObject.getString("client_name"));
sendNotification(jsonString);
} else {
editor.putBoolean(Constants.DB_IS_JOB_DONE, true);
editor.putInt(Constants.FRAGMENT_POSITION,
Constants.FRAGMENT_MAP);
editor.putString(Constants.USER_RANDOM_ID, "");
}
editor.commit();
} catch (JSONException e) {
e.printStackTrace();
}
Utils.log("Received message.. " + jsonString);
Intent i = new Intent(Constants.PUSHNOTIFICATION);
i.putExtra("json", jsonString);
LocalBroadcastManager.getInstance(context).sendBroadcast(i);
}
@Override
protected void onDeletedMessages(Context context, int total) {
}
@Override
public void onError(Context context, String errorId) {
}
@Override
protected boolean onRecoverableError(Context context, String errorId) {
return super.onRecoverableError(context, errorId);
}
@Override
protected void onUnregistered(Context arg0, String arg1) {
}
private void sendNotification(String msg) {
mNotificationManager = (NotificationManager) this
.getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, MyMainFragmentActivity.class),
PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(
getResources().getString(R.string.text_uber_driver))
.setStyle(
new NotificationCompat.BigTextStyle()
.bigText(getResources().getString(
R.string.text_job_assigned)))
.setDefaults(
Notification.DEFAULT_SOUND
| Notification.DEFAULT_VIBRATE);
mBuilder.setContentIntent(contentIntent);
mBuilder.setAutoCancel(true);
Notification notification = mBuilder.build();
notification.flags |= Notification.FLAG_AUTO_CANCEL;
mNotificationManager
.notify(Constants.NOTIFICATION_ID, mBuilder.build());
}
}
<application>..
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<!-- GCM -->
<receiver
android:name=".GCMMessageReciever"
android:exported="true"
android:permission="com.google.android.c2dm.permission.SEND">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<action android:name="android.net.conn." />
<category android:name="<my-pakage-name>" />
</intent-filter>
</receiver>
<service
android:name=".GCMIntentService"
android:exported="false">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
</intent-filter>
</service>
<service
android:name=".notifications.InstanceIDListenerService"
android:exported="false">
<intent-filter>
<action android:name="com.google.android.gms.iid.InstanceID" />
</intent-filter>
</service>
<service
android:name=".notifications.RegistrationIntentService"
android:exported="false"></service>
</application>
Android清单:
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.support.v4.content.WakefulBroadcastReceiver;
public class GCMMessageReciever extends WakefulBroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
ComponentName comp = new ComponentName(context.getPackageName(),
GCMIntentService.class.getName());
startWakefulService(context, (intent.setComponent(comp)));
setResultCode(Activity.RESULT_OK);
}
}
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.preference.PreferenceManager;
import android.support.v4.app.NotificationCompat;
import android.support.v4.content.LocalBroadcastManager;
import com.appdupe.uberforxserviceprovider.R;
import com.google.android.gcm.GCMBaseIntentService;
import com.uber.driver.MyMainFragmentActivity;
import com.uber.driver.constants.Constants;
import com.uber.driver.gcm.ServerUtilities;
import com.uber.driver.util.Utils;
public class GCMIntentService extends GCMBaseIntentService {
private NotificationManager mNotificationManager;
public GCMIntentService() {
super(Constants.SENDER_ID);
}
@Override
protected void onRegistered(Context context, String registrationId) {
Intent i = new Intent(Constants.PUSHNOTIFICATION);
i.putExtra(Constants.DB_PHONE_GCM_ID, registrationId);
LocalBroadcastManager.getInstance(context).sendBroadcast(i);
ServerUtilities.register(context, "name", "email", registrationId);
}
@Override
protected void onMessage(Context context, Intent intent) {
String jsonString = intent.getExtras().getString("message");
try {
SharedPreferences preferences = PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
Editor editor = preferences.edit();
JSONObject jsonObject = new JSONObject(jsonString);
if (jsonObject.getString("id").equals("1")) {
editor.putFloat(Constants.USER_LATTITUDE,
Float.parseFloat(jsonObject.getString("lattitude")));
editor.putFloat(Constants.USER_LOGITUDE,
Float.parseFloat(jsonObject.getString("logitude")));
editor.putString(Constants.USER_RANDOM_ID,
jsonObject.getString("random_id"));
editor.putBoolean(Constants.IS_USER_SET, true);
editor.putString(Constants.PHONE_CLIENT,
jsonObject.getString("client_contact"));
editor.putString(Constants.PHONE_OPEARTOR,
jsonObject.getString("operator_contact"));
// editor.putBoolean(Constants.IS_USER_SET, true);
editor.putString(Constants.CLIENT_NAME,
jsonObject.getString("client_name"));
sendNotification(jsonString);
} else {
editor.putBoolean(Constants.DB_IS_JOB_DONE, true);
editor.putInt(Constants.FRAGMENT_POSITION,
Constants.FRAGMENT_MAP);
editor.putString(Constants.USER_RANDOM_ID, "");
}
editor.commit();
} catch (JSONException e) {
e.printStackTrace();
}
Utils.log("Received message.. " + jsonString);
Intent i = new Intent(Constants.PUSHNOTIFICATION);
i.putExtra("json", jsonString);
LocalBroadcastManager.getInstance(context).sendBroadcast(i);
}
@Override
protected void onDeletedMessages(Context context, int total) {
}
@Override
public void onError(Context context, String errorId) {
}
@Override
protected boolean onRecoverableError(Context context, String errorId) {
return super.onRecoverableError(context, errorId);
}
@Override
protected void onUnregistered(Context arg0, String arg1) {
}
private void sendNotification(String msg) {
mNotificationManager = (NotificationManager) this
.getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, MyMainFragmentActivity.class),
PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(
getResources().getString(R.string.text_uber_driver))
.setStyle(
new NotificationCompat.BigTextStyle()
.bigText(getResources().getString(
R.string.text_job_assigned)))
.setDefaults(
Notification.DEFAULT_SOUND
| Notification.DEFAULT_VIBRATE);
mBuilder.setContentIntent(contentIntent);
mBuilder.setAutoCancel(true);
Notification notification = mBuilder.build();
notification.flags |= Notification.FLAG_AUTO_CANCEL;
mNotificationManager
.notify(Constants.NOTIFICATION_ID, mBuilder.build());
}
}
<application>..
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<!-- GCM -->
<receiver
android:name=".GCMMessageReciever"
android:exported="true"
android:permission="com.google.android.c2dm.permission.SEND">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<action android:name="android.net.conn." />
<category android:name="<my-pakage-name>" />
</intent-filter>
</receiver>
<service
android:name=".GCMIntentService"
android:exported="false">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
</intent-filter>
</service>
<service
android:name=".notifications.InstanceIDListenerService"
android:exported="false">
<intent-filter>
<action android:name="com.google.android.gms.iid.InstanceID" />
</intent-filter>
</service>
<service
android:name=".notifications.RegistrationIntentService"
android:exported="false"></service>
</application>
。。
您在日志中看到了什么?与GCM无关..您在注册时添加了正确的发件人id吗?@Dhaval注册时将发件人id放在哪里?InstanceID InstanceID=InstanceID.getInstance(此);String token=instanceID.getToken(“发送方ID”,GoogleCloudMessaging.INSTANCE\u ID\u作用域,null)代码>在getToken
call中添加发件人id。您将在RegistrationEntertService
类中找到此代码段。