Skip to content

Commit 7337d8a

Browse files
author
Devendra
committed
saving changes
1 parent aa93298 commit 7337d8a

4 files changed

Lines changed: 135 additions & 54 deletions

File tree

4 KB
Loading
230 Bytes
Loading

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

Lines changed: 55 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,14 @@
55
import com.pubnub.examples.pubnubsubscribe.R;
66

77
import android.app.Activity;
8+
import android.app.NotificationManager;
9+
import android.app.PendingIntent;
810
import android.content.BroadcastReceiver;
911
import android.content.Context;
1012
import android.content.Intent;
1113
import android.content.IntentFilter;
1214
import android.os.Bundle;
15+
import android.support.v4.app.NotificationCompat;
1316
import android.util.Log;
1417
import android.view.View;
1518
import android.widget.AdapterView;
@@ -19,46 +22,82 @@
1922

2023
public class PubnubActivity extends Activity {
2124

25+
public static final int NOTIFICATION_ID = 1;
26+
private static final String TAG = "PubnubGcm";
27+
private NotificationManager mNotificationManager;
2228
public ArrayList<String> list = new ArrayList<String>();
2329
public ArrayAdapter adapter;
30+
31+
boolean sendNotification = true;
32+
33+
private void sendNotification(String msg) {
34+
mNotificationManager = (NotificationManager)
35+
this.getSystemService(Context.NOTIFICATION_SERVICE);
36+
37+
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
38+
new Intent(this, PubnubActivity.class), 0);
39+
40+
NotificationCompat.Builder mBuilder =
41+
new NotificationCompat.Builder(this)
42+
.setSmallIcon(R.drawable.icon)
43+
.setContentTitle("GCM Notification")
44+
.setStyle(new NotificationCompat.BigTextStyle()
45+
.bigText(msg))
46+
.setContentText(msg);
47+
48+
mBuilder.setContentIntent(contentIntent);
49+
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
50+
}
51+
52+
53+
public void notifyUser(String message) {
54+
if (!sendNotification) {
55+
list.add(0, message);
56+
adapter.notifyDataSetChanged();
57+
} else {
58+
sendNotification(message);
59+
}
60+
}
61+
2462
BroadcastReceiver receiver = new BroadcastReceiver() {
2563

2664
@Override
2765
public void onReceive(Context context, Intent intent) {
28-
list.add(0, intent.getStringExtra("data"));
29-
adapter.notifyDataSetChanged();
66+
notifyUser(intent.getStringExtra("data"));
3067
}
3168
};
3269

70+
@Override
3371
public void onCreate(Bundle savedInstanceState) {
3472
super.onCreate(savedInstanceState);
3573

36-
3774
setContentView(R.layout.activity_pubnub);
3875

39-
4076
ListView listview = (ListView) findViewById(R.id.message_list);
4177

42-
adapter = new ArrayAdapter(this,
43-
android.R.layout.simple_list_item_1, list);
78+
adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, list);
4479

4580
listview.setAdapter(adapter);
4681

47-
final IntentFilter myFilter = new
48-
49-
IntentFilter("android.intent.action.MAIN");
50-
51-
registerReceiver(receiver, myFilter);
52-
53-
54-
// In new versions of Android, the service may not be activated unless an
55-
// associated activity is run at least once. This empty activity serves
56-
// that purpose
82+
IntentFilter myFilter = new IntentFilter("android.intent.action.MAIN");
83+
registerReceiver(receiver, myFilter);
5784

5885
Intent serviceIntent = new Intent(this, PubnubService.class);
5986
startService(serviceIntent);
6087

6188
Log.i("HelloWorldActivity", "PubNub Activity Started!");
6289

6390
}
91+
92+
@Override
93+
public void onResume() {
94+
super.onResume();
95+
sendNotification = false;
96+
}
97+
98+
@Override
99+
public void onPause() {
100+
super.onPause();
101+
sendNotification = true;
102+
}
64103
}

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

Lines changed: 80 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,83 +1,122 @@
11
package com.pubnub.examples.pubnubsubscribe;
22

33

4+
import org.json.JSONArray;
5+
import org.json.JSONException;
6+
47
import android.os.Handler;
58
import android.os.Message;
69
import android.os.PowerManager;
710

811
import com.pubnub.api.Callback;
912
import com.pubnub.api.Pubnub;
13+
import com.pubnub.api.PubnubError;
1014
import com.pubnub.api.PubnubException;
1115

1216

17+
import android.app.NotificationManager;
18+
import android.app.PendingIntent;
1319
import android.app.Service;
1420
import android.content.Context;
1521
import android.content.Intent;
1622
import android.os.IBinder;
23+
import android.support.v4.app.NotificationCompat;
1724
import android.util.Log;
1825
import android.widget.Toast;
1926

