我正在尝试重定向到点击其他通知后显示的其他页面。当应用程序在后台运行时,仅当其在前台运行时,才会触发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");
}
Raw 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。我认为通知中心是负责显示通知的人。
可能是什么问题?
FCM(Firebase云消息传递)中有两种消息:
onMessageReceived()
回调onMessageReceived()
回调evenNOTE: Firebase团队尚未开发可将
data-messages
发送到您的设备呢。您应该使用服务器发送此类型!
要实现这一点,您必须对以下URL执行POST
请求:
Content-Type
,值: application/json
Authorization
,值: key=<your-server-key>
{
"to": "/topics/my_topic",
"data": {
"my_custom_key": "my_custom_value",
"my_custom_key2": true
}
}
{
"data": {
"my_custom_key": "my_custom_value",
"my_custom_key2": true
},
"registration_ids": ["{device-token}","{device2-token}","{device3-token}"]
}
NOTE:确保您未添加 JSON密钥
notification
注意:要获取服务器密钥,您可以在firebase控制台中找到它:Your project -> settings -> Project settings -> Cloud messaging -> Server Key
这是您处理收到的消息的方式:
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Map<String, String> data = remoteMessage.getData();
String myCustomKey = data.get("my_custom_key");
// Manage data
}
这是指here