mob_push/push_notify/android/
notify_style.rs

1use serde::{ser::SerializeStruct, Serialize};
2use typed_builder::TypedBuilder;
3
4use crate::push_notify::NotifySerialize;
5
6#[derive(Debug, Clone)]
7pub enum NotifyStyle {
8    Default,
9    LongContent(String),
10    BigVision(String),
11    Banner(Vec<String>),
12    Custom(CustomStyle),
13}
14
15impl NotifyStyle {
16    /// 创建一个 **默认样式**  的推送消息格式
17    pub fn new_default() -> Self {
18        Self::Default
19    }
20    /// 创建一个 **长文本** 的推送消息格式  
21    ///
22    /// 该推送模式将会使原有Content隐藏  
23    pub fn new_long_content(content: impl Into<String>) -> Self {
24        Self::LongContent(content.into())
25    }
26
27    /// 创建一个 **大图** 的推送消息格式
28    ///  
29    /// 该推送方法会同时保留图片和content  
30    pub fn new_big_vision(image_url: impl Into<String>) -> Self {
31        Self::BigVision(image_url.into())
32    }
33
34    /// 创建一个 **多行横幅** 的推送消息格式  
35    ///
36    /// 迭代器中每一元素一行
37    ///   
38    /// 该推送模式将会使原有Content隐藏  
39    pub fn new_banner<I, T>(contents: I) -> Self
40    where
41        T: Into<String>,
42        I: IntoIterator<Item = T>,
43    {
44        Self::Banner(contents.into_iter().map(Into::into).collect())
45    }
46    /// 创建一个 **用户定义** 的推送消息格式  
47    pub fn new_custom(custom_style: CustomStyle) -> Self {
48        Self::Custom(custom_style)
49    }
50}
51
52impl NotifyStyle {
53    fn get_code(&self) -> i32 {
54        match self {
55            NotifyStyle::Default => 0,
56            NotifyStyle::LongContent(_) => 1,
57            NotifyStyle::BigVision(_) => 2,
58            NotifyStyle::Banner(_) => 3,
59            NotifyStyle::Custom(_) => 4,
60        }
61    }
62}
63
64#[derive(Debug, Clone)]
65pub enum StyleId {
66    // 1 样式1
67    One,
68    // 2 样式2
69    Tow,
70    // 3 样式3
71    Three,
72}
73
74impl Serialize for StyleId {
75    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
76    where
77        S: serde::Serializer,
78    {
79        serializer.serialize_i32(self.to_code())
80    }
81}
82
83impl StyleId {
84    fn to_code(&self) -> i32 {
85        match self {
86            StyleId::One => 1,
87            StyleId::Tow => 2,
88            StyleId::Three => 3,
89        }
90    }
91}
92
93#[derive(Debug, Serialize, TypedBuilder, Clone)]
94#[builder(field_defaults(default, setter(strip_option)))]
95pub struct CustomStyle {
96    /// 样式序号
97    #[serde(rename = "styleNo", skip_serializing_if = "Option::is_none")]
98    style: Option<StyleId>,
99
100    /// 背景图Url
101    #[serde(rename = "backgroundUrl", skip_serializing_if = "Option::is_none")]
102    background_url: Option<String>,
103
104    ///小图标
105    #[serde(rename = "smallIcons", skip_serializing_if = "Option::is_none")]
106    small_icons: Option<String>,
107
108    ///按钮文案
109    #[serde(rename = "buttonCopy", skip_serializing_if = "Option::is_none")]
110    button_copy: Option<String>,
111
112    ///点击按钮跳转的链接
113    #[serde(rename = "buttonJumpUrl", skip_serializing_if = "Option::is_none")]
114    button_jump_url: Option<String>,
115}
116
117impl NotifySerialize for NotifyStyle {
118    fn serialize_field(&self) -> usize {
119        match self {
120            NotifyStyle::Default => 1,
121            NotifyStyle::LongContent(_)
122            | NotifyStyle::BigVision(_)
123            | NotifyStyle::Banner(_)
124            | NotifyStyle::Custom(_) => 2,
125        }
126    }
127
128    fn serialize<S: serde::Serializer>(
129        &self,
130        serialize_struct: &mut <S as serde::Serializer>::SerializeStruct,
131    ) -> Result<(), <S as serde::Serializer>::Error> {
132        serialize_struct.serialize_field("style", &self.get_code())?;
133        match self {
134            NotifyStyle::Default => Ok(()),
135            NotifyStyle::LongContent(s) | NotifyStyle::BigVision(s) => {
136                serialize_struct.serialize_field("content", &[s])
137            }
138            NotifyStyle::Banner(info_vec) => serialize_struct.serialize_field("content", info_vec),
139            NotifyStyle::Custom(inner) => serialize_struct.serialize_field("customStyle", inner),
140        }
141    }
142}