使用volley post请求从任何Android设备推送主题时未收到GCM通知消息

时间:2016-10-12 22:15:08

标签: php android rest google-cloud-messaging slim

我正在关注http://www.androidhive.info/2016/02/android-push-notifications-using-gcm-php-mysql-realtime-chat-app-part-1/教程,将GCM实现到我的Android应用程序中。

我使用php和slim编写了一个REST url post api,它将android in-app post消息发送到一个组,并在插入MYSQL后,向注册到该组/主题的每个人发送GCM推送通知消息。

有趣的是,如果我使用Chrome扩展程序Advanced Rest Client点击帖子请求,则注册到该特定组的每个设备都会获得gcm通知....但是,如果有人尝试使用内部排球库来点击该帖子的休息地址应用程序, 期望注册到该组的每个人都会收到gcm通知。

但这次没有任何事情发生。我检查了onMessageReceived的日志,看起来该设备甚至没有发现推送通知。服务器发送带有响应200和message_id的GCM消息。

以下是相关代码。

  

的AndroidManifest.xml

<!-- START Added for GCM -->
<uses-permission android:name="android.permission.WAKE_LOCK" />
<permission
    android:name="<my_package>.permission.C2D_MESSAGE"
    android:protectionLevel="signature" />

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

<service
        android:name=".Api.GCM.MyGcmPushReceiver"
        android:exported="false">
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
        </intent-filter>
    </service>

    <!--<service-->
        <!--android:name=".Api.GCM.GcmIntentService"-->
        <!--android:exported="false">-->
        <!--<intent-filter>-->
            <!--<action android:name="com.google.android.gms.iid.InstanceID" />-->
        <!--</intent-filter>-->
    <!--</service>-->

    <service
        android:name=".Api.GCM.GcmIntentService"
        android:exported="false">
    </service>

    <service
        android:name="<my_package>.Api.GCM.MyInstanceIDListenerService"
        android:exported="false">
        <intent-filter>
            <action android:name="com.google.android.gms.iid.InstanceID" />
        </intent-filter>
    </service>

    <!-- START Added for GCM -->
    <receiver
        android:name="com.google.android.gms.gcm.GcmReceiver"
        android:exported="true"
        android:permission="com.google.android.c2dm.permission.SEND"
        android:authorities="<my_package>">
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />

            <category android:name="<my_package>" />
        </intent-filter
</receiver>
  

MyGcmPushReceiver扩展了GcmListenerService {

@Override
public void onMessageReceived(String from, Bundle bundle) {
    String title = bundle.getString("title");
    Boolean isBackground = Boolean.valueOf(bundle.getString("is_background"));
    String flag = bundle.getString("flag");
    String data = bundle.getString("data");
    Log.d(TAG, "From: " + from);
    Log.d(TAG, "title: " + title);
    Log.d(TAG, "isBackground: " + isBackground);
    Log.d(TAG, "flag: " + flag);
    Log.d(TAG, "data: " + data);
  

服务器端index.php发布方法代码

$app->post('/xxxxxx/:id/post', function(


--------- portion to insert my feed post to mysql--------


if ($response['error'] == false) {
    require_once __DIR__ . '/../libs/gcm/gcm.php';
    require_once __DIR__ . '/../libs/gcm/push.php';
    $gcm = new GCM();
    $push = new Push();

    $msg = array();
    $msg['message'] = $Title;
    $msg['message_id'] = <some id>;
    $msg['created_at'] = date('Y-m-d G:i:s');

    $data = array();
    $data['message'] = $msg;
    $data['chat_room_id'] = <some id>;
    $data['image'] = '';

    $push->setTitle("New Feed Post in ".<group name for topic>);
    $push->setIsBackground(FALSE);
    $push->setFlag(<some flag to recognise notification schema>);
    $push->setData($data);

    $responseGCM = $gcm->sendToTopic('topic_' . <id for that topic group>, $push->getPush());

    $response['gcmdata'] = $data;
    $response['error'] = false;
    $response['gcm_result'] = $responseGCM;
}
  

gcm.php代码

// Sending message to a group using topics method
public function sendToTopic($to, $message) {
    $random_collapse = rand(11, 100);
    $fields = array(
        'to' => '/topics/' . $to,
        'collapse_key' => "{$random_collapse}",
        'data' => $message,
    );
    return $this->sendPushNotification($fields);
}
// function makes curl request to gcm servers
private function sendPushNotification($fields) {

    // include config
    include_once __DIR__ . '/../../include/config.php';

    // Set POST variables
    $url = 'https://gcm-http.googleapis.com/gcm/send';
    $headers = array(
        'Authorization: key=' . GOOGLE_API_KEY,
        'Content-Type: application/json'
    );
    // Open connection
    $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_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
    curl_setopt($ch, CURLOPT_VERBOSE, true);
    curl_setopt($ch, CURLOPT_HEADER, true);
    curl_setopt($ch, CURLINFO_HEADER_OUT, true);

    // Disabling SSL Certificate support temporarly
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

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

    // Execute post
    $result = curl_exec($ch);
    $result_header = curl_getinfo($ch);

    $response = array();
    $response['result'] = $result;
    $response['header'] = $result_header;

    if ($result === FALSE) {
        die('Curl failed: ' . curl_error($ch));
    }

    // Close connection
    curl_close($ch);

    return $response;
}

过去3天一直在努力解决这个问题,在stackoverflow上做了各种与此相关的答案,似乎没什么用...... 请不要建议使用FCM,因为我觉得问题完全是另外一个问题,因为我可以在使用Chrome扩展程序单独使用相同的服务器端邮政方法时收到通知。

尝试打开GCM所需的所有端口仍然没有成功,使用过 除了参考这个答案的卷曲之外,还有一种不同的方法

How to send a gcm message in php without using cURL?

仍然是同样的问题,也许我错过了一个小配置或什么。 :(

0 个答案:

没有答案