java消息推送 友盟

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_39179428/article/details/82389547
package com.push;

import org.json.JSONArray;
import org.json.JSONObject;

import com.push.android.AndroidBroadcast;
import com.push.android.AndroidCustomizedcast;
import com.push.android.AndroidFilecast;
import com.push.android.AndroidGroupcast;
import com.push.android.AndroidUnicast;
import com.push.ios.IOSBroadcast;
import com.push.ios.IOSCustomizedcast;
import com.push.ios.IOSFilecast;
import com.push.ios.IOSGroupcast;
import com.push.ios.IOSUnicast;

public class MessagePush {
	private String appkey = null;
	private String appMasterSecret = null;
	private String timestamp = null;
	private PushClient client = new PushClient();
	
	public MessagePush(String key, String secret) {
		try {
			appkey = key;
			appMasterSecret = secret;
		} catch (Exception e) {
			e.printStackTrace();
			System.exit(1);
		}
	}

	public void sendAndroidBroadcast() throws Exception {
		AndroidBroadcast broadcast = new AndroidBroadcast(appkey, appMasterSecret);
		broadcast.setTicker("Android broadcast ticker");
		broadcast.setTitle("中文的title");
		broadcast.setText("Android broadcast text");
		broadcast.setExpireTime("2018-03-18 21:17:58");
		broadcast.setDescription("title");
		broadcast.setProductionMode(false);
		broadcast.setCustomField("内容");
		broadcast.goAppAfterOpen();
		broadcast.setDisplayType(AndroidNotification.DisplayType.MESSAGE);
		// TODO Set 'production_mode' to 'false' if it's a test device.
		// For how to register a test device, please see the developer doc.
		broadcast.setProductionMode();
		// Set customized fields
		broadcast.setExtraField("test", "helloworld");
		client.send(broadcast);
	}

	public void sendAndroidUnicast() throws Exception {
		AndroidUnicast unicast = new AndroidUnicast(appkey, appMasterSecret);
		// TODO Set your device token
		unicast.setDeviceToken("your device token");
		unicast.setTicker("Android unicast ticker");
		unicast.setTitle("中文的title");
		unicast.setText("Android unicast text");
		unicast.goAppAfterOpen();
		unicast.setDisplayType(AndroidNotification.DisplayType.NOTIFICATION);
		// TODO Set 'production_mode' to 'false' if it's a test device.
		// For how to register a test device, please see the developer doc.
		unicast.setProductionMode();
		// Set customized fields
		unicast.setExtraField("test", "helloworld");
		client.send(unicast);
	}

	public void sendAndroidGroupcast() throws Exception {
		AndroidGroupcast groupcast = new AndroidGroupcast(appkey, appMasterSecret);
		/*
		 * TODO Construct the filter condition: "where": { "and": [ {"tag":"test"},
		 * {"tag":"Test"} ] }
		 */
		JSONObject filterJson = new JSONObject();
		JSONObject whereJson = new JSONObject();
		JSONArray tagArray = new JSONArray();
		JSONObject testTag = new JSONObject();
		JSONObject TestTag = new JSONObject();
		testTag.put("tag", "test");
		TestTag.put("tag", "Test");
		tagArray.put(testTag);
		tagArray.put(TestTag);
		whereJson.put("and", tagArray);
		filterJson.put("where", whereJson);
		System.out.println(filterJson.toString());

		groupcast.setFilter(filterJson);
		groupcast.setTicker("Android groupcast ticker");
		groupcast.setTitle("中文的title");
		groupcast.setText("Android groupcast text");
		groupcast.goAppAfterOpen();
		groupcast.setDisplayType(AndroidNotification.DisplayType.NOTIFICATION);
		// TODO Set 'production_mode' to 'false' if it's a test device.
		// For how to register a test device, please see the developer doc.
		groupcast.setProductionMode();
		client.send(groupcast);
	}

	public void sendAndroidCustomizedcast() throws Exception {
		AndroidCustomizedcast customizedcast = new AndroidCustomizedcast(appkey, appMasterSecret);
		// TODO Set your alias here, and use comma to split them if there are
		// multiple alias.
		// And if you have many alias, you can also upload a file containing these
		// alias, then
		// use file_id to send customized notification.

		customizedcast.setAlias("user_50", "user");
		customizedcast.setBuilderId(222);

		customizedcast.setTicker("Android customizedcast ticker");
		customizedcast.setTitle("中文的title");
		customizedcast.setText("Android customizedcast text");
		customizedcast.setPlayVibrate(false);
		customizedcast.setPlayLights(false);
		customizedcast.setPlaySound(true);
		customizedcast.goAppAfterOpen();
		// 后加
		customizedcast.setCustomField("custom");
		customizedcast.setExpireTime("2018-05-14 21:17:58");
		customizedcast.setDescription("222");
		customizedcast.setProductionMode(false);

		customizedcast.setDisplayType(AndroidNotification.DisplayType.NOTIFICATION);
		// TODO Set 'production_mode' to 'false' if it's a test device.
		// For how to register a test device, please see the developer doc.
		customizedcast.setProductionMode();
		client.send(customizedcast);
	}

