问题
Hello I want to make a simple notification application. I am using this (http://www.youtube.com/watch?v=rmzv716SYkQ) tutorial. here is my codes.
manifest file
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS"/>
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<permission
android:name="com.myapp.ntfapp.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.turk.bakistik.permission.C2D_MESSAGE" />
<uses-permission android:name="android.permission.WAKE_LOCK"/>
manifest file in application block
<receiver
android:name=".MyNotificationService"
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" />
<category android:name="com.myapp.ntfapp" />
</intent-filter>
</receiver>
MyNotificationService Class
public class MyNotificationService extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
try {
String action = intent.getAction();
if (action.equals("com.google.android.c2dm.intent.REGISTRATION")) {
String registrationId = intent.getStringExtra("registration_id");
Log.i("ui", 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 { }
}
}
Activate and Deactivate buttons in main activity
button2=(Button)findViewById(R.id.button2);
button3=(Button)findViewById(R.id.button3);
button2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent registrationIntent = new Intent("com.google.android.c2dm.intent.REGISTER");
registrationIntent.putExtra("app", PendingIntent.getBroadcast(v.getContext(), 0, new Intent(), 0));
registrationIntent.putExtra("sender", "123456789101112");
startService(registrationIntent);
}
});
button3.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent unregIntent = new Intent("com.google.android.c2dm.intent.UNREGISTER");
unregIntent.putExtra("app", PendingIntent.getBroadcast(v.getContext(), 0, new Intent(), 0));
startService(unregIntent);
}
});
When I click activate button
W/ActivityManager(70): Unable to start service Intent { act=com.google.android.c2dm.intent.REGISTRATION (has extras) }: not found
How can I fix this problem?
回答1:
I see an inconsistency in your manifest :
<permission
android:name="com.myapp.ntfapp.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.turk.bakistik.permission.C2D_MESSAGE" />
You should use your app's package name in both (either com.myapp.ntfapp
or com.turk.bakistik
).
In addition, you are using an old and obsolete way to register to GCM. You should use the GoogleCloudMessaging.register method which is part of the Google Play Services library.
来源:https://stackoverflow.com/questions/25484020/android-gcm-unable-to-start-service-intent