【问题标题】:Android GCM message successfully sent but not receivedAndroid GCM 消息成功发送但未收到
【发布时间】:2013-08-28 03:42:15
【问题描述】:

我正在尝试使用 GCM 服务向我的设备发送推送通知。

对于服务器端功能,我已遵循 Android Hive tutorial(现在已像 many other questions and answers 一样弃用),因为我可以获得这种输出,所以看起来可以按预期工作:

{"multicast_id":9131068334342174816,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1377441098827443%1d84a26ff9fd7ecd"}]}

但是根据some answers的说法,收到这个响应只是表示消息已经被GCM服务器接受发送,而不是已经发送。所以,正如预期的那样,我的BroadcastReceiver 没有收到任何东西。

这是我的BroadcastReceiver 代码:

public class GcmBroadcastReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.i("gcm_debug", "PushReceiver onReceive called");

        Bundle extras = intent.getExtras();
        GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(context);

        String msgType = gcm.getMessageType(intent);

        if(!extras.isEmpty()){
            if(GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR.equals(msgType)){
                Log.i("gcm_debug", "Message send error");
            }else if(GoogleCloudMessaging.MESSAGE_TYPE_DELETED.equals(msgType)){
                Log.i("gcm_debug", "Message deleted");
            }else if(GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(msgType)){
                Log.i("gcm_debug", "Message received : " + extras.toString());
            }
        }

        setResultCode(Activity.RESULT_OK);
    }

}

还有我的AndroidManifest

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />

<permission android:name="<MYAPP>.permission.C2D_MESSAGE"
            android:protectionLevel="signature" />
<uses-permission android:name="<MYAPP>.permission.C2D_MESSAGE" />

<!-- Require OpenGL ES2 for GMap -->
<uses-feature android:glEsVersion="0x00020000"
              android:required="true" />


<!-- Because this app is using the GCM library to send messages, the min SDK cannot be lower
than 8 -->
<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="18" />


<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/LovRTheme" >
    <receiver
        android:name=".GcmBroadcastReceiver"
        android:permission="com.google.android.gcm.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="<MYAPP>" />
        </intent-filter>
    </receiver>
    <service android:name=".GcmIntentService" />

还有服务器端代码:

    $url = "https://android.googleapis.com/gcm/send";
    $fields = array(
        'registration_ids' => array($dstRegId),
        'delay_while_idle' => true,
        'data' => array("message" => $message));

    $headers = array(
        'Authorization: key=' . $google_api_key,
        'Content-Type: application/json');

    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));

    $result = curl_exec($ch);
    if($result === FALSE){
        die("Curl failed");
    }
    curl_close($ch);

    echo $result;

我在家里编码,所以我假设 GCM 进出网络不会有问题,因为 GCM 使用一种反向 shell 连接。无论如何,我没有收到任何我期望的日志输出。

编辑: 在让我的应用程序运行一段时间后,LogCat 向我发送了以下输出:

08-26 00:00:48.984  17966-17966/? D/GCM: Ignoring attempt to send heartbeat on dead connection.
08-26 00:01:49.000  17966-17966/? D/GCM: Ignoring attempt to send heartbeat on dead connection.
08-26 00:02:01.672      433-448/? W/BroadcastQueue: Permission Denial: broadcasting Intent { act=com.google.android.c2dm.intent.RECEIVE flg=0x10 pkg=<myApp> (has extras) } from com.google.android.gsf (pid=17966, uid=10003) requires com.google.android.gcm.c2dm.permission.SEND due to receiver com.navissal.lovr/com.navissal.lovr.GcmBroadcastReceiver
08-26 00:17:09.063  17966-17966/? D/GCM: Ignoring attempt to send heartbeat on dead connection.
08-26 00:18:09.086  17966-17966/? D/GCM: Ignoring attempt to send heartbeat on dead connection.
08-26 00:24:15.250    3936-3951/? V/meshclient: uri: [/beacon?e=wTkyFfdWFtlzOme7rNizVCtnUu9t3SbJgKCe72og-OhyhGIYab1QikytBeU2Nc02QwdDtqI4sX7HHU6GpDQL8zZdKXFXmCics6ZG-Jmr84yvMX1x9EqdyyW1UI6PEhoOb9MV4R_msQ_MEWFnwUzQrV-vGTycKtSNKMMIRE-zM1caIe__7hdu_UStbhz0dhl7cAFmHYF74IQI6EYFCEORxgV2Wts4Ls-hRWEKfEzuBhViND7TeCVpqUOhdVwGMnDO_Qwlo0rpuSNFegdfJCYY4L8fZJogPsXdQW2cFSZ2S0kujCH9uIrljFUTSGcM5GCFuJRq8vWiTP07MqpMq7h2fGGpXQvjImFcVP81erkVzvlYu3bNpjyQe6MhxSFEZrG37Kdp2Fd3liXFZzSjKlLILeYsDK9rRYqO8fEy3PH07et1HjqYWrH-v7wnjcQm6TtyZu914oR-dPBAwX9D3WvbdQlPlqxnuwJ1huUTwaiKotPLgrAzo3Mc5vI93VTs3row]

【问题讨论】:

  • 服务器代码与您的问题无关,因为您收到了 Google 的成功回复。你能包括你的客户代码吗?
  • 客户端代码仅用于接收,因为它是一个跨系统应用程序,服务器处理所有发送。你需要哪部分代码?
  • 广播接收器和意图服务。
  • 我只使用广播接收器,因为我觉得不需要使用意图服务,并且已经添加了广播接收器代码。我应该有一个吗?
  • "...现在已弃用..." 与大多数 GCM 示例一样。谷歌最近添加了一个带有示例的新 github 存储库:github.com/google/gcm

标签: php android push-notification google-cloud-messaging


【解决方案1】:

这是你的错误:

<receiver
    android:name=".GcmBroadcastReceiver"
    android:permission="com.google.android.gcm.c2dm.permission.SEND" >

应该是:

<receiver
    android:name=".GcmBroadcastReceiver"
    android:permission="com.google.android.c2dm.permission.SEND" >

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-12-29
    • 1970-01-01
    • 1970-01-01
    • 2012-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多