我正在我的应用程序中实现GCM,我正在成功注册设备ID,但是当我从服务器发送它时,我没有收到任何通知。 以下是我的代码的一部分: 清单
<receiver
android:name=".services.GcmBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE"/>
<category android:name="com.test.gcm"/>
</intent-filter>
</receiver>
<service android:name=".services.GcmIntentService"/>
内部包服务GcmBroadcastReceiver:
public class GcmBroadcastReceiver extends WakefulBroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
ComponentName component = new ComponentName(context.getPackageName(),
GcmIntentService.class.getName());
startWakefulService(context, (intent.setComponent(component)));
setResultCode(Activity.RESULT_OK);
}
}
最后是gcmIntentService
public class GcmIntentService extends IntentService {
public static final int NOTIFICATION_ID = 1;
private NotificationManager mNotificationManager;
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()) {
sendNotification("Notification " + extras.toString());
}
GcmBroadcastReceiver.completeWakefulIntent(intent);
}
private void sendNotification(String message) {
mNotificationManager = (NotificationManager)
this.getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, MainActivity.class), 0);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setContentTitle("Notification")
.setContentText(message)
.setStyle(new NotificationCompat.BigTextStyle())
.setAutoCancel(true);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}
}
我不知道可能出现什么问题,甚至不知道如何调试它。 谢谢你的帮助
答案 0 :(得分:0)
当谈到GCM时,服务和接收者名称非常特别。 接收者姓名应为
<receiver
android:name="com.google.android.gcm.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="com.packagename" />
</intent-filter>
</receiver>
和服务应该是
<service android:name=".GCMIntentService" />
此外,GCMIntentService应与启动器类位于同一个包中。
答案 1 :(得分:0)
问题是尝试启动服务的方法:
public class GcmBroadcastReceiver extends WakefulBroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
ComponentName component = new ComponentName(context.getPackageName(),
GcmIntentService.class.getName());
startWakefulService(context, (intent.setComponent(component)));
setResultCode(Activity.RESULT_OK);
}
}
您使用ComponentName
初始化context.getPackageName()
这是您应用的主要包(com.test.gcm
),但您的GcmIntentService
类位于不同的包中({{ 1}})。
您可以指定正确的包名称:
com.test.gcm.services
或者您可以以不同的方式启动服务:
ComponentName component = new ComponentName(
GcmIntentService.class.getPackage ().getName (),
GcmIntentService.class.getName());