7

有没有人可以帮助我提供一个使用 Google Cloud Messaging for Android 从 gcm 接收消息的工作示例。我已经尝试了两种方法(帮助库和 GoogleCloudMessaging 类),但似乎没有任何效果。我正在使用显示以下内容的 PHP 脚本:

多播 ID:5.2108110103215E+18 成功处理的消息数:1 处理错误的消息数:0 规范 ID:0

所以显然一切都很好。我可以通过两种方式注册设备,使用帮助程序库 (gcm.jar) 和使用 GoogleCloudMessaging 类。问题是我通过 PHP 发送的消息无法到达,或者至少我不知道如何正确处理它。这是我清单中的权限和接收者:

<permission android:name="com.example.gcm.permission.C2D_MESSAGE" 
    android:protectionLevel="signature" />
<uses-permission android:name="com.example.gcm.permission.C2D_MESSAGE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />

<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="android.permission.USE_CREDENTIALS" />
<uses-permission android:name="android.permission.READ_OWNER_DATA" />

<receiver
        android:name="com.google.android.gcm.GCMBroadcastReceiver"
        android:permission="com.google.android.c2dm.permission.SEND" >
        <intent-filter>

            <!-- Receives the actual messages. -->
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            <!-- Receives the registration id. -->
            <action android:name="com.google.android.c2dm.intent.REGISTRATION" />

            <category android:name="com.example.gcm" />
        </intent-filter>
    </receiver>
<service android:name=".GCMIntentService" />

最后是服务类

public class GCMIntentService extends GCMBaseIntentService {

private static final String PROJECT_ID = "49XXXXXXXX6";    
private static final String TAG = "GCM Intent Service";

public GCMIntentService()
{
    super(PROJECT_ID);
    Log.d(TAG, "GCMIntentService init");
}


@Override
protected void onError(Context ctx, String sError) {

    Log.d(TAG, "Error: " + sError);     
}

@Override
protected void onMessage(Context ctx, Intent intent) {

    Log.d(TAG, "Message Received");

    String message = intent.getStringExtra("message");

    sendGCMIntent(ctx, message);

}


private void sendGCMIntent(Context ctx, String message) {

    Intent broadcastIntent = new Intent();
    broadcastIntent.setAction("GCM_RECEIVED_ACTION");

    broadcastIntent.putExtra("gcm", message);

    ctx.sendBroadcast(broadcastIntent);

}

@Override
protected void onRegistered(Context ctx, String regId) {

    Log.d(TAG, regId);

    // Notify main UI to update registration status
    Intent registrationIntent = new Intent();
    registrationIntent.setAction("registered");
    registrationIntent.putExtra("regId", regId);
    sendBroadcast(registrationIntent);      
}

@Override
protected void onUnregistered(Context ctx, String regId) {
    //...

}
}

这是使用 GoogleCloudMessaging 类时的代码(我将清单更改为使用自定义接收器):

public class GCMBroadcastReceiver extends BroadcastReceiver {

private static final String TAG = "GCM Receiver";
public static final int NOTIFICATION_ID = 1;
private NotificationManager mNotificationManager;
private Context ctx;

@Override
public void onReceive(Context context, Intent intent) {

    Log.d(TAG, "Message received");

    GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(context);
    ctx = context;
    String messageType = gcm.getMessageType(intent);
    if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR.equals(messageType)) {
        sendNotification("Send error: " + intent.getExtras().toString());
    }
    else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED
            .equals(messageType)) {
        sendNotification("Deleted messages on server: "
                + intent.getExtras().toString());
    }
    else {
        sendNotification("Received: " + intent.getExtras().toString());
    }
    setResultCode(Activity.RESULT_OK);
}

// Put the GCM message into a notification and post it.
private void sendNotification(String msg) {
    mNotificationManager = (NotificationManager) ctx
            .getSystemService(Context.NOTIFICATION_SERVICE);

    PendingIntent contentIntent = PendingIntent.getActivity(ctx, 0,
            new Intent(ctx, MainActivity.class), 0);

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
            ctx).setSmallIcon(R.drawable.ic_launcher_temp)
            .setContentTitle("GCM Notification")
            .setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
            .setContentText(msg);

    mBuilder.setContentIntent(contentIntent);
    mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}

}

问题是一切似乎都很好,但消息永远不会到达。有任何想法吗??提前致谢。

4

5 回答 5

7

在您的清单中添加以下内容

 <permission
    android:name="PACKAGE_NAME.permission.C2D_MESSAGE"
    android:protectionLevel="signature" />

<uses-permission android:name="PACKAGE_NAME.permission.C2D_MESSAGE" />

<!-- App receives GCM messages. -->
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
于 2013-07-01T04:11:27.610 回答
2

您的清单缺少一些权限:

<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />

<permission android:name="com.example.gcm.permission.C2D_MESSAGE" 
    android:protectionLevel="signature" />
<uses-permission android:name="com.example.gcm.permission.C2D_MESSAGE" />

编辑 :

在您的清单中,您正在使用com.google.android.gcm.GCMBroadcastReceiver. 这是旧帮助程序库 ( gcm.jar) 中的一个类,它启动一个意图服务。如果您想使用帮助程序库,您应该在清单中定义意图服务。

如果您不想使用帮助程序库,则应GCMBroadcastReceiver将清单中的包更改为GCMBroadcastReceiver您问题中包含的类的包。否则,将不会使用该类。

于 2013-07-01T04:04:07.927 回答
1

更正上述问题后,仍然没有推送消息。问题是 403 错误。看起来该服务在古巴不可用,因此解决方案是使用“你的自由”或“雷神”等工具。

于 2014-04-18T01:01:58.270 回答
0

您正在使用 Google 已弃用的 GCMBaseIntentService。(但它应该仍然有效。)我按照最新的教程http://developer.android.com/google/gcm/client.html并确实收到了消息。你也可以试试。

另一个可能的原因是您可能收到了一条消息,但它没有成功显示在通知中。在方法中添加这一行:sendNotification(String msg)

    Log.i(TAG, msg);
于 2013-12-19T02:28:21.447 回答
0

不要忘记将“com.example.gcm”更改为您的包名,例如“com.yourdomain.yourapp”

于 2013-12-17T10:26:33.293 回答