我正在开发我的第一个 Android 应用程序。我进行了彻底的搜索,但仍然无法找到解决方案,并且在终点线之前我不再有太多时间了。
我的应用可以注册到 GCM 服务器。我的服务器还没有准备好,但我确实测试了消息发送(服务器->设备),通过在线服务将它们投掷(注意,GCM 在这些发送时返回成功)。问题是 GooglePlay 服务似乎没有消息到达我的接收器。
从 Android 开发人员指南开始,我设置了这些代码:
AndroidManifest.xml
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="it.uniroma1.informatica.didapp.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="it.uniroma1.informatica.didapp.permission.C2D_MESSAGE" />
<receiver
android:name="it.uniroma1.informatica.didapp.remote.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" />
<category android:name="it.uniroma1.informatica.didapp" />
</intent-filter>
</receiver>
<service android:name="it.uniroma1.informatica.didapp.remote.GCMIntentService" />
GCMBroadcastReceiver.java
public class GCMBroadcastReceiver 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);
}
}
最后是 GCMIntentService.java
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);
String messageType = gcm.getMessageType(intent);
if (!extras.isEmpty()) {
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());
} else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType)) {
sendNotification("Message: " + extras.toString());
}
}
GCMBroadcastReceiver.completeWakefulIntent(intent);
}
private void sendNotification(String msg) {
mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, Start.class), 0);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.setContentTitle("GCM Notification")
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(msg))
.setContentText(msg);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}
}
我所需要的很简单:
- 服务器向 GCM 发送通知(无负载)
- GCM 将其转发到设备
- 设备在系统栏中显示通知并最终启动活动