[英]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.