我正在开发我的第一个Android应用。我经常搜索但仍无法找到解决方案,而且我终于在终点线之前没有多少时间。
我的应用可以注册到GCM服务器。我的服务器还没有准备好,但我测试了消息发送(服务器 - >设备)通过在线服务投掷它们(注意,GCM在这些发送时返回成功)。问题是似乎没有消息从GooglePlay服务到达我的接收器。
从Android Developer指南开始,我设置了这些代码:
的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());
}
}
我所需要的只是非常简单:
答案 0 :(得分:0)
我认为这可能是问题所在:
ComponentName comp = new ComponentName(context.getPackageName(), GCMIntentService.class.getName());
context.getPackageName()
会返回it.uniroma1.informatica.didapp
,但您的意向服务位于it.uniroma1.informatica.didapp.remote
。尝试将其移至应用程序的主程序包。
public ComponentName(String pkg,String cls)
在API级别1中添加
创建新的组件标识符。
参数
pkg 组件所在的包的名称。不能为空。
cls 实现组件的pkg内部类的名称。不能为空。