我从PHP推送Firebase消息,它们通常会得到目标Android应用程序。
尽管如此,偶尔,如果目标安卓手机在一段时间内处于非活动状态,推送消息不会正确传递。然后,如果我打开应用程序,消息会立即传递。
我读到关于多兹的状态;电池优化;等等。我不想麻烦用户明确地将应用程序列入白名单。
提前感谢!
PHP:
private function sendFirebaseMessage($msg, $to_uid) {
// getting firebase token ID
$user_token_id = DB::getInstance()->getUserFirebaseTokenId($to_uid);
#API access key from Google API's Console
define( 'API_ACCESS_KEY', 'A...7' );
$fields = array
(
'to' => $user_token_id,
'data' => $msg,
"priority" => "high"
);
$headers = array
(
'Authorization: key=' . API_ACCESS_KEY,
'Content-Type: application/json'
);
#Send Reponse To FireBase Server
$ch = curl_init();
curl_setopt( $ch,CURLOPT_URL, 'https://fcm.googleapis.com/fcm/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 Of FireBase Server
return $user_token_id . "]XXX[" . $result;
}
安卓/Java:
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
// Check if message contains a data payload.
Map<String, String> data = remoteMessage.getData();
if (data.size() > 0) {
Log.d(TAG, "StorableMessage data payload: " + data);
if (data.containsKey("sender_id") &&
data.containsKey("sender_name") &&
data.containsKey("chat_topic")) {
if (!ChatActivity.IsVisible) {
NotifsManager.showFirebaseNotif(this,
data.get("sender_name"),
data.get("sender_id"),
data.get("chat_topic"));
}
}
}
}
如果设备正在休眠,消息不会立即传递,FCM将以这种方式工作。
此外,如果您在清单中添加REQUEST_IGNORE_BATTERY_OPTIMIZATIONS权限,如果您的应用程序不符合以下要求,谷歌可能会在PLay商店中屏蔽您的应用:https://developer.android.com/training/monitoring-device-state/doze-standby.html#exemption-案例