标签:
<receiver
android:name=".JpushReceiver"
android:exported="false"
android:enabled="true">
<intent-filter>
<action android:name="cn.jpush.android.intent.REGISTRATION" />
<action android:name="cn.jpush.android.intent.MESSAGE_RECEIVED" />
<action android:name="cn.jpush.android.intent.NOTIFICATION_RECEIVED" />
<action android:name="cn.jpush.android.intent.NOTIFICATION_OPENED" />
<action android:name="cn.jpush.android.intent.ACTION_RICHPUSH_CALLBACK" />
<action android:name="cn.jpush.android.intent.CONNECTION" />
<category android:name="com.example.exmpushjpush" />
</intent-filter>
</receiver>import cn.jpush.api.JPushClient;
import cn.jpush.api.common.ClientConfig;
import cn.jpush.api.common.resp.APIConnectionException;
import cn.jpush.api.common.resp.APIRequestException;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Message;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.Notification;
import java.util.Map;
import java.util.Set;
public class MessagePush {
public static long IOS = 0L;
public static long Android = 1L;
private String mTitle;
private String mMessage;
private long mPlatformType;
private JPushClient mJpushClient;
private Platform mPlatform;
private Audience mAudience;
private Notification mNotify;
private Message mMsg;
public long mMsgId;
public String mStatus;
public String mErrCode;
public String mErrMsg;
public MessagePush(String appKey, String masterSecret, String message) {
ClientConfig conf = ClientConfig.getInstance();
conf.setMaxRetryTimes(3);
mJpushClient = new JPushClient(masterSecret, appKey, null, conf);
mMessage = message;
mTitle = "";
mPlatform = Platform.all();
mMsg = Message.content(message);
mAudience = Audience.all();
setMsg(-1L, 0, 0, "");
}
public MessagePush(String appKey, String masterSecret, String message, String title) {
this(appKey, masterSecret, message);
mTitle = title;
}
public MessagePush(String appKey, String masterSecret, String message,
String title, Long platformType, Map<String, String> extras) {
this(appKey, masterSecret, message, title);
mPlatformType = platformType.longValue();
System.out.println("MessagePush platformType=" + platformType);
if (platformType.longValue() == IOS) {
mPlatform = Platform.ios();
mNotify = Notification.ios(mMessage, extras);
} else if (platformType.longValue() == Android) {
mPlatform = Platform.android();
mNotify = Notification.android(mMessage, mTitle, extras);
} else {
mPlatform = Platform.winphone();
mNotify = Notification.winphone(mMessage, extras);
}
}
public void setAlias(Set<String> alias) {
mAudience = Audience.alias(alias);
}
public void setTag(String[] tag) {
mAudience = Audience.tag(tag);
}
public void sendPush() {
PushPayload payload = build();
try {
PushResult result = mJpushClient.sendPush(payload);
mMsgId = result.msg_id;
System.out.println("Got result - " + result);
} catch (APIConnectionException e) {
System.out.println("Connection error. Should retry later. "
+ e.getMessage());
setMsg(-1L, -1, -1, e.getMessage());
} catch (APIRequestException e) {
System.out.println("HTTP Status: " + e.getStatus());
System.out.println("Error Code: " + e.getErrorCode());
System.out.println("Error Message: " + e.getErrorMessage());
System.out.println("Msg ID: " + e.getMsgId());
setMsg(e.getMsgId(), e.getStatus(), e.getErrorCode(),
e.getErrorMessage());
}
}
private void setMsg(long msgId, int status, int errCode, String errMsg) {
mMsgId = msgId;
mStatus = String.valueOf(status);
mErrCode = String.valueOf(errCode);
mErrMsg = errMsg;
}
private PushPayload build() {
System.out.println("build platformType=" + mPlatformType);
PushPayload push;
if (mPlatformType == IOS) {
push = PushPayload.newBuilder().setPlatform(mPlatform)
.setAudience(mAudience).setNotification(mNotify)
.build();
} else if (mPlatformType == Android) {
push = PushPayload.newBuilder().setPlatform(mPlatform)
.setAudience(mAudience).setMessage(mMsg).build();
//.setAudience(mAudience).setNotification(mNotify).build();
} else {
push = PushPayload.newBuilder().setPlatform(mPlatform)
.setAudience(mAudience).setMessage(mMsg).build();
}
return push;
}
} <receiver
android:name=".GexinReceiver"
android:exported="false" >
<intent-filter>
<action android:name="com.igexin.sdk.action.应用分配的AppID值" />
</intent-filter>
</receiver>import java.util.ArrayList;
import java.util.List;
import com.gexin.rp.sdk.base.IPushResult;
import com.gexin.rp.sdk.base.impl.AppMessage;
import com.gexin.rp.sdk.base.uitls.AppConditions;
import com.gexin.rp.sdk.http.IGtPush;
import com.gexin.rp.sdk.template.NotificationTemplate;
public class PushtoAppNotify {
//采用"Java SDK 快速入门", "第二步 获取访问凭证 "中获得的应用配置,用户可以自行替换
private static String appId = "FJ9uNM6WkS8laiS3C05W9";
private static String appKey = "cpV7gRK6IlAo26aDZGMtI1";
private static String masterSecret = "QSvOwGnx0E9jEMpiXtqJ39";
static String host = "http://sdk.open.api.igexin.com/apiex.htm";
public static void main(String[] args) throws Exception {
IGtPush push = new IGtPush(host, appKey, masterSecret);
NotificationTemplate template = NotificationTemplateDemo();
AppMessage message = new AppMessage();
message.setData(template);
message.setOffline(true);
//离线有效时间,单位为毫秒,可选
message.setOfflineExpireTime(24 * 1000 * 3600);
//推送给App的目标用户需要满足的条件
AppConditions cdt = new AppConditions();
List<String> appIdList = new ArrayList<String>();
appIdList.add(appId);
message.setAppIdList(appIdList);
//手机类型
List<String> phoneTypeList = new ArrayList<String>();
//省份
List<String> provinceList = new ArrayList<String>();
//自定义tag
List<String> tagList = new ArrayList<String>();
cdt.addCondition(AppConditions.PHONE_TYPE, phoneTypeList);
cdt.addCondition(AppConditions.REGION, provinceList);
cdt.addCondition(AppConditions.TAG,tagList);
message.setConditions(cdt);
IPushResult ret = push.pushMessageToApp(message,"任务别名_toApp");
System.out.println(ret.getResponse().toString());
}
public static NotificationTemplate NotificationTemplateDemo() throws Exception {
NotificationTemplate template = new NotificationTemplate();
template.setAppId(appId);
template.setAppkey(appKey);
template.setTitle("PushtoAppNotify标题");
template.setText("PushtoAppNotify内容");
template.setLogo("icon.png");
template.setLogoUrl("");
template.setIsRing(true);
template.setIsVibrate(true);
template.setIsClearable(true);
// 透传消息设置,1为强制启动应用,客户端接收到消息后就会立即启动应用;2为等待应用启动
template.setTransmissionType(1);
template.setTransmissionContent("PushtoAppNotify请输入您要透传的内容");
return template;
}
public static class NotificationTemplateDemo {
public static NotificationTemplate notificationTemplateDemo(String appId, String appkey) {
NotificationTemplate template = new NotificationTemplate();
// 设置APPID与APPKEY
template.setAppId(appId);
template.setAppkey(appkey);
// 设置通知栏标题与内容
template.setTitle("请输入通知栏标题");
template.setText("请输入通知栏内容");
// 配置通知栏图标
template.setLogo("icon.png");
// 配置通知栏网络图标
template.setLogoUrl("");
// 设置通知是否响铃,震动,或者可清除
template.setIsRing(true);
template.setIsVibrate(true);
template.setIsClearable(true);
// 透传消息设置,1为强制启动应用,客户端接收到消息后就会立即启动应用;2为等待应用启动
template.setTransmissionType(1);
template.setTransmissionContent("请输入您要透传的内容");
// 设置定时展示时间
// template.setDuration("2015-01-16 11:40:00", "2015-01-16 12:24:00");
return template;
}
}
}标签:
原文地址:http://blog.csdn.net/aqi00/article/details/51593839