响应说通知成功,但我的安卓手机上没有显示。
响应:
{"multicast_id":6696507350475373329,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1486651908616097%298b4204f9fd7ecd"}]}
PHP(来源:https://gist.github.com/prime31/5675017)
<?php
// API access key from Google API's Console
define( 'API_ACCESS_KEY', ThisIsMyKey);
$registrationIds = array( $_GET['id'] );
// prep the bundle
$msg = array
(
'message' => 'TestNachricht',
'title' => 'TestTitel',
'subtitle' => 'TestSubTitle',
'vibrate' => 1,
'sound' => 1,
'priority' => 10,
'smallIcon' => 'small_icon'
);
$fields = array
(
'registration_ids' => $registrationIds,
'data' => $msg
);
$headers = array
(
'Authorization: key=' . API_ACCESS_KEY,
'Content-Type: application/json'
);
$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;
安卓:
我没有添加 onReceive-Methods,因为我能够使用 FireBase 控制台广播通知并向特定设备 (RegistrationId) 发送通知。所有这些都显示在我的设备上。
private String getRegistrationId() {
return FirebaseInstanceId.getInstance().getToken();
}
<service android:name=".MyFirebaseInstanceIdService" android:exported="true">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
</intent-filter>
</service>
<service android:name=".MyFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
最佳答案
当通过 Firebase 控制台发送推送通知时,它被归类为 notification
消息负载。默认情况下,Android 会在您的应用处于后台时处理此消息。
在您的 PHP 代码中,您将发送一个仅包含 data
的消息负载,它可以在 Android 中通过实现 onMessageReceived()
来处理。
如果您仍然不想实现 onMessageReceived()
,则必须修改您的 PHP 端代码以使用仅通知
的消息负载。
请注意,notification
负载仅具有可以使用的预定义参数。
更多细节在这里:
关于android - Firebase 通知不显示使用 PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42140192/