Skip to content

Commit 512fd76

Browse files
Merge pull request #7 from jparreira/master
Removed service to handle push notifications when app is closed
2 parents 4f31e68 + 64c394e commit 512fd76

File tree

6 files changed

+151
-167
lines changed

6 files changed

+151
-167
lines changed

RTMChat/.idea/misc.xml

Lines changed: 27 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

RTMChat/app/src/main/AndroidManifest.xml

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
android:value="@integer/google_play_services_version" />
5151

5252
<receiver
53-
android:name="ibt.ortc.extensibility.GcmOrtcBroadcastReceiver"
53+
android:name="receiver.GcmReceiver"
5454
android:permission="com.google.android.c2dm.permission.SEND" >
5555
<intent-filter>
5656
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
@@ -60,15 +60,6 @@
6060

6161
<service android:name="ibt.ortc.extensibility.GcmOrtcIntentService" />
6262

63-
<service android:name="services.MyService" />
64-
65-
<receiver
66-
android:name="receiver.MyReceiver">
67-
<intent-filter>
68-
<action android:name="android.intent.action.BOOT_COMPLETED" />
69-
</intent-filter>
70-
</receiver>
71-
7263
</application>
7364

7465
</manifest>
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
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+
}

RTMChat/app/src/main/java/receiver/MyReceiver.java

Lines changed: 0 additions & 20 deletions
This file was deleted.

RTMChat/app/src/main/java/rtmchat/realtime/co/rtmchat/activities/MainActivity.java

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
package rtmchat.realtime.co.rtmchat.activities;
22

3-
import android.app.Activity;
43
import android.app.ActivityManager;
54
import android.content.Intent;
65
import android.graphics.Color;
76
import android.os.Bundle;
87
import android.support.v7.app.ActionBarActivity;
9-
import android.util.Log;
108
import android.view.KeyEvent;
119
import android.view.Menu;
1210
import android.view.MenuItem;
@@ -21,7 +19,6 @@
2119
import interfaces.InterfaceRefresher;
2220
import preferences.PreferencesManager;
2321
import rtmchat.realtime.co.rtmchat.R;
24-
import services.MyService;
2522

2623

2724
public class MainActivity extends ActionBarActivity implements InterfaceRefresher {
@@ -33,10 +30,6 @@ protected void onCreate(Bundle savedInstanceState) {
3330

3431
OrtcHandler.prepareClient(getApplicationContext(),this);
3532

36-
if(!isMyServiceRunning(MyService.class)) {
37-
startService(new Intent(this, MyService.class));
38-
}
39-
4033
EditText inputUser = (EditText) this.findViewById(R.id.nickNameInput);
4134

4235
final Button bt = (Button) findViewById(R.id.chatRooms);
@@ -107,14 +100,4 @@ public void run() {
107100
});
108101

109102
}
110-
111-
private boolean isMyServiceRunning(Class<?> serviceClass) {
112-
ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
113-
for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
114-
if (serviceClass.getName().equals(service.service.getClassName())) {
115-
return true;
116-
}
117-
}
118-
return false;
119-
}
120103
}

RTMChat/app/src/main/java/services/MyService.java

Lines changed: 0 additions & 120 deletions
This file was deleted.

0 commit comments

Comments
 (0)