5

I'm trying to wake my phone or get the light blinking using GCM. I'm getting the messages just fine but theres no difference in setting a high priority or none at all. I'm using a razr maxx hd to test. is there anything I'm missing here?

<?php
// API access key from Google API's Console
define('API_ACCESS_KEY', 'blee');

// prep the bundle
$msg = array
(
    'body' => 'this is my nice body',
    'sound' => 'misc/androidnotification.mp3',

    'custom' => array(
        'route' => '/beee'
    )
);
$fields = array
(
    'collapse_key' => 'test',
    "time_to_live" => 0,
    'priority' => 'high',
    'to' => 'mykey',    
    'data'          => $msg,

);

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

$ch = curl_init();
curl_setopt( $ch,CURLOPT_URL, 'https://android.googleapis.com/gcm/send' );
curl_setopt( $ch,CURLOPT_POST, true );
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 );
curl_close( $ch );
echo $result;
hamobi
  • 7,940
  • 4
  • 35
  • 64
  • Is creating a JSON object in PHP usually similar in creating an array? The content of `$fields` array looks good, its just indicated in the [documentation](https://developers.google.com/cloud-messaging/http-server-ref#send-downstream) that its supposed to be JSON. [Setting priority](https://developers.google.com/cloud-messaging/concept-options#setting-the-priority-of-a-message) to `high` should wake up a sleeping device. Make sure that GCM delivers the message right away – adjuremods Mar 25 '16 at 23:47
  • the headers are "application/json" which will encode it as json. I'm making this app using corona SDK so all the regular java code you'd run in a native android app is unavailable to me. not sure if I need to run some notification code within the app itself to accomplish this.. – hamobi Mar 25 '16 at 23:49

1 Answers1

4

From the following two links

GCM Priority

Optimizing for Doze and App Standby

you can deduce that for high priority message

GCM attempts to deliver high priority messages immediately, allowing the GCM service to wake a sleeping device when possible and open a network connection to your app server.

and for normal message

Normal priority messages won't open network connections on a sleeping device, and their delivery may be delayed to conserve battery.

and as you can see from answer for the following question

you can never be sure that Android device is in sleep mode for Android version less than Marshmallow, for devices running Marshmallow or higher there is doze mode.

So obtain a device running Marshmallow or greater and put it to dose mode by running following commands

$ adb shell dumpsys battery unplug
$ adb shell dumpsys deviceidle step

You may need to run the second command more than once. Repeat it until the device state changes to idle.

Now try sending the push notification with high priority and normal priority. When the message priority is high the notification should be received and similarly when no priority is set or set to normal the notification will be delivered with some delay or when you wake up the device.

Community
  • 1
  • 1
Anirudha Agashe
  • 3,510
  • 2
  • 32
  • 47
  • thanks for the answer but as i said i'm using a razr maxx hd. my company isnt providing me any android devices to test on.. using an old one :( i think thats android 4.4.2 – hamobi Apr 03 '16 at 04:46
  • You can use emulator with Google Api image for testing purpose – Anirudha Agashe Apr 04 '16 at 05:15