Skip to content

Commit cba769d

Browse files
author
Devendra
committed
startup service on boot
1 parent f4d88b0 commit cba769d

4 files changed

Lines changed: 137 additions & 68 deletions

File tree

‎android/examples/PubnubSubscribe/AndroidManifest.xml‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
22
package="com.pubnub.examples.pubnubsubscribe"
3+
android:installLocation="internalOnly"
34
android:versionCode="1"
45
android:versionName="1.0" >
56

@@ -27,12 +28,17 @@
2728
</intent-filter>
2829
</activity>
2930

30-
<receiver android:name=".BootReceiver" >
31+
<receiver
32+
android:name=".BootReceiver"
33+
android:enabled="true"
34+
android:permission="android.permission.RECEIVE_BOOT_COMPLETED" >
3135
<intent-filter>
3236
<action android:name="android.intent.action.BOOT_COMPLETED" />
37+
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
3338
</intent-filter>
3439
</receiver>
3540

41+
3642
<service android:name=".PubnubService" />
3743
</application>
3844

‎android/examples/PubnubSubscribe/src/com/pubnub/examples/pubnubsubscribe/BootReceiver.java‎

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,17 @@
44
import android.content.Context;
55
import android.content.Intent;
66
import android.util.Log;
7+
import android.widget.Toast;
78

89
public class BootReceiver extends BroadcastReceiver {
910

1011
@Override
1112
public void onReceive(Context context, Intent arg1) {
12-
Log.i("PubnubService", "PubNub BootReceiver Starting");
13+
Log.i("PUBNUB", "PubNub BootReceiver Starting");
1314
Intent intent = new Intent(context, PubnubService.class);
1415
context.startService(intent);
15-
Log.i("PubnubService", "PubNub BootReceiver Started");
16+
Log.i("PUBNUB", "PubNub BootReceiver Started");
1617

17-
Intent i = new Intent(context, PubnubActivity.class);
18-
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
19-
context.startActivity(i);
2018
}
2119

22-
}
20+
}

‎android/examples/PubnubSubscribe/src/com/pubnub/examples/pubnubsubscribe/PubnubActivity.java‎

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22

33
import java.util.ArrayList;
44

5+
import org.json.JSONArray;
56
import org.json.JSONException;
67
import org.json.JSONObject;
78

9+
import com.pubnub.api.Callback;
10+
import com.pubnub.api.PubnubError;
811
import com.pubnub.examples.pubnubsubscribe.R;
912

