在这里,我遇到了另一个问题push notification
,它可以完美地工作,emulator
但不能正常工作device
。
代码:对于GCMIntentService
public class GCMIntentService extends GCMBaseIntentService{
public GCMIntentService(){
super(Constants.SenderId);
}
@Override
protected void onError(Context context, String regId) {
// TODO Auto-generated method stub
System.out.println("insideerroe");
Log.e("", "error registration id : "+regId);
}
@Override
protected void onMessage(Context context, Intent intent) {
// TODO Auto-generated method stub
}
@Override
protected void onRegistered(Context context, String regId) {
// TODO Auto-generated method stub
// Log.e(“”, “registration id : “+regId);
System.out.println("inside on registration");
handleRegistration(getApplicationContext(), regId);
}
@Override
protected void onUnregistered(Context context, String regId) {
// TODO Auto-generated method stub
}
private void handleRegistration(Context context, String regId) {
// TODO Auto-generated method stub
Log.e("", "registration id : "+regId);
}
}
和下面的代码:
public class Androidpushnotification extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GCMRegistrar.checkDevice(this);
GCMRegistrar.checkManifest(this);
String regId = GCMRegistrar.getRegistrationId(this);
if (regId.equals("")) {
GCMRegistrar.register(this, Constants.SenderId);
}
else {
Log.v("", "Already registered: "+regId);
}
}
@Override
protected void onDestroy() {
GCMRegistrar.onDestroy(this);
super.onDestroy();
}
}
清单文件:
<!-- GCM connects to Internet Services. -->
<uses-permission android:name="android.permission.INTERNET" />
<!-- GCM requires a Google account. -->
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<!-- Keeps the processor from sleeping when a message is received. -->
<uses-permission android:name="android.permission.WAKE_LOCK" />
<!-- Creates a custom permission so only this app can receive its messages. -->
<permission
android:name="com.example.androidpushnotification.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.example.androidpushnotification.permission.C2D_MESSAGE" />
<!-- This app has permission to register and receive data message. -->
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<!-- Network State Permissions to detect Internet status -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission
android:name="android.permission.READ_PHONE_STATE" >
</uses-permission>
<!-- Permission to vibrate -->
<uses-permission android:name="android.permission.VIBRATE" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".Androidpushnotification"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name="com.google.android.gcm.GCMBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<!-- Receives the actual messages. -->
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<!-- Receives the registration id. -->
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="com.example.androidpushnotification" />
</intent-filter>
</receiver>
<service android:name=".GCMIntentService" />
</application>
有人可以帮我解决这个问题吗,谢谢