我按照https://docs.microsoft.com/en-us/xamarin/android/data-cloud/google-messaging/remote-notifications-with-fcm?tabs=vswin中的教程设法制作服务器到客户端的通知。
我在java中找到了一些教程和示例,但我需要在Xamarin android平台上进行。
我需要制作应用程序,将通知从一部手机发送到一部手机,客户端到客户端仍在使用FCM,任何人都有我可以挖掘的想法或示例代码?
答案 0 :(得分:0)
首先,请按照this一步一步完成,然后您将获得基本应用程序。
其次,添加一个按钮将Http发送到FCM服务器:
<?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:
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):
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);
});
}
点击按钮后,您会收到通知(如果您已完成第一个链接),此外,您不需要发送通知,您可以使用该数据执行其他操作。
将您的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"));
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=...
。