1
我正在使用帶FCM的通知集線器向我的Android應用程序發送通知。我想爲每個通知設置消息優先級和生存時間屬性,但通知中心期望在HubClinet上的SendGcmNativeNotificationAsync方法中使用jsonpayload和標記。我不確定如何在有效負載中添加這些附加屬性。如何在通知中心設置GCM/FCM負載的生存時間
我正在使用帶FCM的通知集線器向我的Android應用程序發送通知。我想爲每個通知設置消息優先級和生存時間屬性,但通知中心期望在HubClinet上的SendGcmNativeNotificationAsync方法中使用jsonpayload和標記。我不確定如何在有效負載中添加這些附加屬性。如何在通知中心設置GCM/FCM負載的生存時間
我們可以以正確的格式在我們的自定義模型中添加這些屬性,然後將其轉換爲json有效內容。
public class GcmNotification
{
[JsonProperty("time_to_live")]
public int TimeToLiveInSeconds { get; set; }
public string Priority { get; set; }
public NotificationMessage Data { get; set; }
}
public class NotificationMessage
{
public NotificationDto Message { get; set; }
}
public class NotificationDto
{
public string Key { get; set; }
public string Value { get; set; }
}
調用sendNotification的方法ANS通過你的model.Now您可以將您的數據使用JSON轉換器轉換但要記住使用小寫設置在JsonConverter否則可能出現對設備厚望。我在LowercaseJsonSerializer類中實現了這個。
private void SendNotification(GcmNotification gcmNotification,string tag)
{
var payload = LowercaseJsonSerializer.SerializeObject(gcmNotification);
var notificationOutcome = _hubClient.SendGcmNativeNotificationAsync(payload, tag).Result;
}
public class LowercaseJsonSerializer
{
private static readonly JsonSerializerSettings Settings = new JsonSerializerSettings
{
ContractResolver = new LowercaseContractResolver()
};
public static string SerializeObject(object o)
{
return JsonConvert.SerializeObject(o,Settings);
}
public class LowercaseContractResolver : DefaultContractResolver
{
protected override string ResolvePropertyName(string propertyName)
{
return propertyName.ToLower();
}
}
}