我查看了几个教程,仍然无法在我的应用中完成此部分。 对不起,我很新,所以请不要恭喜我。
我的清单中的所有内容都正确,我在MainActivity中
public void registerService(){
Intent registrationIntent=new Intent ("com.google.android.c2dm.intent.REGISTER");
registrationIntent.putExtra("app",PendingIntent.getBroadcast(mContext,0, new Intent(), 0));
registrationIntent.putExtra("sender", stringWithSenderID);
startService(registrationIntent);
}
GCMReceiver:
public class GCMReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
try{
String action=intent.getAction();
if (action.equals("com.google.android.c2dm.intent.REGISTRATION")){
String registrationid=intent.getStringExtra("registration_id");
Log.d("MyLog","registrationid ="+registrationid);
String error= intent.getStringExtra("error");
String unregistered=intent.getStringExtra("unregistered");
}
else if (action.equals("com.google.android.c2dm.intent.RECEIVE")){
String data1=intent.getStringExtra("data1");
String data2=intent.getStringExtra("data2");
}
} finally{
}
}
}
所以我的registration_id记录正确,我接下来该怎么办?我是否必须以某种方式将其发送到服务器?
答案 0 :(得分:0)
我认为它可以帮到你:
public String Sender_id = "your_google_api_sender_id";
try {
GCMRegistrar.checkDevice(this);
GCMRegistrar.checkManifest(this);
String regId = GCMRegistrar.getRegistrationId(this);
if (regId.equals("")) {
GCMRegistrar.register(this, Sender_id);
} else {
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
在GCm服务中,我们必须这样写:
public class GCMIntentService extends GCMBaseIntentService {
@Override
protected void onError(Context context, String error) {
// TODO Auto-generated method stub
if (error != null) {
// optionally retry using exponential back-off
// (see Advanced Topics)
Toast.makeText(getBaseContext(), "" + error, Toast.LENGTH_SHORT)
.show();
}
}
public GCMIntentService() {
super("your_snde_id");
// SENDER_ID is my project id into google account url
// TODO Auto-generated constructor stub
}
public GCMIntentService(String senderId) {
super(senderId);
// TODO Auto-generated constructor stub
}
@Override
protected void onMessage(Context context, Intent intent) {
String message = intent.getStringExtra("message");
// TODO Auto-generated method stub
//createNotification(context, message);
}
@Override
protected void onRegistered(Context context, String regId) {
// TODO Auto-generated method stub
// you need to handle what you have to do after registration
}
@Override
protected void onUnregistered(Context context, String error) {
// TODO Auto-generated method stub
}
}
和In Manifest文件如下:
<permission
android:name="your.package.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="your.package.permission.C2D_MESSAGE" />
<!-- App receives GCM messages. -->
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<receiver
android:name="com.google.android.gcm.GCMBroadcastReceiver"
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="com.google.android.c2dm.intent.UNREGISTER" />
<category android:name="com.activelifeapps.android.alAlleghany" />
</intent-filter>
</receiver>
<service android:name="your.package.GCMIntentService" />