Skip to content

Commit 3b6ea3e

Browse files
author
Devendra
committed
modifications for background subcribe app
1 parent fc4fd54 commit 3b6ea3e

40 files changed

Lines changed: 367 additions & 249 deletions

‎android/Pubnub-Android-3.6.1.jar‎

-15 Bytes
Binary file not shown.
-15 Bytes
Binary file not shown.

‎android/examples/PubnubSubscribe/AndroidManifest.xml‎

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,34 @@
66
<uses-sdk
77
android:minSdkVersion="8"
88
android:targetSdkVersion="19" />
9-
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
10-
<uses-permission android:name="android.permission.INTERNET"/>
11-
<uses-permission android:name="android.permission.WAKE_LOCK"/>
12-
9+
10+
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
11+
<uses-permission android:name="android.permission.INTERNET" />
12+
<uses-permission android:name="android.permission.WAKE_LOCK" />
13+
1314
<application
1415
android:allowBackup="true"
1516
android:icon="@drawable/ic_launcher"
1617
android:label="@string/app_name"
1718
android:theme="@style/AppTheme" >
18-
19-
<activity android:name=".PubnubActivity" >
19+
<activity
20+
android:name=".PubnubActivity"
21+
android:launchMode="singleTop"
22+
android:theme="@android:style/Theme.Light.NoTitleBar" >
2023
<intent-filter>
21-
<action android:name="android.intent.action.MAIN"/>
22-
<category android:name="android.intent.category.LAUNCHER"/>
24+
<action android:name="android.intent.action.MAIN" />
25+
26+
<category android:name="android.intent.category.LAUNCHER" />
2327
</intent-filter>
2428
</activity>
25-
<receiver android:name=".BootReceiver">
29+
30+
<receiver android:name=".BootReceiver" >
2631
<intent-filter>
27-
<action android:name="android.intent.action.BOOT_COMPLETED"/>
32+
<action android:name="android.intent.action.BOOT_COMPLETED" />
2833
</intent-filter>
2934
</receiver>
3035

31-
<service android:name=".PubnubService"/>
32-
36+
<service android:name=".PubnubService" />
3337
</application>
3438

35-
</manifest>
39+
</manifest>

‎android/examples/PubnubSubscribe/res/layout/activity_pubnub.xml‎

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,27 @@
22
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
33
android:orientation="vertical"
44
android:layout_width="fill_parent"
5-
android:layout_height="fill_parent">
6-
5+
android:layout_height="fill_parent">
6+
7+
<ImageView
8+
android:id="@+id/imageView1"
9+
android:layout_width="150dp"
10+
android:layout_height="wrap_content"
11+
android:layout_gravity="center_vertical|center_horizontal"
12+
android:maxHeight="@dimen/image_max_height"
13+
android:maxWidth="@dimen/image_max_width"
14+
android:src="@drawable/ic_launcher"
15+
android:textAlignment="center" />
16+
717
<ListView
818
android:id="@+id/message_list"
919
android:layout_width="fill_parent"
10-
android:layout_height="fill_parent" >
11-
</ListView>
20+
android:layout_height="fill_parent"
21+
android:padding="@dimen/list_padding" >
22+
23+
</ListView>
24+
25+
1226

13-
</LinearLayout>
27+
</LinearLayout>
28+
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<LinearLayout
3+
xmlns:android="http://schemas.android.com/apk/res/android"
4+
android:layout_width="wrap_content"
5+
android:layout_height="wrap_content">
6+
<TextView
7+
android:layout_width="fill_parent"
8+
android:layout_height="wrap_content"
9+
android:id="@+id/textView"
10+
android:textSize="20px" android:paddingTop="10dip" android:paddingBottom="10dip"/>
11+
</LinearLayout>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<resources>
3+
<dimen name="list_padding">5px</dimen>
4+
<dimen name="image_max_width">100px</dimen>
5+
<dimen name="image_max_height">50px</dimen>
6+
</resources>
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.pubnub.examples.pubnubsubscribe;
2+
3+
import java.util.List;
4+
5+
import android.content.Context;
6+
import android.graphics.Color;
7+
import android.view.LayoutInflater;
8+
import android.view.View;
9+
import android.view.ViewGroup;
10+
import android.widget.ArrayAdapter;
11+
import android.widget.TextView;
12+
13+
14+
class CustomListAdapter extends ArrayAdapter {
15+
16+
private Context mContext;
17+
private int id;
18+
private List <String>items ;
19+
20+
public CustomListAdapter(Context context, int textViewResourceId , List<String> list )
21+
{
22+
super(context, textViewResourceId, list);
23+
mContext = context;
24+
id = textViewResourceId;
25+
items = list ;
26+
}
27+
28+
@Override
29+
public View getView(int position, View v, ViewGroup parent)
30+
{
31+
View mView = v ;
32+
if(mView == null){
33+
LayoutInflater vi = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
34+
mView = vi.inflate(id, null);
35+
}
36+
37+
TextView text = (TextView) mView.findViewById(R.id.textView);
38+
39+
if(items.get(position) != null )
40+
{
41+
text.setTextColor(Color.BLACK);
42+
text.setText(items.get(position));
43+
text.setTextSize(12);
44+
text.setPadding(5, 5, 5, 5);
45+
}
46+
47+
return mView;
48+
}
49+
50+
}

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

