Получено уведомление, отправленное через FCM в xamarin.forms, но изображение не отображается в системном трее - PullRequest
2 голосов
/ 29 января 2020

Я настроил приложение на FCM для отправки уведомлений на устройства android в Xamarin. Формы, но принимаются только уведомления, но изображения не отображаются в системном трее.

Это мой AndroidManifest. xml

<application android:label="DMSMobileApp.Android">
    <receiver
    android:name="com.google.firebase.iid.FirebaseInstanceIdInternalReceiver"
    android:exported="false" />
    <receiver
        android:name="com.google.firebase.iid.FirebaseInstanceIdReceiver"
        android:exported="true"
        android:permission="com.google.android.c2dm.permission.SEND">
      <intent-filter>
        <action android:name="com.google.android.c2dm.intent.RECEIVE" />
        <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
        <category android:name="${applicationId}" />
      </intent-filter>
    </receiver>
  </application>

Я создал FirebaseMessagingService.cs для получения и преобразования в локальные уведомления.

[Service]
    [IntentFilter(new[] { "com.google.firebase.MESSAGING_EVENT" })]
    public class MyFirebaseMessagingService : FirebaseMessagingService
    {
        const string TAG = "MyFirebaseMsgService";
        public override void OnMessageReceived(RemoteMessage message)
        {
            var body = message.GetNotification().Body;
            Log.Debug(TAG, "Notification Message Body: " + body);
            SendNotification(body, message.Data);
            //new NotificationHelper().CreateNotification(message.GetNotification().Title, message.GetNotification().Body);
        }
        void SendNotification(string messageBody, IDictionary<string, string> data)
        {
            var intent = new Intent(this, typeof(MainActivity));
            intent.AddFlags(ActivityFlags.ClearTop);
            foreach (var key in data.Keys)
            {
                intent.PutExtra(key, data[key]);
            }

            var pendingIntent = PendingIntent.GetActivity(this,
                                                          MainActivity.NOTIFICATION_ID,
                                                          intent,
                                                          PendingIntentFlags.OneShot);


            var notificationBuilder = new NotificationCompat.Builder(this, MainActivity.CHANNEL_ID)
                                      .SetSmallIcon(Resource.Drawable.notification_template_icon_bg)
                                      .SetContentTitle("FCM Message")
                                      .SetContentText(messageBody)
                                      .SetAutoCancel(true)
                                      .SetContentIntent(pendingIntent);

            var notificationManager = NotificationManagerCompat.From(this);
            notificationManager.Notify(MainActivity.NOTIFICATION_ID, notificationBuilder.Build());
        }
    }

И через REST API я отправляю уведомления.

var data = new
            {
                to = "/topics/ALL", // Recipient device token
                notification = new {
                    title = "Test",
                    body = "Message",
                    image = "https://cdn.pixabay.com/photo/2015/05/15/14/38/computer-768608_960_720.jpg"
                },
                image = "https://cdn.pixabay.com/photo/2015/05/15/14/38/computer-768608_960_720.jpg"
            };
var jsonBody = JsonConvert.SerializeObject(data);
            bool fcmState;
            using (var httpRequest = new HttpRequestMessage(HttpMethod.Post, "https://fcm.googleapis.com/fcm/send"))
            {
                httpRequest.Headers.TryAddWithoutValidation("Authorization", serverKey);
                httpRequest.Headers.TryAddWithoutValidation("Sender", senderId);
                httpRequest.Content = new StringContent(jsonBody, Encoding.UTF8, "application/json");

                using (var httpClient = new HttpClient())
                {
                    var result = await httpClient.SendAsync(httpRequest);

                    if (result.IsSuccessStatusCode)
                    {
                        fcmState = true;
                    }
                    else
                    {
                        // Use result.StatusCode to handle failure
                        // Your custom error handler here
                        //_logger.LogError($"Error sending notification. Status Code: {result.StatusCode}");
                    }
                }
            }
  1. Я могу получать уведомления в фоновом режиме, но на переднем плане я получаю только звук, но никаких уведомлений в системном трее.
  2. Я получаю уведомления в системный трей, когда приложение работает в фоновом режиме, но не получает изображение.

Ответы [ 2 ]

2 голосов
/ 30 января 2020

Я просто решил эту проблему, загрузив изображение, пока получаю данные изображения из FCM. Вот мой код.

