Я пытаюсь создать push-уведомление с помощью PHP. Я создал свою учетную запись в Firebase и добавил, что нужно добавить в мое приложение для Android. Я протестировал его с помощью консоли Firebase, и он получает уведомление.
Теперь я пытаюсь создать push-уведомление, используя PHP ниже, мой код
$apiKey = "apikey"; //Server Key Legacy
$fcmUrl = 'https://fcm.googleapis.com/fcm/send';
$token= 'singletoken';
$notification = array('title' =>'test title',
'body' => 'hello message');
$notifdata = array('title' =>'test title data',
'body' => 'hello',
'image' => 'path'
);
$fcmNotification = array (
'to' => $token,
'notification' => $notification,
'data' => $notifdata
);
$headers = array( 'Authorization: key='.$apiKey, '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);
В моем Login Activity
Я использую код ниже, чтобы получить свой регистрационный идентификатор
FirebaseInstanceId.getInstance().getInstanceId().addOnSuccessListener( this, new OnSuccessListener<InstanceIdResult>() {
@Override
public void onSuccess(InstanceIdResult instanceIdResult) {
device_key = instanceIdResult.getToken();
Log.e(TAG, device_key);
}
});
А вот мой MyFirebaseMessagingService
класс
public class MyFirebaseMessagingService extends FirebaseMessagingService {
@Override
public void onNewToken(String s) {
super.onNewToken(s);
}
@Override
public void onMessageReceived(RemoteMessage message) {
super.onMessageReceived(message);
sendMyNotification(message.getNotification().getBody());
}
private void sendMyNotification(String message) {
Intent intent = new Intent(this, NotificationActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
String channelId = "Default";
Uri soundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("My App")
.setContentText(message)
.setAutoCancel(true)
.setSound(soundUri)
.setContentIntent(pendingIntent);
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(channelId, "Default channel", NotificationManager.IMPORTANCE_DEFAULT);
manager.createNotificationChannel(channel);
}
manager.notify(0, builder.build());
}
}
Я получаю уведомления на свойAndroid Emulator, но в реальном устройстве я не получаю никаких уведомлений.