Lines changed: 111 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
import java.util.ArrayList;
44

5+
import org.json.JSONException;
6+
import org.json.JSONObject;
7+
58
import com.pubnub.examples.pubnubsubscribe.R;
69

710
import android.app.Activity;
@@ -11,6 +14,9 @@
1114
import android.content.Context;
1215
import android.content.Intent;
1316
import android.content.IntentFilter;
17+
import android.media.Ringtone;
18+
import android.media.RingtoneManager;
19+
import android.net.Uri;
1420
import android.os.Bundle;
1521
import android.support.v4.app.NotificationCompat;
1622
import android.util.Log;
@@ -19,85 +25,117 @@
1925
import android.widget.ArrayAdapter;
2026
import android.widget.ListView;
2127

22-
2328
public class PubnubActivity extends Activity {
24-
25-
public static final int NOTIFICATION_ID = 1;
26-
private static final String TAG = "PubnubGcm";
27-
private NotificationManager mNotificationManager;
29+
30+
public static final int NOTIFICATION_ID = 1;
31+
private static final String TAG = "Pubnub";
32+
private NotificationManager mNotificationManager;
2833
public ArrayList<String> list = new ArrayList<String>();
29-
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);
34+
public CustomListAdapter adapter;
35+
36+
volatile boolean sendNotification = false;
37+
38+
private void sendNotification(String msg) {
39+
mNotificationManager = (NotificationManager) this
40+
.getSystemService(Context.NOTIFICATION_SERVICE);
41+
42+
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
43+
new Intent(this, PubnubActivity.class), 0);
44+
45+
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
46+
this).setSmallIcon(R.drawable.icon)
47+
.setContentTitle("PubNub Notification")
48+
.setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
49+
.setContentText(msg);
50+
51+
mBuilder.setContentIntent(contentIntent);
52+
mBuilder.setAutoCancel(true);
53+
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
54+
55+
try {
56+
Uri notification = RingtoneManager
57+
.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
58+
Ringtone r = RingtoneManager.getRingtone(getApplicationContext(),
59+
notification);
60+
r.play();
61+
} catch (Exception e) {
62+
Log.i("PUBNUB", e.toString());
63+
}
64+
}
65+
66+
public void notifyUser(JSONObject message) {
67+
String notification = null;
68+
Object data = null;
69+
try {
70+
data = message.get("data");
71+
} catch (JSONException e1) {
72+
Log.i("PUBNUB", e1.toString());
73+
return;
74+
}
75+
try {
76+
notification = message.getString("message");
77+
} catch (JSONException e) {
78+
notification = data.toString();
79+
}
80+
81+
if (sendNotification) {
82+
sendNotification(notification);
5983
}
84+
list.add(0, data.toString());
85+
adapter.notifyDataSetChanged();
86+
6087
}
61-
62-
BroadcastReceiver receiver = new BroadcastReceiver() {
88+
89+
BroadcastReceiver receiver = new BroadcastReceiver() {
6390

6491
@Override
6592
public void onReceive(Context context, Intent intent) {
66-
notifyUser(intent.getStringExtra("data"));
93+
try {
94+
notifyUser(new JSONObject(intent.getStringExtra("data")));
95+
} catch (JSONException e) {
96+
Log.i("PUBNUB", e.toString());
97+
}
6798
}
68-
};
69-
70-
@Override
71-
public void onCreate(Bundle savedInstanceState) {
72-
super.onCreate(savedInstanceState);
73-
74-
setContentView(R.layout.activity_pubnub);
75-
76-
ListView listview = (ListView) findViewById(R.id.message_list);
77-
78-
adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, list);
79-
80-
listview.setAdapter(adapter);
81-
82-
IntentFilter myFilter = new IntentFilter("android.intent.action.MAIN");
83-
registerReceiver(receiver, myFilter);
84-
85-
Intent serviceIntent = new Intent(this, PubnubService.class);
86-
startService(serviceIntent);
87-
88-
Log.i("HelloWorldActivity", "PubNub Activity Started!");
89-
90-
}
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-
}
99+
};
100+
101+
@Override
102+
public void onCreate(Bundle savedInstanceState) {
103+
super.onCreate(savedInstanceState);
104+
105+
setContentView(R.layout.activity_pubnub);
106+
107+
ListView listview = (ListView) findViewById(R.id.message_list);
108+
/*
109+
* adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1,
110+
* list);
111+
*
112+
* listview.setAdapter(adapter);
113+
*/
114+
115+
adapter = new CustomListAdapter(this, R.layout.custom_list, list);
116+
listview.setAdapter(adapter);
117+
118+
IntentFilter myFilter = new IntentFilter("android.intent.action.MAIN");
119+
registerReceiver(receiver, myFilter);
120+
121+
Intent serviceIntent = new Intent(this, PubnubService.class);
122+
startService(serviceIntent);
123+
124+
Log.i("PubnubActivity", "PubNub Activity Started!");
125+
126+
}
127+
128+
@Override
129+
public void onResume() {
130+
super.onResume();
131+
Log.i("PUBNUB", "Messages count : " + list.size());
132+
adapter.notifyDataSetChanged();
133+
sendNotification = false;
134+
}
135+
136+
@Override
137+
public void onStop() {
138+
super.onStop();
139+
sendNotification = true;
140+
}
103141
}

0 commit comments

Comments
 (0)