【问题标题】:FCM API: not receiving any notification from PHP serverFCM API:未收到来自 PHP 服务器的任何通知
【发布时间】: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 中记录notificationTitlenotificationBody,但它也没有显示任何内容。我做错了什么吗?为什么我收不到通知?请帮我。

【问题讨论】:

    标签: javascript php android firebase firebase-cloud-messaging


    【解决方案1】:

    根据您的负载,您将发送带有employeebody 字段的data message

    但是,您的消息处理实现使用 getNotification() 用于通知消息。它不会为 notificationTitlenotificationBody 返回任何值,因为有效负载中没有设置 titlebody 字段。

    您应该改用getData()。它应该类似于this

    if (remoteMessage.getData().size() > 0) {
        Log.d(TAG, "Message data payload: " + remoteMessage.getData());
        if (true) {
            scheduleJob();
        } else {
            handleNow();
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-10-30
      • 2019-08-17
      • 2017-04-07
      • 2019-09-08
      • 1970-01-01
      • 2017-11-29
      • 1970-01-01
      相关资源
      最近更新 更多