7

Google Cloud Messaging for Android を使用して gcm からメッセージを受信するための実際の例を手伝ってくれる人はいますか? 両方の方法 (ヘルパー ライブラリと GoogleCloudMessaging クラス) を試しましたが、何も機能していないようです。以下を示す PHP スクリプトを使用しています。

マルチキャスト ID: 5.2108110103215E+18 正常に処理されたメッセージの数: 1 処理エラーのあるメッセージの数: 0 正規 ID: 0

どうやらすべてがOKです。ヘルパー ライブラリ (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

「com.example.gcm」を「com.yourdomain.yourapp」などのパッケージ名に変更することを忘れないでください

于 2013-12-17T10:26:33.293 に答える