1+ package com .kolserdav .ana ;
2+
3+ import android .app .PendingIntent ;
4+ import android .content .Intent ;
5+ import android .app .Service ;
6+ import android .net .Uri ;
7+ import android .os .*;
8+ import android .util .Log ;
9+
10+ import androidx .annotation .Nullable ;
11+ import androidx .core .app .NotificationCompat ;
12+ import androidx .core .app .NotificationManagerCompat ;
13+
14+ import org .java_websocket .handshake .ServerHandshake ;
15+ import org .json .JSONException ;
16+ import org .json .JSONObject ;
17+
18+ import java .net .URI ;
19+ import java .net .URISyntaxException ;
20+
21+
22+ public class DisplayNotification extends Service {
23+
24+ private final String TAG = "DisplayNotification" ;
25+
26+ public static final String INTENT_EXTRA_NAME_URL = "url" ;
27+
28+ public static final String INTENT_EXTRA_NAME_WS_ADDRESS = "ws_address" ;
29+
30+ public static final String INTENT_EXTRA_NAME_NOTIFICATION_UNIT_ID = "notification_unit_id" ;
31+
32+ private String wsAddress = null ;
33+
34+ private String url = null ;
35+
36+ private String unitId = null ;
37+
38+ public void createNotification (String title , String content , String path ) {
39+ Intent intent = new Intent (this , MainActivity .class );
40+ intent .setData (Uri .parse (url + path ));
41+ intent .setFlags (Intent .FLAG_ACTIVITY_NEW_TASK | Intent .FLAG_ACTIVITY_CLEAR_TASK );
42+ int requestID = (int ) System .currentTimeMillis ();
43+ int flags = PendingIntent .FLAG_UPDATE_CURRENT ;
44+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .S ) {
45+ flags = PendingIntent .FLAG_IMMUTABLE ;
46+ }
47+ PendingIntent pendingIntent = PendingIntent .getActivity (this , requestID , intent , flags );
48+ NotificationCompat .Builder builder = new NotificationCompat .Builder (this , Config .CHANNEL_ID )
49+ .setSmallIcon (R .mipmap .ic_launcher )
50+ .setContentTitle (title )
51+ .setContentIntent (pendingIntent )
52+ .setAutoCancel (true )
53+ .setStyle (new NotificationCompat .BigTextStyle ()
54+ .bigText (content ))
55+
56+ .setPriority (NotificationCompat .PRIORITY_DEFAULT );
57+ NotificationManagerCompat notificationManager = NotificationManagerCompat .from (this );
58+ notificationManager .notify (requestID , builder .build ());
59+ }
60+
61+ private void listenNotifications () {
62+ URI uri = null ;
63+ try {
64+ uri = new URI (wsAddress );
65+ } catch (URISyntaxException e ) {
66+ Log .e (TAG , "Error create WebSocket URI: " + e .getMessage ());
67+ }
68+
69+ if (uri != null ) {
70+ WebSocket ws = new WebSocket (uri ) {
71+ @ Override
72+ public void onOpen (ServerHandshake handshake ) {
73+ super .onOpen (handshake );
74+ JSONObject obj = new JSONObject ();
75+ try {
76+ obj .put ("message" , Config .WS_MESSAGE_NOTIFICATION_USER_ID );
77+ obj .put ("data" , unitId );
78+ } catch (JSONException e ) {
79+ Log .e (TAG , "Failed create JSON object: " + e .getMessage ());
80+ }
81+ send (obj .toString ());
82+ }
83+
84+ @ Override
85+ public void onClose (int code , String reason , boolean remote ) {
86+ super .onClose (code , reason , remote );
87+ try {
88+ Thread .sleep (Config .WS_RECONNECT_TIMEOUT );
89+ } catch (InterruptedException e ) {
90+ Log .e (TAG , "Failed wait reconnect WS: " + e .getMessage ());
91+ }
92+ listenNotifications ();
93+ }
94+
95+ @ Override
96+ public void onMessage (String message ) {
97+ super .onMessage (message );
98+ JSONObject data = null ;
99+ try {
100+ data = new JSONObject (message );
101+ } catch (JSONException e ) {
102+ Log .e (TAG , "Failed parse WS message: " + e .getMessage ());
103+ }
104+ if (data != null ) {
105+ try {
106+ String title = data .get ("type" ).toString ();
107+ String content = data .get ("message" ).toString ();
108+ String path = data .get ("data" ).toString ();
109+ createNotification (title , content , path );
110+ } catch (JSONException e ) {
111+ Log .e (TAG , "Failed parse WS message: " + e .getMessage ());
112+ }
113+ }
114+ }
115+ };
116+ ws .connect ();
117+ }
118+ }
119+
120+ @ Override
121+ public void onCreate () {
122+ super .onCreate ();
123+ Thread thread = new Thread () {
124+ @ Override
125+ public void run () {
126+ super .run ();
127+ while (url == null ) {}
128+ Log .d (TAG , "Notifications service is running: " + wsAddress + ", " + url );
129+ listenNotifications ();
130+ }
131+ };
132+ thread .start ();
133+ }
134+
135+ @ Nullable
136+ @ Override
137+ public IBinder onBind (Intent intent ) {
138+ return null ;
139+ }
140+
141+ @ Override
142+ public int onStartCommand (Intent intent , int flags , int startId ) {
143+ Bundle extras = intent .getExtras ();
144+ if (extras == null ) {
145+ Log .w (TAG , "Extras is null" );
146+ } else {
147+ wsAddress = extras .get (INTENT_EXTRA_NAME_WS_ADDRESS ).toString ();
148+ url = extras .get (INTENT_EXTRA_NAME_URL ).toString ();
149+ unitId = extras .get (INTENT_EXTRA_NAME_NOTIFICATION_UNIT_ID ).toString ();
150+ }
151+ return super .onStartCommand (intent , flags , startId );
152+ }
153+
154+ @ Override
155+ public void onDestroy () {
156+ super .onDestroy ();
157+ Log .d (TAG , "Service destroyed" );
158+ }
159+ }
0 commit comments