	public void sendAndroidCustomizedcast(String message) throws Exception {
		AndroidCustomizedcast customizedcast = new AndroidCustomizedcast(appkey, appMasterSecret);
		// TODO Set your alias here, and use comma to split them if there are
		// multiple alias.
		// And if you have many alias, you can also upload a file containing these
		// alias, then
		// use file_id to send customized notification.
		String[] s = message.split(",");
		customizedcast.setAlias("user_" + s[1], "user");
		customizedcast.setBuilderId(222);

		customizedcast.setTicker("Android customizedcast ticker");
		customizedcast.setTitle(s[0]);
		customizedcast.setText(s[2]);
		customizedcast.setPlayVibrate(false);
		customizedcast.setPlayLights(false);
		customizedcast.setPlaySound(true);
		customizedcast.goAppAfterOpen();
		// 后加
		customizedcast.setCustomField("custom");
		// customizedcast.setExpireTime(String.valueOf(System.currentTimeMillis()));
		customizedcast.setDescription("222");
		customizedcast.setProductionMode(true);

		customizedcast.setDisplayType(AndroidNotification.DisplayType.MESSAGE);
		// TODO Set 'production_mode' to 'false' if it's a test device.
		// For how to register a test device, please see the developer doc.
		// customizedcast.setProductionMode(true);
		client.send(customizedcast);
	}

	public void sendAndroidCustomizedcastFile() throws Exception {
		AndroidCustomizedcast customizedcast = new AndroidCustomizedcast(appkey, appMasterSecret);
		// TODO Set your alias here, and use comma to split them if there are
		// multiple alias.
		// And if you have many alias, you can also upload a file containing these
		// alias, then
		// use file_id to send customized notification.
		String fileId = client.uploadContents(appkey, appMasterSecret, "aa" + "\n" + "bb" + "\n" + "alias");
		customizedcast.setFileId(fileId, "user");
		customizedcast.setTicker("Android customizedcast ticker");
		customizedcast.setTitle("中文的title");
		customizedcast.setText("Android customizedcast text");
		customizedcast.goAppAfterOpen();
		customizedcast.setDisplayType(AndroidNotification.DisplayType.NOTIFICATION);
		// TODO Set 'production_mode' to 'false' if it's a test device.
		// For how to register a test device, please see the developer doc.
		customizedcast.setProductionMode();
		client.send(customizedcast);
	}

	public void sendAndroidFilecast() throws Exception {
		AndroidFilecast filecast = new AndroidFilecast(appkey, appMasterSecret);
		// TODO upload your device tokens, and use '\n' to split them if there are
		// multiple tokens
		String fileId = client.uploadContents(appkey, appMasterSecret, "aa" + "\n" + "bb");
		filecast.setFileId(fileId);
		filecast.setTicker("Android filecast ticker");
		filecast.setTitle("中文的title");
		filecast.setText("Android filecast text");
		filecast.goAppAfterOpen();
		filecast.setDisplayType(AndroidNotification.DisplayType.NOTIFICATION);
		client.send(filecast);
	}

	public void sendIOSBroadcast() throws Exception {
		IOSBroadcast broadcast = new IOSBroadcast(appkey, appMasterSecret);
		broadcast.setAlert("IOS 广播测试");
		broadcast.setBadge(0);
		broadcast.setSound("default");
		// TODO set 'production_mode' to 'true' if your app is under production mode
		// broadcast.setTestMode();
		broadcast.setProductionMode(true);
		// Set customized fields
		broadcast.setCustomizedField("test", "helloworld");
		client.send(broadcast);
	}

	public void sendIOSUnicast() throws Exception {
		IOSUnicast unicast = new IOSUnicast(appkey, appMasterSecret);
		// TODO Set your device token
		unicast.setDeviceToken("xx");
		unicast.setAlert("IOS 单播测试");
		unicast.setBadge(0);
		unicast.setSound("default");
		// TODO set 'production_mode' to 'true' if your app is under production mode
		unicast.setTestMode();
		// Set customized fields
		unicast.setCustomizedField("test", "helloworld");
		client.send(unicast);
	}

	public void sendIOSGroupcast() throws Exception {
		IOSGroupcast groupcast = new IOSGroupcast(appkey, appMasterSecret);
		/*
		 * TODO Construct the filter condition: "where": { "and": [
		 * {"tag":"iostest"} ] }
		 */
		JSONObject filterJson = new JSONObject();
		JSONObject whereJson = new JSONObject();
		JSONArray tagArray = new JSONArray();
		JSONObject testTag = new JSONObject();
		testTag.put("tag", "iostest");
		tagArray.put(testTag);
		whereJson.put("and", tagArray);
		filterJson.put("where", whereJson);
		System.out.println(filterJson.toString());

		// Set filter condition into rootJson
		groupcast.setFilter(filterJson);
		groupcast.setAlert("IOS 组播测试");
		groupcast.setBadge(0);
		groupcast.setSound("default");
		// TODO set 'production_mode' to 'true' if your app is under production mode
		groupcast.setTestMode();
		client.send(groupcast);
	}

