Android gcmregistar.register不工作

Android gcmregistar.register不工作,android,push-notification,google-cloud-messaging,Android,Push Notification,Google Cloud Messaging,我正在尝试在我的应用程序上使用GCM,但gcmregistar.register始终返回空字符串。这是我的舱单: <?xml version="1.0" encoding="utf-8"?> 和意向服务: 公共类GCMinentService扩展了GCMBaseintService{ private static final String TAG = "GCMIntentService"; public GCMIntentService() { super(SENDER_

我正在尝试在我的应用程序上使用GCM,但gcmregistar.register始终返回空字符串。这是我的舱单:

<?xml version="1.0" encoding="utf-8"?>
和意向服务:

公共类GCMinentService扩展了GCMBaseintService{

private static final String TAG = "GCMIntentService";

public GCMIntentService() {
    super(SENDER_ID);
}

/**
 * Method called on device registered
 **/
@Override
protected void onRegistered(Context context, String registrationId) {
    Log.i(TAG, "Device registered: regId = " + registrationId);
    displayMessage(context, "Your device registred with GCM");
    // Log.d("NAME", MainActivity.name);
    ServerUtilities.register(context, "gcm aliko", "gcm aliko@a.com",
            registrationId);
}

/**
 * Method called on device un registred
 * */
@Override
protected void onUnregistered(Context context, String registrationId) {
    Log.i(TAG, "Device unregistered");
    displayMessage(context, getString(R.string.gcm_unregistered));
    ServerUtilities.unregister(context, registrationId);
}

/**
 * Method called on Receiving a new message
 * */
@Override
protected void onMessage(Context context, Intent intent) {
    Log.i(TAG, "Received message");
    String message = intent.getExtras().getString("price");

    displayMessage(context, message);
    // notifies user
    generateNotification(context, message);
}

/**
 * Method called on receiving a deleted message
 * */
@Override
protected void onDeletedMessages(Context context, int total) {
    Log.i(TAG, "Received deleted messages notification");
    String message = getString(R.string.gcm_deleted, total);
    displayMessage(context, message);
    // notifies user
    generateNotification(context, message);
}

/**
 * Method called on Error
 * */
@Override
public void onError(Context context, String errorId) {
    Log.i(TAG, "Received error: " + errorId);
    displayMessage(context, getString(R.string.gcm_error, errorId));
}

@Override
protected boolean onRecoverableError(Context context, String errorId) {
    // log message
    Log.i(TAG, "Received recoverable error: " + errorId);
    displayMessage(context,
            getString(R.string.gcm_recoverable_error, errorId));
    return super.onRecoverableError(context, errorId);
}

/**
 * Issues a notification to inform the user that server has sent a message.
 */

private static void generateNotification(Context context, String message) {
    int icon = R.drawable.ic_launcher;
    long when = System.currentTimeMillis();
    NotificationManager notificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
    Notification notification = new Notification(icon, message, when);

    String title = context.getString(R.string.app_name);

    Intent notificationIntent = new Intent(context, MainActivity.class);
    // set intent so it does not start a new activity
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
            | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent intent = PendingIntent.getActivity(context, 0,
            notificationIntent, 0);
    notification.setLatestEventInfo(context, title, message, intent);
    notification.flags |= Notification.FLAG_AUTO_CANCEL;

    // Play default notification sound
    notification.defaults |= Notification.DEFAULT_SOUND;

    // Vibrate if vibrate is enabled
    notification.defaults |= Notification.DEFAULT_VIBRATE;
    notificationManager.notify(0, notification);

}
}

这是我的日志:没有错误或警告

onReceive: com.google.android.c2dm.intent.REGISTRATION
GCM IntentService class: net.andromedya.GCMIntentService
Acquiring wakelock
您正在使用com.google.android.gcm.GCMBroadcastReceiver作为广播接收器。该广播接收器期望intent服务类位于应用程序的主包中,我假设它是net.andromedya,但您的intent服务实际上位于net.andromedya.activities中

您应该将GCMinentService类移动到net.andromedya或重写GCMBroadcastReceiver并指定GCMinentService类的位置

清单中的其他问题:

应该是

在您的广播接收机声明中:

应该是

请注意,如果您使用的是新版本的Android,GCM可能在没有这两个补丁的情况下工作,但在旧版本上无法工作。

您正在使用com.google.Android.GCM.gcmbroadcasreceiver作为广播接收器。该广播接收器期望intent服务类位于应用程序的主包中,我假设它是net.andromedya,但您的intent服务实际上位于net.andromedya.activities中

您应该将GCMinentService类移动到net.andromedya或重写GCMBroadcastReceiver并指定GCMinentService类的位置

清单中的其他问题:

应该是

在您的广播接收机声明中:

应该是


请注意,如果您使用的是新版本的Android,GCM可能在没有这两个补丁的情况下工作,但在旧版本上无法工作。

只需将GCMinentService和BroadcastReceiver类放在同一个包中即可。这对我有用

只需将gcminentservice和BroadcastReceiver类保持在同一个包中即可。这对我有用

看看这里。我的许多应用程序中的工作代码。。您应该发布其余的舱单,以及广播接收器和意向服务代码。@Eran i列出了您想要的代码。曼尼什,我今晚会查你的教程。看看这里。我的许多应用程序中的工作代码。。您应该发布其余的舱单,以及广播接收器和意向服务代码。@Eran i列出了您想要的代码。曼尼什,我今晚会看你的教程。是的,我终于连接到云了。谢谢@Eran:现在我自己也遇到了将它保存到php服务器的问题。我遇到了GCMRegistar.register有时无法与服务器通信的问题。那么我能做什么呢?@Kenji没有看到你的确切代码,我说不出问题出在哪里。你应该发布一个新问题。@Eran我发现这个问题是因为我的API代码用于演示应用程序和我的应用程序。它应该不一样。我有一个新的问题,接收器不会接收任何东西。这是boradcaster:static void displayMessageContext上下文,字符串消息{Intent Intent=new IntentDISPLAY_MESSAGE_ACTION;Intent.putextra_MESSAGE,MESSAGE;context.sendBroadcastintent;是的,我终于连接到了云。谢谢@Eran:现在我自己也遇到了将其保存到php服务器的问题。我遇到了gcmregistar.register有时无法与s通信的问题伺服器。那我该怎么办?@Kenji没有看到你的确切代码,我说不出问题出在哪里。你应该发布一个新问题。@Eran我发现问题出在我的API代码上,它用于演示应用程序和我的应用程序。它不应该是一样的。我有一个新问题,接收器不会收到任何东西。所以请帮助我。这是boradcaster:static void displayMessageContext,字符串消息{Intent Intent=new IntentDISPLAY_message_ACTION;Intent.putextra_message,message;context.sendBroadcastintent;
private static final String TAG = "GCMIntentService";

public GCMIntentService() {
    super(SENDER_ID);
}

/**
 * Method called on device registered
 **/
@Override
protected void onRegistered(Context context, String registrationId) {
    Log.i(TAG, "Device registered: regId = " + registrationId);
    displayMessage(context, "Your device registred with GCM");
    // Log.d("NAME", MainActivity.name);
    ServerUtilities.register(context, "gcm aliko", "gcm aliko@a.com",
            registrationId);
}

/**
 * Method called on device un registred
 * */
@Override
protected void onUnregistered(Context context, String registrationId) {
    Log.i(TAG, "Device unregistered");
    displayMessage(context, getString(R.string.gcm_unregistered));
    ServerUtilities.unregister(context, registrationId);
}

/**
 * Method called on Receiving a new message
 * */
@Override
protected void onMessage(Context context, Intent intent) {
    Log.i(TAG, "Received message");
    String message = intent.getExtras().getString("price");

    displayMessage(context, message);
    // notifies user
    generateNotification(context, message);
}

/**
 * Method called on receiving a deleted message
 * */
@Override
protected void onDeletedMessages(Context context, int total) {
    Log.i(TAG, "Received deleted messages notification");
    String message = getString(R.string.gcm_deleted, total);
    displayMessage(context, message);
    // notifies user
    generateNotification(context, message);
}

/**
 * Method called on Error
 * */
@Override
public void onError(Context context, String errorId) {
    Log.i(TAG, "Received error: " + errorId);
    displayMessage(context, getString(R.string.gcm_error, errorId));
}

@Override
protected boolean onRecoverableError(Context context, String errorId) {
    // log message
    Log.i(TAG, "Received recoverable error: " + errorId);
    displayMessage(context,
            getString(R.string.gcm_recoverable_error, errorId));
    return super.onRecoverableError(context, errorId);
}

/**
 * Issues a notification to inform the user that server has sent a message.
 */

private static void generateNotification(Context context, String message) {
    int icon = R.drawable.ic_launcher;
    long when = System.currentTimeMillis();
    NotificationManager notificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
    Notification notification = new Notification(icon, message, when);

    String title = context.getString(R.string.app_name);

    Intent notificationIntent = new Intent(context, MainActivity.class);
    // set intent so it does not start a new activity
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
            | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent intent = PendingIntent.getActivity(context, 0,
            notificationIntent, 0);
    notification.setLatestEventInfo(context, title, message, intent);
    notification.flags |= Notification.FLAG_AUTO_CANCEL;

    // Play default notification sound
    notification.defaults |= Notification.DEFAULT_SOUND;

    // Vibrate if vibrate is enabled
    notification.defaults |= Notification.DEFAULT_VIBRATE;
    notificationManager.notify(0, notification);

}
onReceive: com.google.android.c2dm.intent.REGISTRATION
GCM IntentService class: net.andromedya.GCMIntentService
Acquiring wakelock