0
我已按照this文檔進行操作,以便能夠接收GCM通知。 這裏是我的清單:接收Android通知
<uses-sdk
android:minSdkVersion="12"
android:targetSdkVersion="18" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<uses-feature android:name="android.hardware.camera" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<permission
android:name="com.example.gcm.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.example.gcm.permission.C2D_MESSAGE" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="@string/google_android_api_key" />
<activity
android:name="com.evapp.activities.SplashActivity"
android:label="@string/title_activity_splash" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.evapp.activities.LoginActivity"
android:label="@string/title_activity_login" >
</activity>
<activity
android:name="com.evapp.activities.RegistrationActivity"
android:label="@string/title_activity_signup"
android:parentActivityName="com.evapp.activities.LoginActivity" >
<!-- Parent activity meta-data to support 4.0 and lower -->
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.evapp.activities.LoginActivity" />
</activity>
<activity
android:name="com.evapp.activities.HomeActivity"
android:label="@string/title_activity_home" >
</activity>
<activity
android:name="com.evapp.activities.UserActivity"
android:label="@string/title_activity_user" >
</activity>
<activity
android:name="com.evapp.activities.TestActivity"
android:label="@string/title_activity_test" >
</activity>
<receiver
android:name="com.evapp.notification.Test"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="com.evapp.notification" />
</intent-filter>
</receiver>
<service android:name="com.evapp.activities.GcmIntentService" />
</application>
這裏是WakefulBroadcastReceiver
:
public class Test extends WakefulBroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
Intent gcmIntent = new Intent(context, GcmIntentService.class);
gcmIntent.putExtras(intent.getExtras());
startWakefulService(context, gcmIntent);
setResultCode(Activity.RESULT_OK);
}
}
而且IntentService
:
public class GcmIntentService extends IntentService
{
public static final int NOTIFICATION_ID = 1;
private NotificationManager mNotificationManager;
NotificationCompat.Builder builder;
public GcmIntentService()
{
super("GcmIntentService");
}
@Override
protected void onHandleIntent(Intent intent)
{
Bundle extras = intent.getExtras();
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
// The getMessageType() intent parameter must be the intent you received
// in your BroadcastReceiver.
String messageType = gcm.getMessageType(intent);
if (!extras.isEmpty())
{ // has effect of unparcelling Bundle
/*
* Filter messages based on message type. Since it is likely that
* GCM will be extended in the future with new message types, just
* ignore any message types you're not interested in, or that you
* don't recognize.
*/
if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR
.equals(messageType))
{
sendNotification("Send error: " + extras.toString());
}
else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED
.equals(messageType))
{
sendNotification("Deleted messages on server: "
+ extras.toString());
// If it's a regular GCM message, do some work.
}
else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE
.equals(messageType))
{
// This loop represents the service doing some work.
for (int i = 0; i < 5; i++)
{
try
{
Thread.sleep(5000);
}
catch (InterruptedException e)
{
}
}
// Post notification of received message.
sendNotification("Received: " + extras.toString());
}
}
// Release the wake lock provided by the WakefulBroadcastReceiver.
Test.completeWakefulIntent(intent);
}
// Put the message into a notification and post it.
// This is just one simple example of what you might choose to do with
// a GCM message.
private void sendNotification(String msg)
{
mNotificationManager = (NotificationManager) this
.getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, HomeActivity.class), 0);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
this).setSmallIcon(R.drawable.calendar)
.setContentTitle("GCM Notification")
.setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
.setContentText(msg);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}
}
從我的服務器端我得到這樣的響應:
{"multicast_id":7269872883857375111,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1394655370108962%e210e99800364492"}]}
但調試我從來沒有漸入GcmIntentService
時,調試器進入了Test
類並沒有任何反應。
我缺少什麼?看來startWakefulService
不開放GcmIntentService
。
注: 我修改了代碼@Eran建議(仍然沒有工作:()
還是沒有,我應該改變清單嗎?這裏是'gcmIntent'(tosString())'Intent {cmp = com.evapp.activities/com.evapp.notification.GcmIntentService(有額外)}的值}' – vlio20
@VladIoffe如果你發佈完整的清單,我會能夠回答這個問題。 – Eran
我已經添加了完整的清單。 – vlio20