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)

publicclassMyFirebaseMessagingServiceextendsFirebaseMessagingService {

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

    privatevoidhandleDataMessage(Map<String, String> data) {
        if(data.containsKey("id") && data.containsKey("message")){
            Stringmessage= 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);
        }
    }


    privatevoidsendNotification(int id, String messageBody) {
        Intentintent=newIntent(this, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntentpendingIntent= PendingIntent.getActivity(this, 0 , intent, PendingIntent.FLAG_ONE_SHOT);
        StringchannelId="appChannelId"; //getString(R.string.default_notification_channel_id);UridefaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.BuildernotificationBuilder=newNotificationCompat.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);
        NotificationManagernotificationManager= (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannelchannel=newNotificationChannel(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"