	public void sendIOSCustomizedcast() throws Exception {
		IOSCustomizedcast customizedcast = new IOSCustomizedcast(appkey, appMasterSecret);
		customizedcast.setAlias("user_4", "user");
		customizedcast.setAlert("小V提醒您,您有订单状态有更新,请点击查看");
		customizedcast.setBadge(0);
		customizedcast.setSound("default");
		// TODO set 'production_mode' to 'true' if your app is under production mode
		customizedcast.setTestMode();
		client.send(customizedcast);
	}

	public void sendIOSCustomizedcast(String message) throws Exception {
		IOSCustomizedcast customizedcast = new IOSCustomizedcast(appkey, appMasterSecret);
		// TODO Set your alias and alias_type here, and use comma to split them if
		// there are multiple alias.
		// And if you have many alias, you can also upload a file containing these
		// alias, then
		// use file_id to send customized notification.
		// customizedcast.setAlias("alias", "user");
		String[] s = message.split(",");
		customizedcast.setAlias("user_" + s[1], "user");
		customizedcast.setAlert(s[0] + s[2]);
		customizedcast.setBadge(1);
		customizedcast.setSound("default");
		// customizedcast.setTestMode();

		// TODO set 'production_mode' to 'true' if your app is under production mode
		// customizedcast.setTestMode();
		customizedcast.setProductionMode(true);
		client.send(customizedcast);
	}

	public void sendIOSFilecast() throws Exception {
		IOSFilecast filecast = new IOSFilecast(appkey, appMasterSecret);
		// TODO upload your device tokens, and use '\n' to split them if there are
		// multiple tokens
		String fileId = client.uploadContents(appkey, appMasterSecret, "aa" + "\n" + "bb");
		filecast.setFileId(fileId);
		filecast.setAlert("IOS 文件播测试");
		filecast.setBadge(0);
		filecast.setSound("default");
		// TODO set 'production_mode' to 'true' if your app is under production mode
		filecast.setTestMode();
		client.send(filecast);
	}

	public static void main(String[] args) {
		// TODO set your appkey and master secret here

		MessagePush demo = new MessagePush("xxxxxx", "xxxxxx");
		MessagePush ios = new MessagePush("xxxxxx", "xxxxxxx");
		try {

			demo.sendAndroidCustomizedcast();
			ios.sendIOSCustomizedcast();
			// ios.sendIOSCustomizedcast();

			// demo.sendIOSBroadcast();
			// demo.sendAndroidCustomizedcastFile();
			// demo.sendAndroidUnicast();
			/*
			 * TODO these methods are all available, just fill in some fields and do
			 * the test demo.sendAndroidCustomizedcastFile();
			 * demo.sendAndroidBroadcast(); demo.sendAndroidGroupcast();
			 * demo.sendAndroidCustomizedcast(); demo.sendAndroidFilecast();
			 * 
			 * demo.sendIOSBroadcast(); demo.sendIOSUnicast();
			 * demo.sendIOSGroupcast(); demo.sendIOSCustomizedcast();
			 * demo.sendIOSFilecast();
			 */
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}


}

话就不多说了 直接贴码 这是友盟的 也有sdk 同理了个公用的 各位知道怎么调用就好了  实现可以自己写 不懂得多看看就好了

发现很多人还是有点蒙 那么我就在废话一下吧 

  1. 基本概念 
    appkey:应用唯一标识 
    app_master_secret:服务器Key。 
    device_token: 友盟后台对设备的唯一标识。注意, Android的device_token是44位字符串, iOS的device-token是64位。 
    单播: 向指定的设备发送消息。 
    广播: 向安装该APP的所有设备发送消息。 
    组播: 向满足特定条件的设备集合发送消息,例如: “特定版本”、”特定地域”等。 
    alias: 第三方账号, 开发者可以在SDK中调用setAlias接口,SDK会把设定的alias,alias_type与device_token进行绑定传回到后端。便于开发者通过自有业务逻辑筛选出alias,通过发送alias给友盟进行消息推送。 
    通知-Android: 消息送达到用户设备后,由友盟处理内容并在通知栏上显示通知内容。 
    消息-Android: 消息送达到用户设备后,消息内容被传递给应用自己解析和后续处理,友盟不对消息内容进行展示和提醒。 
    通知/消息-iOS: 和APNs定义一致。 
    测试模式-Android: 在广播、组播等大规模发送消息的情况下,为了防止开发者误发消息, 增加了测试模式。 测试模式下,只会将消息发送给测试设备。测试设备需要到网站上添加。测试模式对单播不生效。 
    测试模式-iOS: iOS的测试模式对应APNs的开发环境(sandbox)。
  2. 测试模式可以调试 要后台配置 如有需要 需要添加白名单机制 
展开阅读全文

没有更多推荐了,返回首页