极光推送使用:
极光推送的流程:Android客户端初始化极光的客户端sdk,给用户打上tag标签,此标签会发送给极光的服务器,
自己的服务器,调用 PushResult result = jpushClient.sendPush(payload);这个方法,将需要推送的内容发生给Android客户端。
其实并不是自己的服务器发送,而是自己的服务器先经过极光的服务器,极光的服务器根据tag标签,将你的内容中转给android客户端。
下面搭建一个springmvc的测试框架:和一个Android的客户端,通过浏览器去请求自己的服务器,然后给Android客户端进行推送的过程。
服务器端代码如下:
@Controller
@RequestMapping("/push")
public class PushController {
public Log LOG = LogFactory.getLog(this.getClass());
private static final String appKey ="9367ad09127755c5136bbbb4";
private static final String masterSecret = "dc4ab6bb3743343fbf5baf6f";
//发送给所有的客户端
@RequestMapping(value = ("/sendAll"),method={RequestMethod.POST,RequestMethod.GET})
public String sendAll(String msg){
ClientConfig clientConfig = ClientConfig.getInstance();
JPushClient jpushClient = new JPushClient(masterSecret, appKey, null, clientConfig);
LOG.info("msg:"+msg);
// For push, all you need do is to build PushPayload object.
PushPayload payload = buildPushObject_android_tag_alertWithTitle();
// PushPayload payload = buildPushObject_all_all_alert();
try {
PushResult result = jpushClient.sendPush(payload);
LOG.info("Got result - " + result);
} catch (APIConnectionException e) {
LOG.error("Connection error. Should retry later. ", e);
} catch (APIRequestException e) {
LOG.error("Error response from JPush server. Should review and fix it. ", e);
LOG.info("HTTP Status: " + e.getStatus());
LOG.info("Error Code: " + e.getErrorCode());
LOG.info("Error Message: " + e.getErrorMessage());
LOG.info("Msg ID: " + e.getMsgId());
}
return JsonUtil.toJson("success");
}
//带有alieas的发送
public static PushPayload buildPushObject_all_alias_alert() {
return PushPayload.newBuilder()
.setPlatform(Platform.all())
.setAudience(Audience.alias("alias1"))
.setNotification(Notification.alert("alert"))
.build();
}
//带有tag的信息发送
public static PushPayload buildPushObject_android_tag_alertWithTitle() {
return PushPayload.newBuilder()
.setPlatform(Platform.android())
.setAudience(Audience.tag("1234"))
.setNotification(Notification.android("alert", "title", null))
.build();
}
//发送信息给客户端 带有额外的信息
public static void buildPushObject_with_extra() {
JsonObject jsonExtra = new JsonObject();
jsonExtra.addProperty("extra1", 1);
jsonExtra.addProperty("extra2", false);
Map extras = new HashMap();
extras.put("extra_1", "val1");
extras.put("extra_2", "val2");
PushPayload payload = PushPayload.newBuilder()
.setPlatform(Platform.android_ios())
.setAudience(Audience.tag("tag1"))
.setNotification(Notification.newBuilder()
.setAlert("alert content")
.addPlatformNotification(AndroidNotification.newBuilder()
.setTitle("Android Title")
.addExtras(extras)
.addExtra("booleanExtra", false)
.addExtra("numberExtra", 1)
.addExtra("jsonExtra", jsonExtra)
.build()).build()).build();
System.out.println(payload.toJSON());
}
public static PushPayload buildPushObject_all_all_alert() {
return PushPayload.alertAll("this is a alert info");
}
}
android客户端配置:参考极光的demo
建立一个自己的广播接收器
public class JupshReciver extends BroadcastReceiver {
private static final String TAG = "JPush";
@Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
Log.d(TAG, "[MyReceiver] onReceive - " + intent.getAction() + ", extras: " + printBundle(bundle));
if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) {
String regId = bundle.getString(JPushInterface.EXTRA_REGISTRATION_ID);
Log.d(TAG, "[MyReceiver] 接收Registration Id : " + regId);
//send the Registration Id to your server...
} else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {
Log.d(TAG, "[MyReceiver] 接收到推送下来的自定义消息: " + bundle.getString(JPushInterface.EXTRA_MESSAGE));
//processCustomMessage(context, bundle);
} else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {
Log.d(TAG, "[MyReceiver] 接收到推送下来的通知");
int notifactionId = bundle.getInt(JPushInterface.EXTRA_NOTIFICATION_ID);
Log.d(TAG, "[MyReceiver] 接收到推送下来的通知的ID: " + notifactionId);
} else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {
Log.d(TAG, "[MyReceiver] 用户点击打开了通知");
//打开自定义的Activity
Intent i = new Intent(context, TestActivity.class);
i.putExtras(bundle);
//i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP );
context.startActivity(i);
} else if (JPushInterface.ACTION_RICHPUSH_CALLBACK.equals(intent.getAction())) {
Log.d(TAG, "[MyReceiver] 用户收到到RICH PUSH CALLBACK: " + bundle.getString(JPushInterface.EXTRA_EXTRA));
//在这里根据 JPushInterface.EXTRA_EXTRA 的内容处理代码,比如打开新的Activity, 打开一个网页等..
} else if(JPushInterface.ACTION_CONNECTION_CHANGE.equals(intent.getAction())) {
boolean connected = intent.getBooleanExtra(JPushInterface.EXTRA_CONNECTION_CHANGE, false);
Log.w(TAG, "[MyReceiver]" + intent.getAction() +" connected state change to "+connected);
} else {
Log.d(TAG, "[MyReceiver] Unhandled intent - " + intent.getAction());
}
}
// 打印所有的 intent extra 数据
private static String printBundle(Bundle bundle) {
StringBuilder sb = new StringBuilder();
for (String key : bundle.keySet()) {
if (key.equals(JPushInterface.EXTRA_NOTIFICATION_ID)) {
sb.append("\nkey:" + key + ", value:" + bundle.getInt(key));
}else if(key.equals(JPushInterface.EXTRA_CONNECTION_CHANGE)){
sb.append("\nkey:" + key + ", value:" + bundle.getBoolean(key));
} else if (key.equals(JPushInterface.EXTRA_EXTRA)) {
if (bundle.getString(JPushInterface.EXTRA_EXTRA).isEmpty()) {
Log.i(TAG, "This message has no Extra data");
continue;
}
try {
JSONObject json = new JSONObject(bundle.getString(JPushInterface.EXTRA_EXTRA));
Iterator<String> it = json.keys();
while (it.hasNext()) {
String myKey = it.next().toString();
sb.append("\nkey:" + key + ", value: [" +
myKey + " - " +json.optString(myKey) + "]");
}
} catch (JSONException e) {
Log.e(TAG, "Get message extra JSON error!");
}
} else {
sb.append("\nkey:" + key + ", value:" + bundle.getString(key));
}
}
return sb.toString();
}
//send msg to MainActivity
// private void processCustomMessage(Context context, Bundle bundle) {
// if (MainActivity.isForeground) {
// String message = bundle.getString(JPushInterface.EXTRA_MESSAGE);
// String extras = bundle.getString(JPushInterface.EXTRA_EXTRA);
// Intent msgIntent = new Intent(MainActivity.MESSAGE_RECEIVED_ACTION);
// msgIntent.putExtra(MainActivity.KEY_MESSAGE, message);
// if (!JupshUtil.isEmpty(extras)) {
// try {
// JSONObject extraJson = new JSONObject(extras);
// if (null != extraJson && extraJson.length() > 0) {
// msgIntent.putExtra(MainActivity.KEY_EXTRAS, extras);
// }
// } catch (JSONException e) {
//
// }
//
// }
// context.sendBroadcast(msgIntent);
// }
// }
详细代码地址:
http://pan.baidu.com/s/1dF6z19f
android客户端 eclipse
服务器端 idea