虽然这是一个重复的问题,但我仍然没有得到确切答案。
我在Pixel 2设备上尝试这个。
- 当应用处于前台/后台时 - 获取通知
- 当应用程序从后台堆栈中滑出时,我收到一条消息“我的应用程序已停止”
- 下一次如果我尝试收到通知,请注意即将发布。没有收到停止的对话框。
如果谷歌有任何参考,请告诉我。
[Service]
[IntentFilter(new[] { "com.google.firebase.MESSAGING_EVENT" })]
public class MyFirebaseMessagingService : FirebaseMessagingService
{
public override void OnMessageReceived(RemoteMessage message)
{
CrossCurrentActivity.Current.Activity.RunOnUiThread(() => Android.Widget.Toast.MakeText(CrossCurrentActivity.Current.Activity, "Notification received", Android.Widget.ToastLength.Long).Show());
}
public override void HandleIntent(Intent intent)
{
try
{
if (intent.Extras != null)
{
var builder = new RemoteMessage.Builder(typeof(MyFirebaseMessagingService).Name);
foreach (string key in intent.Extras.KeySet())
{
builder.AddData(key, intent.Extras.Get(key).ToString());
}
this.OnMessageReceived(builder.Build());
}
else
{
base.HandleIntent(intent);
}
}
catch (Exception)
{
base.HandleIntent(intent);
}
}
}
我的有效负载
输入1
{
"message":{
"token":"<MyDevice token>",
"notification":{
"body" : "Someone has escaped the jail",
"title" : "Jail escape alert"
}
}
}
类型2
{
"message":{
"token":"<My Device token>",
"notification":{
"body" : "Someone has escaped the jail",
"title" : "Jail escape alert"
},
"data" : {
"Eventid" : "911",
"Longmessage" : "A super-secret message",
"id" : "007"
},
"android":{
"priority":"high"
}
}
}
两者都没有收到通知。 甚至Firebase文档也以相同的方式提出建议。 https://firebase.google.com/docs/cloud-messaging/concept-options