我正在实施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="<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>
apply plugin: 'com.google.gms.google-services'
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.android.support:design:23.1.1'
compile 'com.google.android.gms:play-services:8.1.0'
compile 'com.google.android.gms:play-services-gcm:8.1.0'
}
dependencies {
classpath 'com.android.tools.build:gradle:1.5.0'
classpath 'com.google.gms:google-services:1.4.0-beta3'
}
因此我的查询是,我不知道上面有什么问题我得到了注册令牌。请帮我拿到注册令牌
答案 0 :(得分:1)
您需要创建自己的Receiver and Service并像这样使用它。
<强>接收器:强>
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());
}
}
Android清单:
<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>