0

I am following http://www.androidhive.info/2016/02/android-push-notifications-using-gcm-php-mysql-realtime-chat-app-part-1/ tutorial to implement GCM into my android application.

I wrote a REST url post api using php and slim, which send android in-app post message to a group and after inserting into MYSQL, send a GCM push notification message to everyone registered to that group/topic.

Funny thing is If I hit the post request using chrome extension Advanced Rest Client, every device registered to that particular group get the gcm notification.... however if someone try to hit that post rest address using volley library inside app, expecting that everyone registered to that group will get a gcm notification.

But nothing happens this time. I checked log of onMessageReceived and seems that device doesn't even reveive a push notification. Server send the GCM message with response 200 and message_id.

Below are the relevant codes.

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 extends 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);

Server side index.php Post method code

$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 code

// 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;
}

Have been struggling on this problem for past 3 days, did everything from various answers related to this on stackoverflow, nothing seems to work... Please don't suggest using FCM because I feel the problem is something else entirely as I can get notifications while using the same server side post method individually using chrome extension.

Tried to open all the ports required by GCM still no success, used a different approach apart from curl in reference to this answer

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

Still the same problem, maybe I am missing a small config or something. :(

Community
  • 1
  • 1
vaibhavbarmy
  • 309
  • 1
  • 4
  • 11
  • Please also try to get **HTTP Authentication** using volley library. If you haven't done so, you may check the implementation on how to request a token in this [GitHub post](https://github.com/smaspe/VolleyTwitter/blob/master/src/com/njzk2/twitterbrowser/TokenRequest.java) or from this [SO post](http://stackoverflow.com/questions/16817980/how-does-one-use-basic-authentication-with-volley-on-android). Hope this helps! – Teyam Oct 13 '16 at 14:30
  • I am not making a post request to gcm server using volley, I am just using a post request of volley to add a php curl request to gcm server. Will HTTP authentication required in this case? – vaibhavbarmy Oct 13 '16 at 20:57

0 Answers0