1013
import android.app.Activity;
@@ -32,6 +35,8 @@ public class PubnubActivity extends Activity {
3235
private NotificationManager mNotificationManager;
3336
public ArrayList<String> list = new ArrayList<String>();
3437
public CustomListAdapter adapter;
38+
39+
public static boolean activityStarted = false;
3540

3641
volatile boolean sendNotification = false;
3742

@@ -64,6 +69,7 @@ private void sendNotification(String msg) {
6469
}
6570

6671
public void notifyUser(JSONObject message) {
72+
Log.i("PUBNUB", "Notify User: " + message.toString());
6773
String notification = null;
6874
Object data = null;
6975
try {
@@ -79,10 +85,25 @@ public void notifyUser(JSONObject message) {
7985
}
8086

8187
if (sendNotification) {
88+
Log.i("PUBNUB", "Sending notification : " + notification);
8289
sendNotification(notification);
8390
}
91+
Log.i("PUBNUB", "Add to List " + data.toString());
8492
list.add(0, data.toString());
85-
adapter.notifyDataSetChanged();
93+
Log.i("PUBNUB", "List Size : " + list.size());
94+
95+
runOnUiThread(new Runnable() {
96+
@Override
97+
public void run() {
98+
try {
99+
adapter.notifyDataSetChanged();
100+
} catch (Exception e) {
101+
Log.i("PUBNUB", "could not notify on adapter " + e.toString());
102+
}
103+
}
104+
});
105+
106+
Log.i("PUBNUB", "Adapter Notified");
86107

87108
}
88109

@@ -103,6 +124,8 @@ public void onCreate(Bundle savedInstanceState) {
103124
super.onCreate(savedInstanceState);
104125

105126
setContentView(R.layout.activity_pubnub);
127+
128+
activityStarted = true;
106129

107130
ListView listview = (ListView) findViewById(R.id.message_list);
108131
/*
@@ -120,6 +143,31 @@ public void onCreate(Bundle savedInstanceState) {
120143

121144
Intent serviceIntent = new Intent(this, PubnubService.class);
122145
startService(serviceIntent);
146+
147+
PubnubService.initPubnub();
148+
149+
PubnubService.pubnub.history(PubnubService.public_channel, 3, true, new Callback() {
150+
@Override
151+
public void successCallback(String channel, Object message) {
152+
Log.i("PUBNUB", message.toString());
153+
JSONArray jsa;
154+
try {
155+
jsa = (JSONArray) ((JSONArray) message).get(0);
156+
Log.i("PUBNUB", "message count : " + jsa.length());
157+
for (int i = jsa.length() - 1; i >= 0; i--) {
158+
notifyUser((JSONObject) jsa.get(i));
159+
}
160+
} catch (JSONException e1) {
161+
e1.printStackTrace();
162+
}
163+
164+
}
165+
166+
@Override
167+
public void errorCallback(String channel, PubnubError error) {
168+
169+
}
170+
});
123171

124172
Log.i("PubnubActivity", "PubNub Activity Started!");
125173

‎android/examples/PubnubSubscribe/src/com/pubnub/examples/pubnubsubscribe/PubnubService.java‎

Lines changed: 77 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
import org.json.JSONException;
55
import org.json.JSONObject;
66

7+
import android.media.Ringtone;
8+
import android.media.RingtoneManager;
9+
import android.net.Uri;
710
import android.os.Handler;
811
import android.os.Message;
912
import android.os.PowerManager;
@@ -29,53 +32,89 @@ public class PubnubService extends Service {
2932

3033
public static final String ACTION_FROM_SERVICE = null;
3134

32-
String device_channel = "Android-1";
33-
String public_channel = "public";
34-
String auth_key = "Android-AuthToken";
35-
String uuid = "Android-user1";
35+
private static final int NOTIFICATION_ID = 1;
3636

37-
String publish_key = "pam";
38-
String subscribe_key = "pam";
37+
public static String device_channel = "Android-1";
38+
public static String public_channel = "public";
39+
public static String auth_key = "Android-AuthToken";
40+
public static String uuid = "Android-user1";
41+
private NotificationManager mNotificationManager;
3942

40-
Pubnub pubnub;
41-
PowerManager.WakeLock wl = null;
43+
public static String publish_key = "pam";
44+
public static String subscribe_key = "pam";
4245

43-
private void notifyUser(Object message) {
44-
Log.i("PUBNUB", message.toString());
45-
Intent intent = new Intent("android.intent.action.MAIN");
46-
intent.putExtra("data", message.toString());
47-
sendBroadcast(intent);
48-
}
46+
public static Pubnub pubnub = null;
47+
48+
PowerManager.WakeLock wl = null;
4949

50-
public void onCreate() {
51-
super.onCreate();
50+
51+
public static void initPubnub() {
52+
if (pubnub != null) return;
5253
pubnub = new Pubnub(publish_key, subscribe_key, false);
5354
pubnub.setUUID(uuid);
5455
pubnub.setAuthKey(auth_key);
56+
pubnub.setMaxRetries(1000);
57+
}
58+
59+
private void sendNotification(String msg) {
60+
mNotificationManager = (NotificationManager) this
61+
.getSystemService(Context.NOTIFICATION_SERVICE);
5562

56-
pubnub.history(public_channel, 3, true, new Callback() {
57-
@Override
58-
public void successCallback(String channel, Object message) {
59-
Log.i("PUBNUB", message.toString());
60-
JSONArray jsa;
61-
try {
62-
jsa = (JSONArray) ((JSONArray) message).get(0);
63-
for (int i = 2; i >= 0; i--) {
64-
notifyUser(jsa.get(i));
65-
}
66-
} catch (JSONException e1) {
67-
// TODO Auto-generated catch block
68-
e1.printStackTrace();
69-
}
63+
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
64+
new Intent(this, PubnubActivity.class), 0);
65+
66+
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
67+
this).setSmallIcon(R.drawable.icon)
68+
.setContentTitle("PubNub Notification")
69+
.setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
70+
.setContentText(msg);
7071

71-
}
72+
mBuilder.setContentIntent(contentIntent);
73+
mBuilder.setAutoCancel(true);
74+
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
7275

73-
@Override
74-
public void errorCallback(String channel, PubnubError error) {
76+
try {
77+
Uri notification = RingtoneManager
78+
.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
79+
Ringtone r = RingtoneManager.getRingtone(getApplicationContext(),
80+
notification);
81+
r.play();
82+
} catch (Exception e) {
83+
Log.i("PUBNUB", e.toString());
84+
}
85+
}
86+
87+
private void notifyUser(JSONObject message) {
88+
Log.i("PUBNUB", message.toString());
89+
90+
String notification = null;
91+
Object data = null;
92+
try {
93+
data = message.get("data");
94+
} catch (JSONException e1) {
95+
Log.i("PUBNUB", e1.toString());
96+
return;
97+
}
98+
try {
99+
notification = message.getString("message");
100+
} catch (JSONException e) {
101+
notification = data.toString();
102+
}
75103

76-
}
77-
});
104+
105+
if (PubnubActivity.activityStarted) {
106+
Intent intent = new Intent("android.intent.action.MAIN");
107+
intent.putExtra("data", message.toString());
108+
sendBroadcast(intent);
109+
} else {
110+
sendNotification(notification);
111+
}
112+
}
113+
78114

115+
public void onCreate() {
116+
super.onCreate();
117+
initPubnub();
79118
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
80119
wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "SubscribeAtBoot");
81120
if (wl != null) {
@@ -86,37 +125,15 @@ public void errorCallback(String channel, PubnubError error) {
86125
Log.i("PUBNUB", "PubnubService created...");
87126

88127
try {
89-
pubnub.subscribe(public_channel, new Callback() {
90-
@Override
91-
public void connectCallback(String channel, Object response) {
92-
try {
93-
pubnub.subscribe(device_channel, new Callback() {
94-
@Override
95-
public void successCallback(String channel,
96-
Object message) {
97-
notifyUser(message.toString());
98-
}
99-
100-
@Override
101-
public void errorCallback(String channel,
102-
Object message) {
103-
notifyUser(message.toString());
104-
}
105-
});
106-
} catch (PubnubException e) {
107-
Log.i("PUBNUB", e.toString());
108-
}
109-
110-
}
111-
128+
pubnub.subscribe(new String[]{public_channel, device_channel}, new Callback() {
112129
@Override
113130
public void successCallback(String channel, Object message) {
114-
notifyUser(message.toString());
131+
notifyUser((JSONObject) message);
115132
}
116133

117134
@Override
118135
public void errorCallback(String channel, Object message) {
119-
notifyUser(message.toString());
136+
notifyUser((JSONObject) message);
120137
}
121138
});
122139
} catch (PubnubException e) {

0 commit comments

Comments
 (0)