简体   繁体   English

未收到PHP脚本发送的FCM通知

[英]Not receiving FCM notifications send by PHP script

I a trying to send FCM notifications from a PHP file to an Android app. 我试图将FCM通知从PHP文件发送到Android应用程序。

The app receives a test message send from Firebase Cloud Messaging console. 该应用会收到从Firebase Cloud Messaging控制台发送的测试消息。

This is my FirebaseMessagingService class: 这是我的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());


    }
}

And this is the services part in Android Manifest: 这是Android Manifest中的服务部分:

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

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

As PHP part I am using following script: 作为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 Key and token are checked and right. API密钥和令牌已检查并正确。

Please take a look at my code and tell me if am I doing something wrong. 请查看我的代码,并告诉我是否做错了什么。

The fact is that the app doesn't receive any FCM notification from the PHP script but from FCM console. 事实是,该应用程序未从PHP脚本收到任何FCM通知,而是从FCM控制台收到了任何FCM通知。

I can think of one way where a notification could not be delivered, which is when the token is incorrect. 我可以想到一种无法传递通知的方式,那就是令牌不正确。

When the Android Application registers itself with Firebase, a token is generated. 当Android应用程序向Firebase注册自己时,会生成一个令牌。 This token is then used by backend servers(In this case, your PHP script) to deliver push notifications. 然后,后端服务器(在本例中为您的PHP脚本)使用此令牌来传递推送通知。

Since you have verified that the token is proper,you print out $result to the console output. 由于已验证令牌正确,因此可以将$ result打印到控制台输出。

This could provide us with clues on what is happening. 这可以为我们提供正在发生的事情的线索。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 FCM 使用 php 脚本向多个设备发送推送通知? - How to send push notifications to multiple devices using php script using FCM? 后台脚本在php中发送通知 - Background script to send notifications in php 在PHP应用程序中使用FCM XMPP协议发送推送通知 - Send push notifications using FCM XMPP protocol within the PHP application 如何使用FCM从php向iOS发送推送通知 - how to send push notifications from php to IOS using FCM 如何在 PHP 中使用 fcm(firebase 控制台)向 iphone 发送推送通知? - how to send push notifications to iphone using fcm(firebase console) in PHP? 如何向多个主题发送 FCM 推送通知 - How to send FCM Push Notifications to multiple topics 当我使用curl发送fcm通知时,php脚本执行两次 - php script executing twice when i am sending fcm notifications using curl 在 PHP 中设置 FCM 通道 ID 用于通知 - Set FCM channel ID in PHP for notifications 获取使用FCM发送通知的自定义帖子类型的ID和标题 - Get the id and title of custom post type for send notifications with FCM 使用MySQL查询作为PHP脚本的标识,将FCM Push通知发送到android应用中的特定设备 - Send FCM Push notifcations to specific devices in android app using MySQL query as identification from a PHP script
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM