Xamarin android FCM 通知客户端到客户端(电话到电话) [英] Xamarin android FCM Notification Client to client(phone to phone)

查看:49
本文介绍了Xamarin android FCM 通知客户端到客户端(电话到电话)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我按照 https://docs.microsoft.com/en-us/xamarin/android/data-cloud/google-messaging/remote-notifications-with-fcm?tabs=vswin 和设法使服务器到客户端通知.

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 notification.

我在 java 中找到了一些教程和示例,但我需要在 Xamarin android 平台上制作.

I find a few tutorials and example in java but i need to make it in Xamarin android platform.

我需要制作从一部手机向一部手机发送通知的应用程序,这是客户端到客户端并且仍在使用 FCM,有人有我可以挖掘的想法或示例代码吗?

I need to make application that send notification from one phone to one phone which is client to client and still using FCM, anyone have idea or example code that i can dig in?

推荐答案

首先请关注这个一步一步完成,然后你就得到了基础应用程序.

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

二、添加发送Http到FCM服务器的按钮:

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

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

OnCreate中初始化按钮:

protected override void OnCreate(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;

}

这里我使用了两个类来生成 json 字符串,我使用的是 json.net:

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

class Mes
{
    public string to;
    public Noti notification;
    public Mes(string to,Noti notification){
        this.to = to;
        this.notification = notification;
    }
}
class Noti {
    public string title;
    public string text;
    public Noti(string body,string text) {
        this.title = body;
        this.text = body;
    }
}

点击事件(我使用的是okhttp3):

Click event(I am using okhttp3):

 private void Bt_Click(object sender, System.EventArgs e)
{

    Mes mes = new Mes("your token",
        new Noti("great","yes"));
    string json = JsonConvert.SerializeObject(mes);
    Log.Error("json",json);
    OkHttpClient client = new OkHttpClient();
    RequestBody body = RequestBody.Create(
    MediaType.Parse("application/json; charset=utf-8"),json);
    Request request = new Request.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 back
        string body1 = response.Body().String();
        Log.Error("lv",body1);
        }, (call, exception) => {
            // There was an error
            Log.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.

将您的 MyFirebaseIIDService 更改为:

public class MyFirebaseIIDService : FirebaseInstanceIdService
{
    const string TAG = "MyFirebaseIIDService";
   public static string token;
    public override void OnTokenRefresh()
    {
         token = FirebaseInstanceId.Instance.Token;
        Log.Debug(TAG, "Refreshed token: " + token);
        SendRegistrationToServer(token);
    }
    void SendRegistrationToServer(string token)
    {
        // Add custom implementation, as needed.
    }
}

然后使用这个:

Mes mes = new Mes(MyFirebaseIIDService.token,new Noti("great","yes"));

更新 2:

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

这是我的http请求,注意AddHeader方法,需要使用key=....

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

这篇关于Xamarin android FCM 通知客户端到客户端(电话到电话)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