Firebase 消息会在设备上接收,但不会通过通知显示



我正在尝试将Firebase Cloud Messaging (我使用此代码,在此处镜像)与我现有的应用程序集成。

相关代码:

服务.php

$msg_id = $_POST['id'];
$title = $_POST['title'];
$content = $_POST['content'];
$msg_date = $_POST['date'];
$author = $_POST['author'];
// insert the message into DB               
$sql = "INSERT INTO tbl_message(msg_id, title, content, msg_date, author) VALUES ('$msg_id','$title','$content','$msg_date','$author')";
if ($result = mysqli_query($db, $sql)){
$firebase = new Firebase();
$push = new Push();
$payload = array();
$payload['foo'] = '123';
$payload['bar'] = 'xyz';
$push->setTitle($title);
$push->setMessage($content);
$push->setAuthor($author);
$push->setDate($msg_date);
$push->setImage('');
$push->setPayload($payload);
$push->setIsBackground(FALSE);
$resp = '';
$response = '';
$resp = $push->getPush();
$response = $firebase->sendToTopic('global', $resp);
echo $response;
}

火力基地.php

<?php

class Firebase {
// sending push message to single user by firebase reg id
public function send($to, $message) {
$fields = array(
'to' => $to,
//'data' => $message,
'data' => array("message" => $message));
return $this->sendPushNotification($fields);
}
// Sending message to a topic by topic name
public function sendToTopic($to, $message) {
$fields = array(
'to' => '/topics/' . $to,
//'data' => $message,
'data' => array("message" => $message)
);
return $this->sendPushNotification($fields);
}
// sending push message to multiple users by firebase registration ids
public function sendMultiple($registration_ids, $message) {
$fields = array(
'to' => $registration_ids,
//'data' => $message,
'data' => array("message" => $message)
);
return $this->sendPushNotification($fields);
}
// function makes curl request to firebase servers
private function sendPushNotification($fields) {
require_once 'config.php';
// Set POST variables
$url = 'https://fcm.googleapis.com/fcm/send';
$headers = array(
'Authorization: key=' . FIREBASE_API_KEY,
'Content-Type: application/json'
);
// Open connection
$ch = curl_init();
// 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));
}
// Close connection
curl_close($ch);
return $result;
}
}
?>

主活动.java

package studio.emcorp.monitoringsiswa;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.NavigationView;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import com.google.firebase.messaging.FirebaseMessaging;

public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
private static final String TAG = MainActivity.class.getSimpleName();
private BroadcastReceiver mRegistrationBroadcastReceiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
;
mRegistrationBroadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// checking for type intent filter
if (intent.getAction().equals(NotificationConfig.REGISTRATION_COMPLETE)) {
// gcm successfully registered
// now subscribe to `global` topic to receive app wide notifications
FirebaseMessaging.getInstance().subscribeToTopic(NotificationConfig.TOPIC_GLOBAL);
displayFirebaseRegId();
} else if (intent.getAction().equals(NotificationConfig.PUSH_NOTIFICATION)) {
// new push notification is received
String message = intent.getStringExtra("message");
Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG).show();
//txtMessage.setText(message);
}
}
};

@Override
protected void onResume() {
super.onResume();
// register GCM registration complete receiver
LocalBroadcastManager.getInstance(this).registerReceiver(mRegistrationBroadcastReceiver,
new IntentFilter(NotificationConfig.REGISTRATION_COMPLETE));
// register new push message receiver
// by doing this, the activity will be notified each time a new message arrives
LocalBroadcastManager.getInstance(this).registerReceiver(mRegistrationBroadcastReceiver,
new IntentFilter(NotificationConfig.PUSH_NOTIFICATION));
// clear the notification area when the app is opened
NotificationUtil.clearNotifications(getApplicationContext());
}
@Override
protected void onPause() {
LocalBroadcastManager.getInstance(this).unregisterReceiver(mRegistrationBroadcastReceiver);
super.onPause();
}
}

直接从 Firebase 控制台发送消息工作正常。现在的问题是通过PHP脚本发送消息。服务器如何设法成功发送消息,例如:

{"message_id":4732936739964848111}

,设备将收到它:

06-19

09:26:21.590 18604-19560/net.anta40.app.firebasetest E/MyFirebase消息服务: 来自:/topics/global 06-19 09:26:21.590 18604-19560/net.anta40.app.firebasetest E/MyFirebaseMessagingService: 数据有效负载: {message={"data":{"image":","is_background":false,"payload":{"bar":"xyz","foo":"123"},"title":"test","message":"mehehehe","timestamp":"2017-06-19 2:26:21"}}} 06-19 09:26:21.600 18604-19560/net.anta40.app.firebasetest E/MyFirebaseMessagingService: push json: {"消息":{"数据":{"时间戳":"2017-06-19 2:26:21","message":"mehehehe","payload":{"bar":"xyz","foo":"123"},"image":","title":"test","is_background":false}}} 06-19 09:26:21.600 18604-19560/net.anta40.app.firebasetest E/MyFirebase消息传递服务:JSON 异常:数据无值 06-19 09:26:21.720 1098-1098/?D/wpa_supplicant: 接收ctrl_iface - hexdump(len=11): 53 49 47 4e 41 4c 5f 50 4f 4c 4c 06-19 09:26:21.720 1098-1098/?D/wpa_supplicant:wlan0:控制接口命令 "SIGNAL_POLL">

但不通过通知显示。这里出了什么问题?

你在 PHP 中发送一个仅限data的有效负载:

public function send($to, $message) {
$fields = array(
'to' => $to,
//'data' => $message,
'data' => array("message" => $message));
return $this->sendPushNotification($fields);
}

您的其他方法也发送相同的内容。

如果要使用仅notification有效负载,则可以简单地将其构造如下:

public function send($to, $message) {
$fields = array(
'to' => $to,
'notification' => array("title" => $title,
"body" => $body));
return $this->sendPushNotification($fields);
}

与从 Firebase 控制台发送消息的区别在于,来自控制台的消息被视为notification消息。

在 Android 中,每种消息类型的处理方式不同(请参阅处理消息)。

一些有用的帖子:

  • 商品信息管理通知问题
  • 我们是否可以仅从 Firebase 通知控制台推送数据消息

相关内容

  • 没有找到相关文章

最新更新