|
2 | 2 |
|
3 | 3 | import java.util.ArrayList; |
4 | 4 |
|
| 5 | +import org.json.JSONException; |
| 6 | +import org.json.JSONObject; |
| 7 | + |
5 | 8 | import com.pubnub.examples.pubnubsubscribe.R; |
6 | 9 |
|
7 | 10 | import android.app.Activity; |
|
11 | 14 | import android.content.Context; |
12 | 15 | import android.content.Intent; |
13 | 16 | import android.content.IntentFilter; |
| 17 | +import android.media.Ringtone; |
| 18 | +import android.media.RingtoneManager; |
| 19 | +import android.net.Uri; |
14 | 20 | import android.os.Bundle; |
15 | 21 | import android.support.v4.app.NotificationCompat; |
16 | 22 | import android.util.Log; |
|
19 | 25 | import android.widget.ArrayAdapter; |
20 | 26 | import android.widget.ListView; |
21 | 27 |
|
22 | | - |
23 | 28 | 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; |
28 | 33 | 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); |
59 | 83 | } |
| 84 | + list.add(0, data.toString()); |
| 85 | + adapter.notifyDataSetChanged(); |
| 86 | + |
60 | 87 | } |
61 | | - |
62 | | - BroadcastReceiver receiver = new BroadcastReceiver() { |
| 88 | + |
| 89 | + BroadcastReceiver receiver = new BroadcastReceiver() { |
63 | 90 |
|
64 | 91 | @Override |
65 | 92 | 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 | + } |
67 | 98 | } |
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 | + } |
103 | 141 | } |
0 commit comments