Skip to content Skip to sidebar Skip to footer

Xamarin Android Fcm Notification Client To Client(phone To Phone)

I follow the tutorial in https://docs.microsoft.com/en-us/xamarin/android/data-cloud/google-messaging/remote-notifications-with-fcm?tabs=vswin and manage to make server to client n

Solution 1:

First, please follow this step by step to complete it, then you will get the base application.

Second, add a button to send Http to the FCM server:

<?xml version="1.0" encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="match_parent"android:layout_height="match_parent"><TextViewandroid:text=" "android:layout_width="wrap_content"android:layout_height="wrap_content"android:id="@+id/msgText"android:textAppearance="?android:attr/textAppearanceMedium"android:padding="10dp" /><Buttonandroid:id="@+id/bt"android:layout_width="match_parent"android:layout_height="wrap_content"/></LinearLayout>

initialize the button in OnCreate:

protectedoverridevoidOnCreate(Bundle savedInstanceState)
{
    base.OnCreate(savedInstanceState);

    // Set our view from the "main" layout resource
    SetContentView(Resource.Layout.Main);
    msgText = FindViewById<TextView>(Resource.Id.msgText);
    IsPlayServicesAvailable();
    Button bt = FindViewById<Button>(Resource.Id.bt);
    bt.Click += Bt_Click;

}

Here I used two class to generate the json string, and I am using json.net:

classMes
{
    publicstring to;
    public Noti notification;
    publicMes(string to,Noti notification){
        this.to = to;
        this.notification = notification;
    }
}
classNoti {
    publicstring title;
    publicstring text;
    publicNoti(string body,string text) {
        this.title = body;
        this.text = body;
    }
}

Click event(I am using okhttp3):

privatevoidBt_Click(object sender, System.EventArgs e)
{

    Mes mes = newMes("your token",
        newNoti("great","yes"));
    string json = JsonConvert.SerializeObject(mes);
    Log.Error("json",json);
    OkHttpClient client = newOkHttpClient();
    RequestBody body = RequestBody.Create(
    MediaType.Parse("application/json; charset=utf-8"),json);
    Request request = newRequest.Builder()
        .Url("https://fcm.googleapis.com/fcm/send")// this is the base url which you can find it from  https://firebase.google.com/docs/cloud-messaging/http-server-ref?#params
        .Post(body)
        .AddHeader("Authorization", "your app key")// find it from this case https://stackoverflow.com/questions/37337512/where-can-i-find-the-api-key-for-firebase-cloud-messaging
        .Build();

    // Synchronous blocking call

    client.NewCall(request).Enqueue(
        (call, response) => {
        // Response came backstring body1 = response.Body().String();
        Log.Error("lv",body1);
        }, (call, exception) => {
            // There was an errorLog.Error("lv", exception.Message);
        });
}

After clicking the button, you will get a notification(if you have done with the first link), also, you don't need send a notification, you can use the data to do other things.

Update 1:

Change your MyFirebaseIIDService to this:

publicclassMyFirebaseIIDService : FirebaseInstanceIdService
{
    conststring TAG = "MyFirebaseIIDService";
   publicstaticstring token;
    publicoverridevoidOnTokenRefresh()
    {
         token = FirebaseInstanceId.Instance.Token;
        Log.Debug(TAG, "Refreshed token: " + token);
        SendRegistrationToServer(token);
    }
    voidSendRegistrationToServer(string token)
    {
        // Add custom implementation, as needed.
    }
}

And then use this:

Mesmes=newMes(MyFirebaseIIDService.token,newNoti("great","yes"));

Update 2:

Requestrequest=newRequest.Builder()
        .Url("https://fcm.googleapis.com/fcm/send")
        .Post(body)
        .AddHeader("Authorization", "key=AAAAjPn9-TY:APA91bE-g4774KmFI72V1gWATmK8uta7N7NgcufoEgGgdidU9wyWBQ5YagCjP0WPBKrgILHZSVeb1I9vegYC-YfFHE2umWWcTzjo-t7W8ynDkwbB6qHY7JZExaxxvlI3VIg3d66sFZ40")
        .Build();

This is my http request, note that AddHeader method, you need use key=....

Post a Comment for "Xamarin Android Fcm Notification Client To Client(phone To Phone)"