2027
public class PubnubService extends Service {
2128

29+
private static final String TAG = "PubnubGcm";
30+
2231
public static final String ACTION_FROM_SERVICE = null;
23-
String channel = "hello_world1";
24-
Pubnub pubnub = new Pubnub("demo", "demo", false);
32+
33+
String device_channel = "Android-1";
34+
String public_channel = "public";
35+
String auth_key = "Android-AuthToken";
36+
String uuid = "Android-user1";
37+
38+
String publish_key = "pam";
39+
String subscribe_key = "pam";
40+
41+
Pubnub pubnub;
2542
PowerManager.WakeLock wl = null;
2643

27-
private final Handler handler = new Handler() {
28-
public void handleMessage(Message msg) {
29-
String pnMsg = msg.obj.toString();
30-
31-
final Toast toast = Toast.makeText(getApplicationContext(), pnMsg, Toast.LENGTH_SHORT);
32-
toast.show();
33-
34-
Handler handler = new Handler();
35-
handler.postDelayed(new Runnable() {
36-
@Override
37-
public void run() {
38-
toast.cancel();
39-
}
40-
}, 200);
41-
42-
}
43-
};
44-
4544
private void notifyUser(Object message) {
46-
47-
Message msg = handler.obtainMessage();
48-
4945
Intent intent = new Intent("android.intent.action.MAIN");
5046
intent.putExtra("data", message.toString());
5147
sendBroadcast(intent);
52-
/*
53-
try {
54-
final String obj = (String) message;
55-
msg.obj = obj;
56-
handler.sendMessage(msg);
57-
Log.i("Received msg : ", obj.toString());
58-
59-
} catch (Exception e) {
60-
e.printStackTrace();
61-
}
62-
*/
6348
}
6449

6550
public void onCreate() {
6651
super.onCreate();
67-
//Toast.makeText(this, "PubnubService created...", Toast.LENGTH_LONG).show();
52+
pubnub = new Pubnub(publish_key, subscribe_key, false);
53+
pubnub.setUUID(uuid);
54+
pubnub.setAuthKey(auth_key);
55+
56+
pubnub.history(public_channel, 3, 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=0; i < 3; i++) {
64+
notifyUser(jsa.get(0));
65+
}
66+
} catch (JSONException e1) {
67+
// TODO Auto-generated catch block
68+
e1.printStackTrace();
69+
}
70+
71+
72+
73+
}
74+
@Override
75+
public void errorCallback(String channel, PubnubError error) {
76+
77+
}
78+
});
79+
6880
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
6981
wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "SubscribeAtBoot");
7082
if (wl != null) {
7183
wl.acquire();
7284
Log.i("PUBNUB", "Partial Wake Lock : " + wl.isHeld());
73-
//Toast.makeText(this, "Partial Wake Lock : " + wl.isHeld(), Toast.LENGTH_LONG).show();
7485
}
7586

7687
Log.i("PUBNUB", "PubnubService created...");
88+
89+
90+
7791
try {
78-
pubnub.subscribe(new String[] {channel}, new Callback() {
92+
pubnub.subscribe(public_channel, new Callback() {
7993
public void connectCallback(String channel) {
80-
//notifyUser("CONNECT on channel:" + channel);
94+
95+
try {
96+
pubnub.subscribe(device_channel, new Callback() {
97+
public void connectCallback(String channel) {
98+
//notifyUser("CONNECT on channel:" + channel);
99+
}
100+
public void disconnectCallback(String channel) {
101+
//notifyUser("DISCONNECT on channel:" + channel);
102+
}
103+
public void reconnectCallback(String channel) {
104+
//notifyUser("RECONNECT on channel:" + channel);
105+
}
106+
@Override
107+
public void successCallback(String channel, Object message) {
108+
notifyUser(message.toString());
109+
}
110+
@Override
111+
public void errorCallback(String channel, Object message) {
112+
notifyUser(message.toString());
113+
}
114+
});
115+
} catch (PubnubException e) {
116+
// TODO Auto-generated catch block
117+
e.printStackTrace();
118+
}
119+
81120
}
82121
public void disconnectCallback(String channel) {
83122
//notifyUser("DISCONNECT on channel:" + channel);
@@ -97,7 +136,10 @@ public void errorCallback(String channel, Object message) {
97136
} catch (PubnubException e) {
98137

99138
}
139+
100140
}
141+
142+
101143

102144
@Override
103145
public void onDestroy() {

0 commit comments

Comments
 (0)