【发布时间】:2017-01-14 01:29:19
【问题描述】:
我已经清楚地阅读了很多关于 Firebase 推送通知行为的文档。当我的应用程序在后台时,我可以在系统托盘上看到通知,但在应用程序在前台时不会触发onMessageReceivedcall back。我可以在我的应用程序上看到收到消息的日志。我尝试过使用数据有效负载进行调用,而没有来自应用服务器的数据有效负载。我的应用服务器正在使用 php 脚本来触发 fcm api。以下是我正在使用的 php 脚本
$url = 'https://fcm.googleapis.com/fcm/send';
$fields = array(
'registration_ids' => $registatoin_ids,
'notification' => array( "title"=>$title, "body" => $notification_message ,"click_action" => $click_action,'color' => "#74BCEE",'icon' => "school_logo"));
$headers = array(
'Authorization: key='xxxxxxxxx',
'Content-Type: application/json'
);
// Open connection
$ch = curl_init();
//echo "here";exit;
// 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);
// 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);
if ($result === FALSE) {
//die('Curl failed: ' . curl_error($ch));
curl_close($ch);
return;
}
print_r($result);
以下是我在 Android 应用中使用的服务
<service
android:name="service.MyFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<service
android:name="service.MyFirebaseInstanceIdService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
</intent-filter>
</service>
【问题讨论】:
-
当您说您尝试使用数据负载时,您的意思是您将 PHP 代码中 JSON 中的
notification字段替换为data吗?即使这样,当您的应用程序处于前台时,您的 onMessageReceived 方法也不会被调用? -
我怀疑问题出在您的 AndroidManifest.xml 中。尝试用“.service.MyFirebaseInstanceIdService”替换“service.MyFirebaseInstanceIdService”(注意开头的点)。请参阅developer.android.com/guide/topics/manifest/… 中的“android:name”部分
-
@GeorgeLBA 我尝试将通知和数据有效负载一起发送。在有和没有数据有效负载的情况下,当应用程序处于前台时,不会调用 onMessageReceived。我可以看到在 FirebaseMessagingService 中收到消息的日志
-
@DiegoGiorgini 我已将包名称重组为单个文件夹,以便我可以直接使用服务。在清单文件中。
-
什么意思?名称必须以“.”开头如果它们是相对于包名的,或者应该包含完整的包名
标签: php android firebase firebase-cloud-messaging