我不久前解决了我的问题,因为我看到没有其他人回答,我会发布我的解决方案,以防其他人有此问题:
失败就这么简单
未收到消息
从不在后台调用。我通过使用
手部内容
相反,这使得我的后台服务变得不必要,因为:
(班级相同)
:
public override void HandleIntent(Intent intent)
{
string msg = intent.GetStringExtra("body");
if (msg == null) msg = "New Notification";
ShortcutBadger.ApplyCount(this, Global.countNotifications());
sendNotification(msg);
}
额外注释
HandleIntent不能使用RemoteMessage类型,因此(如果我没有弄错的话)它无法检索通知的正文。
A.
自定义通知发件人
为了能够以json的形式手动检索主体,需要。
我就是这样建立我的:
:
public static string signalFCM(string msg, string user_firebase_id) //This must be gathered by storing a firebase_id created by an android device.
{
try
{
WebRequest tRequest = WebRequest.Create("https://fcm.googleapis.com/fcm/send");
tRequest.Method = "post";
tRequest.ContentType = "application/json";
var data = new
{
to = user_firebase_id,
notification = new // *** THIS IS HOW FIREBASE SENDS THEM BY DEFAULT
{
body = msg,
title = "MyAPP",
sound = "Enabled"
},
// **** THIS IS THE CUSTOM EXTRA TO RETRIEVE ITS CONTENT ****
data = new
{
body = msg,
title = "MyAPP",
payload = "1"
}
// ******************
};
var serializer = new JavaScriptSerializer();
var json = serializer.Serialize(data);
Byte[] byteArray = Encoding.UTF8.GetBytes(json);
tRequest.Headers.Add(string.Format("Authorization: key={0}", APP_KEY)); //This must be gathered from the Firebase Console APP page
tRequest.Headers.Add(string.Format("Sender: id={0}", APP_ID)); //This must be gathered from the Firebase Console APP page
tRequest.ContentLength = byteArray.Length;
using (Stream dataStream = tRequest.GetRequestStream())
{
dataStream.Write(byteArray, 0, byteArray.Length);
using (WebResponse tResponse = tRequest.GetResponse())
{
using (Stream dataStreamResponse = tResponse.GetResponseStream())
{
using (StreamReader tReader = new StreamReader(dataStreamResponse))
{
String sResponseFromServer = tReader.ReadToEnd();
return sResponseFromServer;
}
}
}
}
}
catch (Exception ex) { return ex.Message; }
}
}