NEWS

[C#] Hướng dẫn push notification sử dụng Firebase Cloud Message

[C#] Hướng dẫn push notification sử dụng Firebase Cloud Message
Đăng bởi: Thảo Meo - Lượt xem: 10110 14:36:49, 19/09/2018DEVEXPRESS   In bài viết

Xin chào các bạn, bài viết hôm nay, mình sẽ hướng dẫn các bạn cách gởi notification FireBase Cloud Message sử dụng C# và gởi http request post đến PHP.

Để chạy được ứng dụng này, các bạn cần phải tạo FireBase trên Android.

Còn trong bài này, mình chỉ hướng dẫn gởi Request từ app Winform C# lên thôi.

Dưới đây là giao diện demo ứng dụng:

send firebase cloud message c#

Cấu hình source code PHP, để tạo lệnh gởi nội dung lên firebase

public function sendNotification() {
    $to = $_POST["to"];        
    $title = $_POST["title"];
    $body = $_POST["body"];
    $tag = $_POST["tag"];      
  
    // $to = "";
    // $title = "Thông báo!";
    // $body = "This is a message";
    // $tag = "nghiphep";

    $url = 'https://fcm.googleapis.com/fcm/send';

    $fields = array (
            'to' => "/topics/MyTopic",
            "priority" => "high",
            'data' => array (
                "body" => $body,
                "title" => $title,
                "sound" => "default",
                "tag" => $tag,
                "to" => $to
            )
    );
    $fields = json_encode ( $fields );

    $headers = array (
            'Authorization: key=' . "AAAAD91fqNY:APA91bGTm5WkABxptKYy-5fWmfMTkZx19IQHVLwDCOv2IEIQkFGdSOJ4EUkTyiKDlq-MA4CR2Fnh5X_dYp4ypNAdvq0KJINr9JRMvneY9U7hsjemokJf72pNXfyJqif4ALXAJJCr5dJD",
            'Content-Type: application/json'
    );

    $ch = curl_init ();
    curl_setopt ( $ch, CURLOPT_URL, $url );
    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, $fields );

    $result = curl_exec ( $ch );
    echo $result;
    curl_close ( $ch );
}

Và dưới đây là source code C#, trong bài viết này mình sử dụng thư viện Microsoft.Net.Http để gởi http request post async lên file php mình vừa tạo ở bên trên:

Cài đặt Nuget: PM> Install-Package Microsoft.Net.Http -Version 2.2.29

Source code C# send http request:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace jsonHttpRequest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private async void button1_Click(object sender, EventArgs e)
        {
            string result = await SendNotification("", txt_title.Text, txt_body.Text, "");
            //MessageBox.Show(result);
        }
        public async Task SendNotification(string to, string title, string body, string tag)
        {
            HttpClient client = new HttpClient();
            try
            {
                var values = new Dictionary<string, string>
                    {
                        { "to", to },
                        { "title", title },
                        { "body", body },
                        { "tag", tag }
                    };

                var content = new FormUrlEncodedContent(values);

                var response = await client.PostAsync("http://192.168.0.3:8081/HOBWEB/android_api/sendNotification", content);

                return await response.Content.ReadAsStringAsync();
            }
            catch (Exception ex)
            {
                return ex.Message.ToString();
            }

        }
    }
}

HAPPY CODING heart

Tags: firebasefcm

THÔNG TIN TÁC GIẢ

BÀI VIẾT LIÊN QUAN

[C#] Hướng dẫn push notification sử dụng Firebase Cloud Message
Đăng bởi: Thảo Meo - Lượt xem: 10110 14:36:49, 19/09/2018DEVEXPRESS   In bài viết

CÁC BÀI CÙNG CHỦ ĐỀ

Đọc tiếp
.