Sending a Firebase Cloud Messaging push notification not from the console

I tried to attach FCM to my Android application, but it partially worked out - when creating a notification in the Firebase console, notifications come to the device, but I want a push notification about this action to be sent to the other devices when a certain action is performed in the application.

To begin with, I decided to use CURL to simulate sending messages, according to the logs, messages are processed in the onMessageReceived (RemoteMessage remoteMessage), however, push notifications do not appear.

Service in AndroidManifest

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

When registering, I subscribe devices to the "test" channel

FirebaseMessaging.getInstance().subscribeToTopic("test");

Method onMessageReceived

public void onMessageReceived(RemoteMessage remoteMessage) {
   showNotification (remoteMessage.getData().get("title"), remoteMessage.getData().get("context"));
}

Sending a request via a PHP script

<?php
// API access key from Google API's Console
define( 'API_ACCESS_KEY', 'AIzaSyB4ZanJWaCay_fXxO5z0jI55T8UOwdGNNQ' );

// prep the bundle
$msg = array
(
    'user'      => 'somebody',
    'title'     => 'This is a title. title',
    'context'   => 'here is a message. message',
    'vibrate'   => 1,
    'sound'     => 1
);

$fields = array
(
    'to'            => '/topics/test',
    'data'          => $msg
);

$headers = array
(
    'Authorization: key=' . API_ACCESS_KEY,
    'Content-Type: application/json'
);

$ch = curl_init();
curl_setopt( $ch,CURLOPT_URL, 'https://fcm.googleapis.com/fcm/send' );
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( $fields ) );
$result = curl_exec($ch );
curl_close( $ch );
echo $result;
?>

When executing this request, I get

{"message_id":5069819597562559135}

Moreover, the data that is sent in the request, the application receives, but there is no push notification. Because of what my requests do not appear push notifications?

Author: Nether, 2020-03-31

1 answers

Solved the problem, it turns out to be on the OS version below 8 everything works fine, but above will not work (without additional body movements).

In 8 android versions added such a thing as "Channels " . If your notification does not belong to any of the channels - it will not be displayed. You can read more in detailright here.

Fixed my problem as follows:

  1. Added the channel name to resources
<string name="default_notification_channel_id" translatable="false">fcm_default_channel</string>
  1. Added meta data to the project manifest
<application>
...
<meta-data
    android:name="com.google.firebase.messaging.default_notification_channel_id"
    android:value="@string/default_notification_channel_id"/>
</application>
  1. Now, before calling the notification (NotificationManager. notify), we will assign our channel
...настройка notificationManager

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    String channelId = context.getString(R.string.default_notification_channel_id);
    NotificationChannel channel = new NotificationChannel(channelId,   title, NotificationManager.IMPORTANCE_DEFAULT);
    channel.setDescription(body);
    mNotificationManager.createNotificationChannel(channel);
    builder.setChannelId(channelId);
}

notificationManager.notify(1, notification);
 1
Author: Nether, 2020-03-31 18:32:27