admin
2022-01-12 4a7367a869ef12375ea6678ca44e102b8919c624
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package cn.jpush.api.push.model.notification;
 
import java.util.Map;
 
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
 
import cn.jiguang.common.ServiceHelper;
 
/**
 * <p><b>APNs 通知类</b></p>
 * <br>
 * 支持 APNs 默认的几个参数:
 * <ul>
 * <li>alert: 继承自父类 PlatformNotification 的 alert 属性;本类设置则覆盖。</li>
 * <li>badge: 支持 setBadge(int) 方法来设置;支持 incrBadge(int) 方法来增加。</li>
 * <li>sound: 支持 setSound(string) 方法来设置声音文件。或者 setSound(JSON object) 对应官方payload结构 </li>
 * <li>content-available: 用来支持后台推送。如果该值赋值为 1,表示开启后台推送。</li>
 * <li>mutable-content: 通知扩展</li>
 * <li>category: IOS 8 才支持。设置 APNs payload 中的 "category" 字段值</li>
 * <li>mutable-content: 通知扩展</li>
 * <li>extras: JSON object. 支持更多的自定义字段信息。</li>
 * </ul>
 * <br>
 * 需要特别留意的是,JPush SDK 会对以下几个值有特别的默认设置考虑:
 * <ul>
 * <li>badge: 默认为 "+1"。如果需要取消 badge 值,需要显式地调用 disableBadge()。</li>
 * <li>sound: 默认为 "",即默认的声音提示。如果需要取消 sound 值,即不要声音,需要显式地调用 disableSound()。</li>
 * </ul>
 */
public class IosNotification extends PlatformNotification {
    public static final String NOTIFICATION_IOS = "ios";
        
    private static final String DEFAULT_SOUND = "";
    private static final String DEFAULT_BADGE = "+1";
    
    private static final String BADGE = "badge";
    private static final String SOUND = "sound";
    private static final String CONTENT_AVAILABLE = "content-available";
    private static final String MUTABLE_CONTENT = "mutable-content";
    private static final String CATEGORY = "category";
    private static final String THREAD_ID = "thread-id";
    
    private static final String ALERT_VALID_BADGE = "Badge number should be 0~99999, "
            + "and can be prefixed with + to add, - to minus";
 
    
    private final boolean soundDisabled;
    private final boolean badgeDisabled;
    private final Object sound;
    private final String badge;
    private final boolean contentAvailable;
    private final String category;
    private final boolean mutableContent;
    private final String threadId;
 
    private IosNotification(Object alert, Object sound, String badge,
            boolean contentAvailable, boolean soundDisabled, boolean badgeDisabled, 
            String category, boolean mutableContent,String threadId,
            Map<String, String> extras, 
            Map<String, Number> numberExtras, 
            Map<String, Boolean> booleanExtras, 
            Map<String, JsonObject> jsonExtras,
            Map<String, JsonPrimitive> customData) {
        super(alert, extras, numberExtras, booleanExtras, jsonExtras, customData);
        
        this.sound = sound;
        this.badge = badge;
        this.contentAvailable = contentAvailable;
        this.soundDisabled = soundDisabled;
        this.badgeDisabled = badgeDisabled;
        this.category = category;
        this.mutableContent = mutableContent;
        this.threadId = threadId;
    }
    
    public static Builder newBuilder() {
        return new Builder();
    }
    
    public static IosNotification alert(String alert) {
        return newBuilder().setAlert(alert).build();
    }
    
    
    @Override
    public String getPlatform() {
        return NOTIFICATION_IOS;
    }
    
    @Override
    public JsonElement toJSON() {
        JsonObject json = super.toJSON().getAsJsonObject();
        
        if (!badgeDisabled) {
            if (null != badge) {
                json.add(BADGE, new JsonPrimitive(this.badge));
            } else {
                json.add(BADGE, new JsonPrimitive(DEFAULT_BADGE));
            }
        }
        if (!soundDisabled) {
            if (null != sound) {
                if(sound instanceof String){
                    json.add(SOUND, new JsonPrimitive((String)sound));
                }else if (sound instanceof JsonObject) {
                    json.add(SOUND,  (JsonObject) sound);
                }
                
            } else {
                json.add(SOUND, new JsonPrimitive(DEFAULT_SOUND));
            }
        }
        if (contentAvailable) {
            json.add(CONTENT_AVAILABLE, new JsonPrimitive(true));
        }
        if (null != category) {
            json.add(CATEGORY, new JsonPrimitive(category));
        }
        if (mutableContent) {
            json.add(MUTABLE_CONTENT, new JsonPrimitive(true));
        }
        if (null != threadId) {
            json.add(THREAD_ID, new JsonPrimitive(threadId));
        }
 
        return json;
    }
    
    
    public static class Builder extends PlatformNotification.Builder<IosNotification, Builder> {
        private Object sound;
        private String badge;
        private boolean contentAvailable = false;
        private boolean soundDisabled = false;
        private boolean badgeDisabled = false;
        private String category;
        private boolean mutableContent;
        private String threadId;
 
        @Override
        protected Builder getThis() {
            return this;
        }
        
        public Builder setSound(Object sound) {
            if (null == sound) {
                LOG.warn("Null sound. Throw away it.");
                return this;
            }
            this.sound = sound;
            return this;
        }
        
        public Builder disableSound() {
            this.soundDisabled = true;
            return this;
        }
        
        public Builder incrBadge(int badge) {
            if (!ServiceHelper.isValidIntBadge(Math.abs(badge))) {
                LOG.warn(ALERT_VALID_BADGE);
                return this;
            }
            
            if (badge >= 0) {
                this.badge = "+" + badge;
            } else {
                this.badge = "" + badge;
            }
            return this;
        }
        
        public Builder setBadge(int badge) {
            if (!ServiceHelper.isValidIntBadge(badge)) {
                LOG.warn(ALERT_VALID_BADGE);
                return this;
            }
            this.badge = "" + badge;
            return this;
        }
        
        /**
         * equals to: +1
         * @return IosNotification builder
         */
        public Builder autoBadge() {
            return incrBadge(1);
        }
        
        public Builder disableBadge() {
            this.badgeDisabled = true;
            return this;
        }
        
        public Builder setContentAvailable(boolean contentAvailable) {
            this.contentAvailable = contentAvailable;
            return this;
        }
        
        public Builder setCategory(String category) {
            this.category = category;
            return this;
        }
        
        @Override
        public Builder setAlert(Object alert) {
            this.alert = alert;
            return this;
        }
 
        public Builder setMutableContent(boolean mutableContent) {
            this.mutableContent = mutableContent;
            return this;
        }
        
        public Builder setThreadId(String threadId) {
            this.threadId = threadId;
            return this;
        }
 
 
        public IosNotification build() {
            return new IosNotification(alert, sound, badge, contentAvailable, 
                    soundDisabled, badgeDisabled, category, mutableContent, threadId,
                    extrasBuilder, numberExtrasBuilder, booleanExtrasBuilder, jsonExtrasBuilder, super.customData);
        }
    }
}