1

我已使用我的应用程序 ID 和浏览器密钥从服务器端成功发送 GCM 消息,但它没有在我的广播接收器中接收,我发现它已被我的设备接收但在 Log-cat 中显示一些错误。我的错误在哪里?这个问题的解决方案是什么?

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="10" />

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.VIBRATE" />
<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="in.myapp.tab.C2D_MESSAGE" />

<permission
    android:name="in.myapp.tab.permission.C2D_MESSAGE"
    android:protectionLevel="signature" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/Theme.Example" >
    <activity
        android:name="in.myapp.tab.MainActivity"
        android:configChanges="orientation"
        android:label="@string/app_name"
        android:screenOrientation="portrait" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name="in.myapp.tab.SecondPage" >
    </activity>


    <receiver
        android:name="in.myapp.tab.PullReqReceiver"
        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" /> -->
            <!-- <action android:name="android.net.conn.CONNECTIVITY_CHANGE" /> -->
            <!-- <action android:name="android.net.wifi.WIFI_STATE_CHANGED" /> -->
        </intent-filter>
    </receiver>

    <service android:name=".WebService" />

    <activity
        android:name="com.facebook.LoginActivity"
        android:configChanges="orientation|screenSize|keyboardHidden" />
    <activity
        android:name="in.myapp.tab.LoginActivity"
        android:configChanges="orientation|screenSize|keyboardHidden" >
        <meta-data
            android:name="com.facebook.sdk.ApplicationId"
            android:value="@string/app_id" />
    </activity>

    <meta-data
        android:name="com.facebook.sdk.ApplicationId"
        android:value="@string/app_id" />
</application>

Logcat 输出是

09-17 18:04:31.329: W/GTalkService(1748): <!>com.google.android.gsf.gtalkservice.DataMessageManager$BroadcastDoneReceiver 100<!> [DataMsgMgr] broadcast intent callback: result=CANCELLED forIntent { act=com.google.android.c2dm.intent.RECEIVE cat=[in.myapp.tab] (has extras) }
09-17 18:04:31.329: W/GTalkService(1748): <!>com.google.android.gsf.gtalkservice.DataMessageManager 334<!> Receiver package not found, unregister application in.myapp.tab sender 
09-17 18:04:31.379: W/GCM(1748): <!>com.google.android.gms.gcm.GcmProvisioning 182<!> DIR: /data/data/com.google.android.gms/app_APP /data/data/com.google.android.gsf
09-17 18:04:33.669: D/GCM(1748): <!>bek 940<!> [C2DMRegistrar.159] Send register result  null 0 0

我的服务器端代码是 PHP

 <?php

// Replace with real BROWSER API key from Google APIs
$apiKey = "My BROWSER API key";
$message = "x";
// Set POST variables
$url = 'https://android.googleapis.com/gcm/send';
$headers = array(
'Authorization: key=' . $apiKey,
'Content-Type: application/json'
);

$registrationIDs = array();   
array_push($registrationIDs, $_GET['id']);
  $fields = array(
    'registration_ids' => $registrationIDs,
    'data' => array("message" => $message),
);
   $ch = curl_init();

// Set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);

// Execute post
$result = curl_exec($ch);
echo 'Curl error: ' . curl_error($ch);
// Close connection
curl_close($ch);
echo  $result;

?>>
4

1 回答 1

2

我认为您在进入接收器时错过了这个意图过滤器。

    <action android:name="com.google.android.c2dm.intent.REGISTRATION" />

            <category android:name="in.myapp.tab" />

并且请检查您的发件人 ID 和 GCM 密钥。

于 2013-09-17T12:57:03.607 回答