我正试图唤醒手机或使用GCM让灯光闪烁.我收到的信息很好,但是设置高优先级或根本没有优先权.我正在使用razr maxx hd进行测试.这里有什么我想念的吗?
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,CURLOPT_SSL_VERIFYPEER,false );
curl_setopt( $ch,CURLOPT_POSTFIELDS,json_encode( $fields ) );
$result = curl_exec($ch );
curl_close( $ch );
echo $result;
最佳答案
从以下两个链接
Optimizing for Doze and App Standby
您可以推断出高优先级消息
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.
并为正常的消息
Normal priority messages won’t open network connections on a sleeping
device,and their delivery may be delayed to conserve battery.
正如您在以下question的答案中所看到的那样
你永远不能确定Android设备是否处于睡眠模式,因为Android版本低于Marshmallow,对于运行Marshmallow或更高版本的设备,有打盹模式.
因此,通过运行以下命令,获取运行Marshmallow或更高版本的设备并将其置于剂量模式
$adb shell dumpsys battery unplug
$adb shell dumpsys deviceidle step
您可能需要多次运行第二个命令.重复此过程,直到设备状态变为空闲.
现在尝试发送具有高优先级和普通优先级的推送通知.当消息优先级高时,应该接收通知,类似地,当没有设置优先级或设置为正常时,通知将在延迟或唤醒设备时传送.