如何在FCM离子通知中显示图像

时间:2019-12-02 12:00:49

标签: ionic-framework firebase-cloud-messaging

嗨,我正在使用Cordova插件“ FCM”处理推送通知,当从服务器推送通知时,我能够在通知内容上显示文本,但工作正常  但是我无法显示图片,但我有一个网址包含jpg formate中的图片。有人可以告诉他如何显示图片

我导入了

import { FCM } from '@ionic-native/fcm';

if (device.platform == "Android" || device.platform == "iOS") {
    fcm.getToken().then(token => {
    //console.log("FCM Token generation :: " + token);
    fcm.onNotification().subscribe(data => {
      if (data.wasTapped) {
        //  console.log("onNotification", JSON.stringify(data))
                        }
    else {
        //  console.log("onNotification", JSON.stringify(data))
        }
    });

}

this is response from server

我需要显示图像才能有人帮助我

这是我用来将数据推送到Firebase控制台的C#代码

var fcmUrl = "https://fcm.googleapis.com/fcm/send";

       var serverKey = "serverKey";

        var senderId = "9866455";





        string sResponseFromServer = string.Empty;



        try

        {

            //WebRequest request = WebRequest.Create(fcmUrl);



            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(fcmUrl);

            request.ServerCertificateValidationCallback += (sender, certificate, chain, sslPolicyErrors) => true;

            request.Method = "post";

            request.ContentType = "application/json";

            var data = new

            {

                to = "/topics/test2",

                notification = new

                {

                    title = "Notification With Image",

                    body = " Message from Data",                        

                    image = "https://abcdedd.com/assets/images/loginheaderlogo.png"                      

                }



            };



            var json = JsonConvert.SerializeObject(data);

            Byte[] byteArray = Encoding.UTF8.GetBytes(json);

            request.Headers.Add(string.Format("Authorization: key={0}", serverKey));

            request.Headers.Add(string.Format("Sender: id={0}", senderId));

            request.ContentLength = byteArray.Length;

            request.Proxy = WebRequest.DefaultWebProxy;

            request.Proxy.Credentials = CredentialCache.DefaultNetworkCredentials;



            using (Stream dataStream = request.GetRequestStream())

            {

                dataStream.Write(byteArray, 0, byteArray.Length);

                using (WebResponse tResponse = request.GetResponse())

                {

                    using (Stream dataStreamResponse = tResponse.GetResponseStream())

                    {

                        using (StreamReader tReader = new StreamReader(dataStreamResponse))

                        {

                            sResponseFromServer = tReader.ReadToEnd();

                        }

                    }

                }

            }

            var result = JsonConvert.DeserializeObject(sResponseFromServer);

            Console.WriteLine(result.ToString());

        }

        catch (Exception ex)

        {

           var e = ex.Message;

            Console.WriteLine(ex.ToString());

        }

1 个答案:

答案 0 :(得分:0)

对于nodejs: 工作示例:

var FCM = require('fcm-node');
    var serverKey = 'asdf-asdf'; //put your server key here
    var fcm = new FCM(serverKey);

    var message = { //this may vary according to the message type (single recipient, multicast, topic, et cetera)
        to: 'asdf-asdf', // your device Token        
        notification: {
            title: 'Notification Title', 
            body: 'Notification body',
            image: 'https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png' // your image URL
        },

        data: {  //you can send only notification or only data(or include both)
            my_key: 'my value',
            my_another_key: 'my another value'
        }
    };

    fcm.send(message, function(err, response){
        if (err) {
            console.log("Something has gone wrong!");
        } else {
            console.log("Successfully sent with response: ", response);
        }
    });