Skip to content

Commit

Permalink
add payload
Browse files Browse the repository at this point in the history
  • Loading branch information
黄志磊 committed Feb 17, 2016
1 parent 376c831 commit 501506c
Show file tree
Hide file tree
Showing 11 changed files with 378 additions and 24 deletions.
47 changes: 25 additions & 22 deletions mpush-api/src/main/java/com/shinemo/mpush/api/PushContent.java
Expand Up @@ -2,51 +2,54 @@


import java.io.Serializable; import java.io.Serializable;



/**
* msgId、msgType 必填
* msgType=1 :nofication,提醒。
* 必填:title,content。没有title,则为应用名称。
* 非必填。nid 通知id,主要用于聚合通知。
* content 为push message。附加的一些业务属性,都在里边。json格式
* msgType=2 :非通知消息。不在通知栏展示。
* 必填:content。
* msgType=3 :消息+提醒
* 作为一个push消息过去。和jpush不一样。jpush的消息和提醒是分开发送的。
*
*
*/
public final class PushContent implements Serializable{ public final class PushContent implements Serializable{
private static final long serialVersionUID = -1805329333995385960L; private static final long serialVersionUID = -1805329333995385960L;
public String msgId; //返回使用 private String msgId; //返回使用
public String title; private String content; //content
public String content; //content private int msgType; //type
public int msgType; //type


public PushContent(int msgType) { public PushContent(int msgType) {
this.msgType = msgType; this.msgType = msgType;
} }


public static PushContent build(int msgType){ public static PushContent build(int msgType,String content){
PushContent pushContent = new PushContent(msgType); PushContent pushContent = new PushContent(msgType);
pushContent.setContent(content);
return pushContent; return pushContent;
} }


public String getMsgId() { public String getMsgId() {
return msgId; return msgId;
} }


public String getTitle() {
return title;
}

public String getContent() {
return content;
}

public int getMsgType() { public int getMsgType() {
return msgType; return msgType;
} }


public PushContent setTitle(String title) { public void setMsgId(String msgId) {
this.title = title; this.msgId = msgId;
return this;
} }


public PushContent setContent(String content) { public String getContent() {
this.content = content; return content;
return this;
} }


public void setMsgId(String msgId) { public void setContent(String content) {
this.msgId = msgId; this.content = content;
} }



} }
@@ -0,0 +1,64 @@
package com.shinemo.mpush.api.payload;


/**
* msgId、msgType 必填
* msgType=1 :nofication,提醒。
* 必填:title,content。没有title,则为应用名称。
* 非必填。nid 通知id,主要用于聚合通知。
* content 为push message。附加的一些业务属性,都在里边。json格式
* msgType=2 :非通知消息。不在通知栏展示。
* 必填:content。
* msgType=3 :消息+提醒
* 作为一个push消息过去。和jpush不一样。jpush的消息和提醒是分开发送的。
*
*
*/
@SuppressWarnings("unchecked")
public class BasePayload<T> implements Payload{

private static final long serialVersionUID = 2367820805720853376L;
/*****以下非必填**/
private Long nid; //主要用于聚合通知,非必填
private Byte flags; //特性字段。 0x01:声音 0x02:震动 0x03:闪灯
private String largeIcon; // 大图标
private String ticker; //和title一样
private Integer number; //
public Long getNid() {
return nid;
}
public T setNid(Long nid) {
this.nid = nid;
return (T)this;
}
public Byte getFlags() {
return flags;
}
public T setFlags(Byte flags) {
this.flags = flags;
return (T)this;
}
public String getLargeIcon() {
return largeIcon;
}
public T setLargeIcon(String largeIcon) {
this.largeIcon = largeIcon;
return (T)this;
}
public String getTicker() {
return ticker;
}
public T setTicker(String ticker) {
this.ticker = ticker;
return (T)this;
}
public Integer getNumber() {
return number;
}
public T setNumber(Integer number) {
this.number = number;
return (T)this;
}

}

@@ -0,0 +1,26 @@
package com.shinemo.mpush.api.payload;

import java.util.HashMap;


/**
* msgId、msgType 必填
* msgType=1 :nofication,提醒。
* 必填:title,content。没有title,则为应用名称。
* 非必填。nid 通知id,主要用于聚合通知。
* content 为push message。附加的一些业务属性,都在里边。json格式
* msgType=2 :非通知消息。不在通知栏展示。
* 必填:content。
* msgType=3 :消息+提醒
* 作为一个push消息过去。和jpush不一样。jpush的消息和提醒是分开发送的。
*
*
*/
public class CustomPushPayload extends HashMap<String, String> implements Payload {

private static final long serialVersionUID = 6076731078625705602L;



}

@@ -0,0 +1,40 @@
package com.shinemo.mpush.api.payload;


