php – Android – 具有谷歌云消息传递的高优先级消息(使用电晕sdk)

前端之家收集整理的这篇文章主要介绍了php – Android – 具有谷歌云消息传递的高优先级消息(使用电晕sdk)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我正试图唤醒手机或使用GCM让灯光闪烁.我收到的信息很好,但是设置高优先级或根本没有优先权.我正在使用razr maxx hd进行测试.这里有什么我想念的吗?

  1. PHP
  2. // API access key from Google API's Console
  3. define('API_ACCESS_KEY','blee');
  4. // prep the bundle
  5. $msg = array
  6. (
  7. 'body' => 'this is my nice body','sound' => 'misc/androidnotification.mp3','custom' => array(
  8. 'route' => '/beee'
  9. )
  10. );
  11. $fields = array
  12. (
  13. 'collapse_key' => 'test',"time_to_live" => 0,'priority' => 'high','to' => 'mykey','data' => $msg,);
  14. $headers = array
  15. (
  16. 'Authorization: key=' . API_ACCESS_KEY,'Content-Type: application/json'
  17. );
  18. $ch = curl_init();
  19. curl_setopt( $ch,CURLOPT_URL,'https://android.googleapis.com/gcm/send' );
  20. curl_setopt( $ch,CURLOPT_POST,true );
  21. curl_setopt( $ch,CURLOPT_HTTPHEADER,$headers );
  22. curl_setopt( $ch,CURLOPT_RETURNTRANSFER,CURLOPT_SSL_VERIFYPEER,false );
  23. curl_setopt( $ch,CURLOPT_POSTFIELDS,json_encode( $fields ) );
  24. $result = curl_exec($ch );
  25. curl_close( $ch );
  26. echo $result;
最佳答案
从以下两个链接

GCM Priority

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或更高版本的设备并将其置于剂量模式

  1. $adb shell dumpsys battery unplug
  2. $adb shell dumpsys deviceidle step

您可能需要多次运行第二个命令.重复此过程,直到设备状态变为空闲.

现在尝试发送具有高优先级和普通优先级的推送通知.当消息优先级高时,应该接收通知,类似地,当没有设置优先级或设置为正常时,通知将在延迟或唤醒设备时传送.

猜你在找的Android相关文章