利用socket进行消息推送

2 篇文章 0 订阅
1 篇文章 0 订阅

对于利用socket通信进行消息推送,我研究了两三天吧,当然是在前几天研究消息推送机制以及第三方和轮询的基础上进行的,以下是我的一些感想吧,有不正确或者不完善的地方希望大家提出来一起研究吧。

1.首先,了解socket的连接过程:

1)服务器监听;2)客户端请求;3)连接确认(具体详情可以见百度百科,我在这也就不多说了)。

2.socket通信机制:

服务器端
  一、创建服务器套接字(CREATE)。
  二、服务器套接字进行信息绑定(BIND),并开始监听连接(LISTEN)。
  三、接受来自客户端的连接请求(ACCEPT),并创建接收进程。
  四、开始数据传输(SEND、RECEIVE)。
  五、关闭套接字(CLOSESOCKET)。
客户机端
  一、创建客户机套接字(CREATE)。
  二、与远程服务器进行连接(CONNECT),如被接受则创建接收进程。
  三、开始数据传输(SEND、RECEIVE)。
  四、关闭套接字(CLOSESOCKET)。

具体在java中的实现是:

服务器端:

1)创建ServerSocket,需要使用端口号进行标识,以便客户端进行连接。

如:ServerSocket serverSocket = new ServerSocket(9999);

2)创建Socket获取连接

如: Socket socket = serverSocket.accept();

3)便可以进行通信内容传输了。

客户端:

1)创建Socket,进行与服务器连接,需要填写服务器ip,以及服务器的端口号

如:Socket socket = new Socket("127.0.0.1", 9999);

2)进行通信内容的传输。

以上只是一个简单的socket通信案例,想要了解的可以进入该链接下载:http://download.csdn.net/detail/xia09222826/8244423
3.利用socket通信进行服务器端到客户端的消息推送的思路:

客户端登录后发送一个消息给服务器端,用一个标识作为用户客户端程序关闭后服务器端是否推送消息给客户端,如果有则进行消息推送,如果没有则不进行消息推送,而当程序运行时就是一般的socket通信,也就是服务器端写一条消息推送给客户端,客户端便可以接收,并在消息栏上显示,点击消息栏进行相应的界面即可。

服务器就不多说了,方法是差不多的。

客户端:

需要使用Service来进行监听:

public class PushService extends Service {

	private PushClient mClient;

	private Handler mHandler;

	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}

	@Override
	public void onCreate() {
		super.onCreate();
		initHandler();
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		mClient = new PushClient(new InetSocketAddress("192.168.4.101", 9999),
				mHandler);
		mClient.start();
		return super.onStartCommand(intent, flags, startId);
	}

	@Override
	public void onDestroy() {
		if (mClient != null) {
			mClient.disConnect();
		}
		super.onDestroy();
	}

	/**
	 * 初始化Handler
	 */
	private void initHandler() {
		mHandler = new Handler(new Callback() {

			@Override
			public boolean handleMessage(Message msg) {
				switch (msg.what) {
				case Const.PUSH_MSG:
					String pushMsg = msg.obj.toString();
					showNotification(pushMsg);
					break;

				default:
					break;
				}
				return false;
			}
		});
	}

	/**
	 * 
	 * 在状态栏显示通知
	 */

	@SuppressWarnings("deprecation")
	private void showNotification(String msg) {
		// 创建一个NotificationManager的引用
		NotificationManager notificationManager = (NotificationManager) getSystemService(android.content.Context.NOTIFICATION_SERVICE);

		// 定义Notification的各种属性
		Notification notification = new Notification(R.drawable.ic_launcher,
				msg, System.currentTimeMillis());

		// FLAG_AUTO_CANCEL 该通知能被状态栏的清除按钮给清除掉
		// FLAG_NO_CLEAR 该通知不能被状态栏的清除按钮给清除掉
		// FLAG_ONGOING_EVENT 通知放置在正在运行
		// FLAG_INSISTENT 是否一直进行,比如音乐一直播放,知道用户响应
		notification.flags |= Notification.FLAG_AUTO_CANCEL; // 表明在点击了通知栏中的"清除通知"后,此通知不清除,经常与FLAG_ONGOING_EVENT一起使用

		// DEFAULT_ALL 使用所有默认值,比如声音,震动,闪屏等等
		// DEFAULT_LIGHTS 使用默认闪光提示
		// DEFAULT_SOUND 使用默认提示声音
		// DEFAULT_VIBRATE 使用默认手机震动,需加上<uses-permission
		// android:name="android.permission.VIBRATE" />权限
		notification.defaults = Notification.DEFAULT_SOUND;

		// 设置通知的事件消息
		CharSequence contentTitle = msg; // 通知栏标题
		CharSequence contentText = msg; // 通知栏内容
		Intent notificationIntent = new Intent(this, TestActivity.class); // 点击该通知后要跳转的Activity

		PendingIntent contentItent = PendingIntent.getActivity(this, 0,
				notificationIntent, 0);
		notification.setLatestEventInfo(this, contentTitle, contentText,
				contentItent);
		// 把Notification传递给NotificationManager
		notificationManager.notify(0, notification);
	}
这样就可以在将接收到的消息在消息栏上显示了,点击后便可以进入相应的界面了。



评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值