/**
* msgId、msgType 必填
* msgType=1 :nofication,提醒。
* 必填:title,content。没有title,则为应用名称。
* 非必填。nid 通知id,主要用于聚合通知。
* content 为push message。附加的一些业务属性,都在里边。json格式
* msgType=2 :非通知消息。不在通知栏展示。
* 必填:content。
* msgType=3 :消息+提醒
* 作为一个push消息过去。和jpush不一样。jpush的消息和提醒是分开发送的。
*
*
*/
public class NotificationPushPayload extends BasePayload<NotificationPushPayload> implements Payload{

private static final long serialVersionUID = 4363667286689742483L;
private String title;
private String content;
public String getTitle() {
return title;
}
public NotificationPushPayload setTitle(String title) {
this.title = title;
return this;
}
public String getContent() {
return content;
}
public NotificationPushPayload setContent(String content) {
this.content = content;
return this;
}



}

@@ -0,0 +1,7 @@
package com.shinemo.mpush.api.payload;

import java.io.Serializable;

public interface Payload extends Serializable{

}
@@ -0,0 +1,39 @@
package com.shinemo.mpush.api.payload;


/**
* msgId、msgType 必填
* msgType=1 :nofication,提醒。
* 必填:title,content。没有title,则为应用名称。
* 非必填。nid 通知id,主要用于聚合通知。
* content 为push message。附加的一些业务属性,都在里边。json格式
* msgType=2 :非通知消息。不在通知栏展示。
* 必填:content。
* msgType=3 :消息+提醒
* 作为一个push消息过去。和jpush不一样。jpush的消息和提醒是分开发送的。
*
*
*/
@SuppressWarnings("unchecked")
public class PayloadFactory{

public static <T> T create(MessageType messageType){

Payload payload = null;
if(messageType.equals(MessageType.NOTIFICATION)){
payload = new NotificationPushPayload();
}else if(messageType.equals(MessageType.CUSTOM)){
payload = new CustomPushPayload();
}
if(payload!=null){
return (T)payload;
}
return null;
}

public enum MessageType{
NOTIFICATION,CUSTOM;
}

}

@@ -0,0 +1,67 @@
//package com.shinemo.mpush.api;
//
//import java.io.Serializable;
//
///**
// * msgId、msgType 必填
// * msgType=1 :nofication,提醒。
// * 必填:title,content。没有title,则为应用名称。
// * 非必填。nid 通知id,主要用于聚合通知。
// * content 为push message。附加的一些业务属性,都在里边。json格式
// * msgType=2 :非通知消息。不在通知栏展示。
// * 必填:content。
// * msgType=3 :消息+提醒
// * 作为一个push消息过去。和jpush不一样。jpush的消息和提醒是分开发送的。
// *
// *
// */
//public final class PushContent implements Serializable{
// private static final long serialVersionUID = -1805329333995385960L;
// private String msgId; //返回使用
// private String title; //没有title,则为应用名称。
// private String content; //content
// private int msgType; //type
//
//
//
// public PushContent(int msgType) {
// this.msgType = msgType;
// }
//
// public static PushContent build(int msgType){
// PushContent pushContent = new PushContent(msgType);
// return pushContent;
// }
//
// public String getMsgId() {
// return msgId;
// }
//
// public String getTitle() {
// return title;
// }
//
// public String getContent() {
// return content;
// }
//
// public int getMsgType() {
// return msgType;
// }
//
// public PushContent setTitle(String title) {
// this.title = title;
// return this;
// }
//
// public PushContent setContent(String content) {
// this.content = content;
// return this;
// }
//
// public void setMsgId(String msgId) {
// this.msgId = msgId;
// }
//
//
//}
63 changes: 63 additions & 0 deletions mpush-test/src/test/java/com/shinemo/mpush/test/gson/GsonTest.java
@@ -0,0 +1,63 @@
package com.shinemo.mpush.test.gson;

import java.util.Map;

import org.junit.Test;
import com.google.common.collect.Maps;
import com.google.gson.JsonObject;
import com.shinemo.mpush.api.PushContent;
import com.shinemo.mpush.tools.Jsons;

public class GsonTest {

@Test
public void test(){
Map<String,String> map = Maps.newHashMap();
map.put("key1", 1121+"");
map.put("key2", "value2");

PushContent content = PushContent.build(1,Jsons.toJson(map));


System.out.println(Jsons.toJson(content));

}

@Test
public void test2(){
ValueMap map = new ValueMap("1122", "value2");

PushContent content = PushContent.build(1,Jsons.toJson(map));


System.out.println(Jsons.toJson(content));

PushContent newContetn = Jsons.fromJson(Jsons.toJson(content), PushContent.class);

System.out.println(newContetn.getContent());


}

private static class ValueMap{

private String key1;
private String key2;

public ValueMap(String key1, String key2) {
this.key1 = key1;
this.key2 = key2;
}

public String getKey1() {
return key1;
}

public String getKey2() {
return key2;
}


}

}

0 comments on commit 501506c

Please sign in to comment.