void SendNotification(string messageBody, IDictionary<string, string> data, RemoteMessage message)
    {
        var intent = new Intent(this, typeof(MainActivity));
        intent.AddFlags(ActivityFlags.ClearTop);
        foreach (var key in data.Keys)
        {
            intent.PutExtra(key, data[key]);
        }

        var pendingIntent = PendingIntent.GetActivity(this,
                                                      MainActivity.NOTIFICATION_ID,
                                                      intent,
                                                      PendingIntentFlags.OneShot);

        //var test = message.GetNotification().Body;
        string title = data["title"];
        string body = data["body"];
        string imageReceived = data["image"]; //It contains image URL.
        GetImageBitmapFromUrl(imageReceived); // This method will download image from URL.
        var notificationBuilder = new NotificationCompat.Builder(this, MainActivity.CHANNEL_ID)
                                  .SetSmallIcon(Resource.Drawable.logo)
                                  .SetContentTitle(title)
                                  .SetContentText(body)
                                  .SetStyle(new NotificationCompat.BigPictureStyle().BigPicture(imageBitmap)) //// This will show the image in system tray.
                                  .SetAutoCancel(true)
                                  .SetContentIntent(pendingIntent);

        var notificationManager = NotificationManagerCompat.From(this);
        notificationManager.Notify(MainActivity.NOTIFICATION_ID, notificationBuilder.Build());
    }

Код для загрузки изображения, если есть ссылка в ключе изображения в Data IDictionary.

Bitmap imageBitmap = null;
    Bitmap roundedImage = null;
    public Bitmap GetImageBitmapFromUrl(string url)
    {
        using (var webClient = new System.Net.WebClient())
        {
            var imageBytes = webClient.DownloadData(url);
            if (imageBytes != null && imageBytes.Length > 0)
            {
                imageBitmap = BitmapFactory.DecodeByteArray(imageBytes, 0, imageBytes.Length);
                roundedImage = Bitmap.CreateScaledBitmap(imageBitmap, 300, 300, false);
                //roundedImage = getRoundedShape(resizedImage);
            }
            webClient.Dispose();
        }
        return roundedImage;
    }

Данные, которые я отправляю в FCM через REST API. Благодаря @Harikrishnan, сначала я использовал объект nofitication, он работал, но в нем не было данных изображения.

var data = new
            {
                to = "/topics/ALL", // Recipient device token
                data = new {
                    title = "Test",
                    body = "Message",
                    image = "https://cdn.pixabay.com/photo/2015/05/15/14/38/computer-768608_960_720.jpg"
                },
            };
1 голос
/ 29 января 2020

Уведомление FCM Имеется два типа сообщений

  • Отображение сообщений: Обратный вызов OnMessageReceived () только тогда, когда ваше приложение находится на переднем плане
  • Data-Messages: Обратный вызов OnMessageReceivedd (), даже если ваше приложение находится на переднем плане / фоне / убито.

По первому вопросу, пожалуйста, ознакомьтесь с приведенными ниже статьями для отправки уведомлений pu sh в Xamarin.

https://xmonkeys360.com/2019/12/08/xamarin-forms-fcm-setup-configuration-part-i/ https://xmonkeys360.com/2019/12/09/fcm-configuration-in-xamarin-forms-part-ii/

Вы должны использовать Data-Message для получения фонового уведомления согласно вашему 2-му требованию.

{
 "to" : "4OsejRWk8RF72znDZEr",

 "data": //Note that here it is sent as Data instead of notification
  {
    "body": "Hello Xamarin",
    "title": "Title"  
    "image": "image url"
  }
} 

Чтобы получить изображение в полученном уведомлении, я использую приведенный ниже код.

var imageReceived = message.Data["image"]; //Make sure you process the received image to required type based on your requirement. 
var notification = new NotificationCompat.Builder(this, MainActivity.CHANNEL_ID);
notification.SetContentIntent(pendingIntent)
                    .SetContentTitle("FCM Message")
                    .SetContentText(messageBody)
                    .SetLargeIcon(BitmapFactory.DecodeResource(Resources, Resource.Drawable.notification_template_icon_bg))                    .SetSmallIcon(Resource.Drawable.notification_template_icon_bg) //Pass here the image you received if that is your requirement.
                    .SetStyle(new NotificationCompat.BigTextStyle())
                    .SetPriority(NotificationCompat.PriorityHigh)
                    .SetColor(0x9c6114)
                    .SetAutoCancel(true);
notificationManager.Notify(MainActivity.NOTIFICATION_ID, notification.Build());
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...