Skip to content Skip to sidebar Skip to footer

Firebase Fail Receiving Notification From Php

I can succesfully recive notifications from Firebase console, but when I call the PHP function to do it, doesn't recive anything. As I saw on internet, this is more less how I can

Solution 1:

By documentation you can send two types of messages to clients:

Notification messages - handled by the FCM SDK automatically.

Data messages - handled by the client app.

If you want to send data message implement your FirebaseMessagingService like this (this is only example what to do, you can use it but improve it with your needs and also test all possible solution to understand how that works)

public class MyFirebaseMessagingService extends FirebaseMessagingService {

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        if (remoteMessage.getData().size() > 0) {
            handleDataMessage(remoteMessage.getData());
        }else if (remoteMessage.getNotification() != null) {
            //remove this line if you dont need or improve...
            sendNotification(100, remoteMessage.getNotification().getBody());
        }
    }

    private void handleDataMessage(Map<String, String> data) {
        if(data.containsKey("id") && data.containsKey("message")){
            String message = data.get("message");
            int id;
            try {
                id = Integer.parseInt(data.get("id"));
            }catch (Exception e){
                e.printStackTrace();
                id = 1; // default id or something else for wrong id from server
            }

            sendNotification(id, message);
        }
    }


    private void sendNotification(int id, String messageBody) {
        Intent intent = new Intent(this, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 , intent, PendingIntent.FLAG_ONE_SHOT);
        String channelId = "appChannelId"; //getString(R.string.default_notification_channel_id);
        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder =
                new NotificationCompat.Builder(this, channelId)
                        .setSmallIcon(R.drawable.icon) //TODO set your icon
                        .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.iconpro_round)) //TODO set your icon
                        .setContentTitle("MyApp")
                        .setContentText(messageBody)
                        .setAutoCancel(true)
                        .setSound(defaultSoundUri)
                        .setContentIntent(pendingIntent);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) notificationBuilder.setPriority(NotificationManager.IMPORTANCE_HIGH);
        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel(channelId, "MyApp", NotificationManager.IMPORTANCE_DEFAULT);
            if (notificationManager != null) {
                notificationManager.createNotificationChannel(channel);
            }
        }
        if (notificationManager != null) {
            notificationManager.notify(id, notificationBuilder.build());
        }
    }
}

Solution 2:

@adnandann's answer explains the reason indeed: you're sending a data message, while the Firebase console always sends a notification message (which is automatically displayed by the operation system).

So you have two options:

  1. Display the data message yourself, which @adnandann's answer shows how to do.

  2. Send a notification message from your PHP code, which you can do with:

    $fields = array (
        'to' => $device_id,
        'notification' => array(
          "title" => "New message from app",
          "body" => $message
        ),
    );
    

Post a Comment for "Firebase Fail Receiving Notification From Php"