|
@@ -1,15 +1,18 @@
|
|
|
package com.chelvc.framework.jpush.context;
|
|
|
|
|
|
import java.util.Collection;
|
|
|
+import java.util.Collections;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
import java.util.Objects;
|
|
|
+import java.util.function.Function;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
import cn.jpush.api.push.model.Message;
|
|
|
import cn.jpush.api.push.model.Options;
|
|
|
import cn.jpush.api.push.model.PushPayload;
|
|
|
import cn.jpush.api.push.model.audience.Audience;
|
|
|
+import cn.jpush.api.push.model.notification.AndroidNotification;
|
|
|
import cn.jpush.api.push.model.notification.IosAlert;
|
|
|
import cn.jpush.api.push.model.notification.IosNotification;
|
|
|
import cn.jpush.api.push.model.notification.Notification;
|
|
@@ -87,6 +90,19 @@ public final class JPushContextHolder {
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 构建通知对象
|
|
|
+ *
|
|
|
+ * @param platform 平台信息
|
|
|
+ * @param title 通知标题
|
|
|
+ * @param content 通知内容
|
|
|
+ * @return 通知对象实例
|
|
|
+ */
|
|
|
+ public static Notification notification(@NonNull com.chelvc.framework.common.model.Platform platform,
|
|
|
+ @NonNull String title, @NonNull String content) {
|
|
|
+ return notification(platform, title, content, Collections.emptyMap());
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 构建通知对象
|
|
|
*
|
|
@@ -97,24 +113,69 @@ public final class JPushContextHolder {
|
|
|
* @return 通知对象实例
|
|
|
*/
|
|
|
public static Notification notification(@NonNull com.chelvc.framework.common.model.Platform platform,
|
|
|
- String title,
|
|
|
- String content, Map<String, String> extras) {
|
|
|
+ @NonNull String title, @NonNull String content,
|
|
|
+ @NonNull Map<String, String> extras) {
|
|
|
+ return notification(platform, title, content, extras, builder -> builder, builder -> builder);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 构建通知对象
|
|
|
+ *
|
|
|
+ * @param platform 平台信息
|
|
|
+ * @param title 通知标题
|
|
|
+ * @param content 通知内容
|
|
|
+ * @param ios 苹果通知构造回调函数
|
|
|
+ * @param android 安卓通知构造回调函数
|
|
|
+ * @return 通知对象实例
|
|
|
+ */
|
|
|
+ public static Notification notification(
|
|
|
+ @NonNull com.chelvc.framework.common.model.Platform platform,
|
|
|
+ @NonNull String title, @NonNull String content,
|
|
|
+ @NonNull Function<IosNotification.Builder, IosNotification.Builder> ios,
|
|
|
+ @NonNull Function<AndroidNotification.Builder, AndroidNotification.Builder> android) {
|
|
|
+ return notification(platform, title, content, Collections.emptyMap(), ios, android);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 构建通知对象
|
|
|
+ *
|
|
|
+ * @param platform 平台信息
|
|
|
+ * @param title 通知标题
|
|
|
+ * @param content 通知内容
|
|
|
+ * @param extras 扩展信息
|
|
|
+ * @param ios 苹果通知构造回调函数
|
|
|
+ * @param android 安卓通知构造回调函数
|
|
|
+ * @return 通知对象实例
|
|
|
+ */
|
|
|
+ public static Notification notification(
|
|
|
+ @NonNull com.chelvc.framework.common.model.Platform platform,
|
|
|
+ @NonNull String title, @NonNull String content, @NonNull Map<String, String> extras,
|
|
|
+ @NonNull Function<IosNotification.Builder, IosNotification.Builder> ios,
|
|
|
+ @NonNull Function<AndroidNotification.Builder, AndroidNotification.Builder> android) {
|
|
|
if (platform == com.chelvc.framework.common.model.Platform.IOS) {
|
|
|
- return Notification.newBuilder().addPlatformNotification(IosNotification.newBuilder()
|
|
|
- .addExtras(extras)
|
|
|
+ IosNotification.Builder builder = IosNotification.newBuilder()
|
|
|
//设置ios标题
|
|
|
.setAlert(IosAlert.newBuilder().setTitleAndBody(title, null, content).build())
|
|
|
+ // 设置扩展参数
|
|
|
+ .addExtras(extras)
|
|
|
//如果要设置右侧大图,推送的时候携带 "mutable-content":true 说明是支持 iOS10 的 UNNotificationServiceExtension
|
|
|
.setMutableContent(true)
|
|
|
//如果不需要自定义声音,推送的时候,iOS 的 sound 值保持默认,传 default ,不要传空!
|
|
|
.setSound("default")
|
|
|
//设置角标自动+1
|
|
|
- .incrBadge(1)
|
|
|
- .build()).build();
|
|
|
+ .incrBadge(1);
|
|
|
+ return Notification.newBuilder().addPlatformNotification(ios.apply(builder).build()).build();
|
|
|
} else if (platform == com.chelvc.framework.common.model.Platform.ANDROID) {
|
|
|
- return Notification.android(content, title, extras);
|
|
|
+ AndroidNotification.Builder builder = AndroidNotification.newBuilder()
|
|
|
+ // 设置标题
|
|
|
+ .setTitle(title)
|
|
|
+ // 设置通知内容
|
|
|
+ .setAlert(content)
|
|
|
+ // 设置扩展参数
|
|
|
+ .addExtras(extras);
|
|
|
+ return Notification.newBuilder().addPlatformNotification(android.apply(builder).build()).build();
|
|
|
}
|
|
|
- throw new IllegalArgumentException("Not support platform: " + platform.name());
|
|
|
+ throw new UnsupportedOperationException("Not support platform: " + platform.name());
|
|
|
}
|
|
|
|
|
|
/**
|