【发布时间】:2020-03-11 13:08:36
【问题描述】:
我正在尝试在单击不同的通知时重定向到不同的页面。 只有当应用程序在前台运行时,才会触发 OnMessagedRecieved。
根据文档notification foreground, background,它是被触发的系统托盘。
所以我查了一下如何让它在后台运行,我发现了这个tutorial。
按照本教程:
如下所述,通过使用 FCM 控制台,您只能发送通知消息。通知消息可以由前台应用程序中的 onMessageReceived 方法处理,并在后台应用程序中传递到设备的系统托盘。用户点击通知和默认应用程序启动器将打开。如果您想在应用程序的每个状态下处理通知,您必须使用数据消息和 onMessageReceived 方法。
所以我查找了数据消息到底是什么以及通知消息是什么。 Documentation
我按照教程进行了操作,但它对我不起作用。 这是我的代码:
public async Task<bool> WakeUp(string[] tokens)
{
var message = new Message()
{
RegistrationID= tokens,
Notification = new Notification()
{
Title = "testing",
Body = "test"
},
Android = new AndroidConfig()
{
Notification = new AndroidNotification()
{
Color = "#FF0000",
ClickAction = "MyActivity"
}
},
Data = new Dictionary<string, string>()
{
{
"notificationType", NotificationType.WakeUp.ToString()
}
}
};
return await SendAsyncMessage(message).ConfigureAwait(false);
}
public async Task<bool> SendAsyncMessage(Message message)
{
var jsonMessage = JsonConvert.SerializeObject(message);
var request = new HttpRequestMessage(HttpMethod, FIREBASE_PUSH_NOTIFICATION_URL)
{
Content = new StringContent(jsonMessage, Encoding.UTF8, "application/json")
};
request.Headers.TryAddWithoutValidation("Authorization", $"key={ConfigurationManager.AppSettings["FirebaseNotificationServerKey"]}");
request.Headers.TryAddWithoutValidation("Sender", $"id={ConfigurationManager.AppSettings["FirebaseNotificationSenderID"]}");
HttpResponseMessage result;
using (var client = new HttpClient())
{
result = await client.SendAsync(request).ConfigureAwait(false);
}
return result.IsSuccessStatusCode;
}
这就是我在应用程序中接收代码的方式
public override void OnMessageReceived(RemoteMessage message)
{
Console.WriteLine("message recieved");
}
原始 Json
{
"registration_ids":["myToken"],
"condition":null,
"data":
{
"notificationType":"WakeUp"
},
"notification":
{
"title":"testing",
"body":"test",
"image":null
},
"android":
{
"collapse_key":null,
"restricted_package_name":null,
"data":null,
"notification":
{
"title":null,
"body":null,
"icon":null,
"color":"#FF0000",
"sound":null,
"tag":null,
"image":null,
"click_action":"mActivity",
"title_loc_key":null,
"title_loc_args":null,
"body_loc_key":null,
"body_loc_args":null,
"channel_id":null
},
"fcm_options":null,
"priority":"high",
"ttl":null
},
"webpush":null,
"apns":null,
"fcm_options":null,
"topic":null
}
收到通知但未触发 OnMessageRecieved。我认为通知中心负责显示通知。
可能是什么问题?
【问题讨论】:
-
原始 json 消息是什么样子的
-
@tyczj 我编辑了页面。忘了那个抱歉
-
如您所见,
"data":null,为空,它永远不会被发送到后台 -
@tyczj 该数据是空的,但上面的数据不是。标题和正文也在一般通知中,因为我填写了数据,所以我假设 android 也可以从那里读取它。我可能错了
-
@tyczj 我将代码更改为也将数据放入空数据属性中。但是它仍然没有触发
标签: android firebase push-notification xamarin.android firebase-cloud-messaging