【发布时间】:2022-11-18 08:45:11
【问题描述】:
我正在从我的localhost 服务器发送通知消息,如下所示:
Java脚本:
function send_notification(empno,charge,op){
return $.ajax({
url: '/notification_server/firebase_server.php',
type: 'POST',
data:{
"employee": empno,
"body":op
},
cache: false
})
}
(async() => {
await send_notification("Hi Leo","This is a test notification");
})();
PHP:
<?php
require __DIR__.'/vendor/autoload.php';
use Kreait\Firebase\Factory;
use Kreait\Firebase\Messaging\CloudMessage;
$factory = (new Factory)
->withServiceAccount('admin.json')
->withDatabaseUri("https://nfr-qtr-electric-billing-default-rtdb.firebaseio.com");
$deviceToken = '.....'; //I have my actual device token here
$messaging = $factory->createMessaging();
$message = CloudMessage::withTarget('token', $deviceToken)
->withNotification(['title' => $_POST["employee"], 'body' => $_POST["body"]]);
$messaging->send($message);
?>
我正在我的android 阅读这样的通知:
public class MyFirebaseMessagingService extends FirebaseMessagingService {
@Override
public void onNewToken(@NonNull String token) {
super.onNewToken(token);
}
@Override
public void onMessageReceived(@NonNull RemoteMessage remoteMessage) {
String notificationTitle = null, notificationBody = null;
if (remoteMessage.getNotification() != null) {
notificationTitle = remoteMessage.getNotification().getTitle();
notificationBody = remoteMessage.getNotification().getBody();
sendLocalNotification(notificationTitle, notificationBody);
}
}
private void sendLocalNotification(String notificationTitle, String notificationBody) {
Intent intent = new Intent(this, record_viewer.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setAutoCancel(true) //Automatically delete the notification
.setSmallIcon(R.mipmap.ic_launcher) //Notification icon
.setContentIntent(pendingIntent)
.setContentTitle(notificationTitle)
.setContentText(notificationBody)
.setSound(defaultSoundUri);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1234, notificationBuilder.build());
}
}
执行代码后,我没有收到任何通知。在网络控制台中,显示AJAX请求已经发送,我的php服务器代码没有错误。我尝试在我的FirebaseMessagingService 中记录notificationTitle 和notificationBody,但它也没有显示任何内容。我做错了什么吗?为什么我收不到通知?请帮我。
【问题讨论】:
标签: javascript php android firebase firebase-cloud-messaging