【问题标题】:Not receiving FCM notifications send by PHP script未收到 PHP 脚本发送的 FCM 通知
【发布时间】:2019-09-08 01:44:03
【问题描述】:

我试图将 FCM 通知从 PHP 文件发送到 Android 应用程序。

应用收到从 Firebase Cloud Messaging 控制台发送的测试消息。

这是我的 FirebaseMessagingService 类:

public class MyFirebaseMessagingService extends FirebaseMessagingService {


    @Override
    public void onNewToken(String s) {
        Log.e("NEW_TOKEN", s);
    }

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {

        Map<String, String> params = remoteMessage.getData();
        JSONObject object = new JSONObject(params);
        Log.e("JSON_OBJECT", object.toString());

        String NOTIFICATION_CHANNEL_ID = "Nilesh_channel";

        long pattern[] = {0, 1000, 500, 1000};

        NotificationManager mNotificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "Your Notifications",
                    NotificationManager.IMPORTANCE_HIGH);

            notificationChannel.setDescription("");
            notificationChannel.enableLights(true);
            notificationChannel.setLightColor(Color.RED);
            notificationChannel.setVibrationPattern(pattern);
            notificationChannel.enableVibration(true);
            mNotificationManager.createNotificationChannel(notificationChannel);
        }

        // to diaplay notification in DND Mode
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = mNotificationManager.getNotificationChannel(NOTIFICATION_CHANNEL_ID);
            channel.canBypassDnd();
        }

        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);

        notificationBuilder.setAutoCancel(true)
                .setColor(ContextCompat.getColor(this, R.color.colorAccent))
                .setContentTitle(getString(R.string.app_name))
                .setContentText(remoteMessage.getNotification().getBody())
                .setDefaults(Notification.DEFAULT_ALL)
                .setWhen(System.currentTimeMillis())
                .setSmallIcon(R.mipmap.ic_launcher)
                .setAutoCancel(true);


        mNotificationManager.notify(1000, notificationBuilder.build());


    }
}

这是 Android Manifest 中的服务部分:

  <service
            android:name=".activity.MyFirebaseMessagingService"
            android:stopWithTask="false">
            <intent-filter>

                <action android:name="com.google.firebase.MESSAGING_EVENT" />
            </intent-filter>
        </service>

作为 PHP 部分,我使用以下脚本:

<?php
define('API_ACCESS_KEY','AAAAEX76PIM:...');
 $fcmUrl = 'https://fcm.googleapis.com/fcm/send';
 $token='e31H8qQq3ts:APA..';

     $notification = [
            'title' =>'title',
            'body' => 'body of message.',
            'icon' =>'myIcon', 
            'sound' => 'mySound'
        ];
        $extraNotificationData = ["message" => $notification,"moredata" =>'dd'];

        $fcmNotification = [
            //'registration_ids' => $tokenList, //multple token array
            'to'        => $token, //single token
            'notification' => $notification,
            'data' => $extraNotificationData
        ];

        $headers = [
            'Authorization: key=' . API_ACCESS_KEY,
            'Content-Type: application/json'
        ];


        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL,$fcmUrl);
        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($fcmNotification));
        $result = curl_exec($ch);
        curl_close($ch);

API 密钥和令牌已检查并正确。

请看看我的代码,如果我做错了什么,请告诉我。

事实上,该应用没有从 PHP 脚本而是从 FCM 控制台收到任何 FCM 通知。

【问题讨论】:

    标签: php android firebase-cloud-messaging


    【解决方案1】:

    我可以想到一种无法传递通知的方式,即令牌不正确。

    当 Android 应用程序向 Firebase 注册自身时,会生成一个令牌。 然后后端服务器(在本例中为您的 PHP 脚本)使用此令牌来传递推送通知。

    由于您已验证令牌正确,您将 $result 打印到控制台输出。

    这可以为我们提供有关正在发生的事情的线索。

    【讨论】:

    • 我检查了 $result 的值,它给了我线索。应用程序用户的令牌已更改。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-30
    • 1970-01-01
    • 2019-08-17
    • 2017-04-26
    • 1970-01-01
    • 2021-02-11
    • 2021-05-23
    相关资源
    最近更新 更多