1+ package receiver ;
2+
3+ import android .app .Notification ;
4+ import android .app .NotificationManager ;
5+ import android .app .PendingIntent ;
6+ import android .content .Context ;
7+ import android .content .Intent ;
8+ import android .os .Bundle ;
9+ import android .support .v4 .app .NotificationCompat ;
10+ import android .util .Log ;
11+
12+ import org .json .JSONException ;
13+
14+ import java .util .regex .Matcher ;
15+ import java .util .regex .Pattern ;
16+
17+ import rtmchat .realtime .co .rtmchat .R ;
18+ import rtmchat .realtime .co .rtmchat .activities .MessageActivity ;
19+ import rtmchat .realtime .co .rtmchat .activities .NotificationActivity ;
20+ import ibt .ortc .extensibility .GcmOrtcBroadcastReceiver ;
21+
22+ public class GcmReceiver extends GcmOrtcBroadcastReceiver {
23+
24+ private static final String TAG = "GcmReceiver" ;
25+ private static final String MESSAGE_PATTERN = "^(.[^_]*)_(.[^-]*)-(.[^_]*)_([\\ s\\ S]*?)$" ;
26+ private static final Pattern messagePattern = Pattern .compile (MESSAGE_PATTERN );
27+
28+ public GcmReceiver () {
29+ }
30+
31+ @ Override
32+ public void onReceive (Context context , Intent intent ) {
33+ Log .d (TAG , "Received message" );
34+ Bundle extras = intent .getExtras ();
35+ if (extras != null && !MessageActivity .isInForeground () && !NotificationActivity .isInForeground ()) {
36+ createNotification (context , extras );
37+ }
38+ }
39+
40+ private String parseOrtcMessage (String ortcMessage ) {
41+
42+ // Automatic ORTC push messages have the following format:
43+ // <msg_id>_1-1_<message sent by user>
44+
45+ Matcher parsedMessageMatcher = messagePattern .matcher (ortcMessage );
46+ String parsedMessage = "" ;
47+
48+ try {
49+ if (parsedMessageMatcher .matches ()) {
50+ parsedMessage = parsedMessageMatcher .group (4 );
51+ }
52+ } catch (Exception parseException ){
53+ // probably a custom push message, use the received string with no parsing
54+ parsedMessage = ortcMessage ;
55+ }
56+
57+ if (parsedMessage == "" ) {
58+ // there's something wrong with the message format. Use the unparsed format.
59+ parsedMessage = ortcMessage ;
60+ }
61+
62+ return parsedMessage ;
63+ }
64+
65+
66+ public void createNotification (Context context , Bundle extras )
67+ {
68+ String message = extras .getString ("M" );
69+ String channel = extras .getString ("C" );
70+ String payload = extras .getString ("P" ); // this is only used by custom notifications
71+
72+ if (message != "" ) {
73+
74+ String parsedMessage = parseOrtcMessage (message );
75+
76+ if (payload != null ) {
77+ Log .i (TAG , String .format ("Custom push notification on channel: %s message: %s payload: %s" , channel , parsedMessage , payload ));
78+ }else {
79+ Log .i (TAG , String .format ("Automatic push notification on channel: %s message: %s " , channel , parsedMessage ));
80+ }
81+
82+ try {
83+
84+ // parsed message format: <user>:<chat message>
85+
86+ String parts [] = parsedMessage .split (":" );
87+ String user = parts [0 ].split ("_" )[parts [0 ].split ("_" ).length - 1 ];
88+ String chatMessage = parts [1 ];
89+
90+ NotificationManager notificationManager = (NotificationManager ) context .getSystemService (Context .NOTIFICATION_SERVICE );
91+ Intent notificationIntent = new Intent (context , NotificationActivity .class );
92+
93+ notificationIntent .putExtra ("channel" , channel );
94+ notificationIntent .putExtra ("message" , chatMessage );
95+ notificationIntent .putExtra ("user" , user );
96+
97+ String appName = getAppName (context );
98+
99+ notificationIntent .setFlags (Intent .FLAG_ACTIVITY_NEW_TASK | Intent .FLAG_ACTIVITY_CLEAR_TASK );
100+ PendingIntent pendingIntent = PendingIntent .getActivity (context , 9999 , notificationIntent , PendingIntent .FLAG_UPDATE_CURRENT );
101+
102+ Notification notification = new NotificationCompat .Builder (context ).setContentTitle (channel ).setContentText (chatMessage ).setSmallIcon (R .drawable .ic_launcher )
103+ .setContentIntent (pendingIntent ).setAutoCancel (true ).build ();
104+ notificationManager .notify (appName , 9999 , notification );
105+
106+ } catch (Exception e ) {
107+ e .printStackTrace ();
108+ }
109+
110+ }
111+ }
112+
113+ private String getAppName (Context context )
114+ {
115+ CharSequence appName =
116+ context
117+ .getPackageManager ()
118+ .getApplicationLabel (context .getApplicationInfo ());
119+
120+ return (String )appName ;
121+ }
122+
123+ }
0 commit comments