首先第一步引入websocket
implementation "org.java-websocket:Java-WebSocket:1.5.1"
第二步
public class JWebSocketClient extends WebSocketClient { public JWebSocketClient(URI serverUri) { super(serverUri, new Draft_6455()); } @Override public void onOpen(ServerHandshake handshakedata) { Log.e("JWebSocketClient", "onOpen()"); } @Override public void onMessage(String message) { Log.e("JWebSocketClient", message); } @Override public void onClose(int code, String reason, boolean remote) { Log.e("JWebSocketClient", "onClose()"); } @Override public void onError(Exception ex) { Log.e("JWebSocketClient", ex.getMessage()); } }
第三步
我是直接放在 Application中了
private JWebSocketClient client; private URI uri;public class BaseApplication extends Application {@Override public void onCreate() { super.onCreate();//推送 也可以做即时通讯 uri= URI.create("ws://这边放后台给你的地址"); client= new JWebSocketClient(uri) { @Override public void onMessage(String message) { //message就是接收到的消息 可以让后台给你发送json,解析使用 Log.e("JWebSClientService", message); sendChatMsg(message); } }; //开启连接 connect();//Android 10要设置通知类型
//设置通知类型 我这边设置两个 根据不同的需求可以添加自己的设置 使用前要记得开启通知权限 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { String channelId = "chat"; String channelName = "聊天消息"; int importance = NotificationManager.IMPORTANCE_HIGH; createNotificationChannel(channelId, channelName, importance); channelId = "subscribe"; channelName = "订阅消息"; importance = NotificationManager.IMPORTANCE_DEFAULT; createNotificationChannel(channelId, channelName, importance); }}
@TargetApi(Build.VERSION_CODES.O) private void createNotificationChannel(String channelId, String channelName, int importance) { NotificationChannel channel = new NotificationChannel(channelId, channelName, importance); NotificationManager notificationManager = (NotificationManager) getSystemService( NOTIFICATION_SERVICE); notificationManager.createNotificationChannel(channel); }//WebSocket开启连接 private void connect(){ try { client.connectBlocking(); } catch (InterruptedException e) { e.printStackTrace(); } }private int requestCode=0; //看使用的地方,两个地方,如果使用的地方,你写的是固定值,那么第一个地方会导致你推送得到的信息被复制,第二个地方,只会显示一条推送 。文笔不好, 如果不理解,可以直接把这两个地方分别写成固定值试试 public void sendChatMsg(String msg) { requestCode++; Intent intent = new Intent(this, NotificationClickReceiver.class); Log.e("fhxx","这是我准备发送的"+msg +" ----- >"+requestCode); intent.putExtra("msg",msg); PendingIntent broadcast = PendingIntent.getBroadcast(this, requestCode, intent, PendingIntent.FLAG_UPDATE_CURRENT); NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); Notification notification = new NotificationCompat.Builder(this, "chat") .setContentTitle("收到一条聊天消息") .setContentText(msg) .setWhen(System.currentTimeMillis()) .setSmallIcon(R.mipmap.icon_logo) .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.icon_logo)) .setDefaults(Notification.GROUP_ALERT_ALL) //设置默认的提示音,振动方式,灯光 .setAutoCancel(true) .setContentIntent(broadcast) .build(); manager.notify(requestCode, notification); }}
public class NotificationClickReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { String msg = intent.getStringExtra("msg"); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); Log.e("fhxx","这是我收到的-"+msg); } }
到此为止,就可以实现推送的基本功能。