| 21480 |
rajender |
1 |
package com.saholic.profittill;
|
|
|
2 |
|
| 24857 |
tejbeer |
3 |
import android.app.Notification;
|
|
|
4 |
import android.app.NotificationChannel;
|
| 21480 |
rajender |
5 |
import android.app.NotificationManager;
|
|
|
6 |
import android.app.PendingIntent;
|
|
|
7 |
import android.content.Context;
|
|
|
8 |
import android.content.Intent;
|
|
|
9 |
import android.content.SharedPreferences;
|
|
|
10 |
import android.graphics.Bitmap;
|
|
|
11 |
import android.graphics.BitmapFactory;
|
| 24857 |
tejbeer |
12 |
import android.graphics.Color;
|
| 21480 |
rajender |
13 |
import android.media.RingtoneManager;
|
|
|
14 |
import android.net.Uri;
|
|
|
15 |
import android.os.AsyncTask;
|
|
|
16 |
import android.os.Build;
|
|
|
17 |
import android.os.Bundle;
|
|
|
18 |
import android.support.v4.app.NotificationCompat;
|
|
|
19 |
import android.util.Log;
|
|
|
20 |
|
|
|
21 |
import com.google.android.gms.analytics.HitBuilders;
|
|
|
22 |
import com.google.android.gms.analytics.Tracker;
|
|
|
23 |
import com.google.firebase.messaging.FirebaseMessagingService;
|
|
|
24 |
import com.google.firebase.messaging.RemoteMessage;
|
|
|
25 |
import com.mixpanel.android.mpmetrics.MixpanelAPI;
|
|
|
26 |
import com.saholic.profittill.Constants.ProfitTillConstants;
|
|
|
27 |
import com.saholic.profittill.Network.NotificationCount;
|
|
|
28 |
import com.saholic.profittill.Utils.AnalyticsUtility;
|
|
|
29 |
import com.saholic.profittill.Utils.UtilityFunctions;
|
|
|
30 |
import com.saholic.profittill.Volley.Analytics;
|
|
|
31 |
import com.saholic.profittill.Volley.AnalyticsErrorResponse;
|
|
|
32 |
import com.saholic.profittill.Volley.AnalyticsJsonResponse;
|
|
|
33 |
import com.saholic.profittill.main.GoogleAnalyticsTracker;
|
|
|
34 |
import com.saholic.profittill.main.LoginActivity;
|
|
|
35 |
import com.saholic.profittill.main.MainActivity;
|
|
|
36 |
|
|
|
37 |
import org.apache.http.HttpEntity;
|
|
|
38 |
import org.apache.http.HttpResponse;
|
|
|
39 |
import org.apache.http.NameValuePair;
|
|
|
40 |
import org.apache.http.client.HttpClient;
|
|
|
41 |
import org.apache.http.client.entity.UrlEncodedFormEntity;
|
|
|
42 |
import org.apache.http.client.methods.HttpPost;
|
|
|
43 |
import org.apache.http.impl.client.DefaultHttpClient;
|
|
|
44 |
import org.apache.http.message.BasicNameValuePair;
|
|
|
45 |
import org.apache.http.util.EntityUtils;
|
|
|
46 |
import org.json.JSONArray;
|
|
|
47 |
import org.json.JSONException;
|
|
|
48 |
import org.json.JSONObject;
|
|
|
49 |
|
|
|
50 |
import java.io.InputStream;
|
|
|
51 |
import java.net.HttpURLConnection;
|
| 22835 |
rajender |
52 |
import java.net.MalformedURLException;
|
| 21480 |
rajender |
53 |
import java.net.URL;
|
|
|
54 |
import java.util.ArrayList;
|
|
|
55 |
import java.util.Map;
|
|
|
56 |
|
|
|
57 |
/**
|
|
|
58 |
* Created by rajender on 5/4/17.
|
|
|
59 |
*/
|
|
|
60 |
public class MyFirebaseMessagingService extends FirebaseMessagingService {
|
|
|
61 |
SharedPreferences userData;
|
|
|
62 |
SharedPreferences apiData;
|
|
|
63 |
MixpanelAPI mixpanel;
|
|
|
64 |
SharedPreferences.Editor userDataEditor;
|
|
|
65 |
SharedPreferences.Editor apiSettingsEditor;
|
| 22835 |
rajender |
66 |
ArrayList<NameValuePair> nameValuePairsGcm;
|
| 21480 |
rajender |
67 |
public static int NOTIFICATION_ID = 1;
|
|
|
68 |
private NotificationManager mNotificationManager;
|
|
|
69 |
NotificationCompat.Builder builder;
|
|
|
70 |
SharedPreferences notificationData;
|
|
|
71 |
SharedPreferences.Editor notificationDataEditor;
|
|
|
72 |
private static final String TAG = "MyFirebaseMsgService";
|
| 24857 |
tejbeer |
73 |
String channelId = "123";
|
|
|
74 |
CharSequence channelName = "SmartDukaan";
|
| 21480 |
rajender |
75 |
Intent intent;
|
| 22835 |
rajender |
76 |
Bitmap bitmap;
|
| 21480 |
rajender |
77 |
|
|
|
78 |
@Override
|
|
|
79 |
public void onMessageReceived(RemoteMessage remoteMessage) {
|
| 24857 |
tejbeer |
80 |
super.onMessageReceived(remoteMessage);
|
| 21480 |
rajender |
81 |
userData = this.getSharedPreferences("User_Data", Context.MODE_PRIVATE);
|
|
|
82 |
apiData = this.getSharedPreferences("API_Data", Context.MODE_PRIVATE);
|
|
|
83 |
userDataEditor = userData.edit();
|
|
|
84 |
apiSettingsEditor = apiData.edit();
|
|
|
85 |
notificationData = this.getSharedPreferences("Notification_Data", Context.MODE_PRIVATE);
|
|
|
86 |
notificationDataEditor = notificationData.edit();
|
|
|
87 |
mixpanel = MixpanelAPI.getInstance(getApplicationContext(), ProfitTillConstants.MIX_PANEL_TOKEN);
|
|
|
88 |
Map<String, String> data = remoteMessage.getData();
|
|
|
89 |
if (data.containsKey("image")) {
|
|
|
90 |
String imageUri = data.get("image");
|
|
|
91 |
bitmap = getBitmapfromUrl(imageUri);
|
|
|
92 |
|
|
|
93 |
}
|
|
|
94 |
try {
|
|
|
95 |
if(data.get("type").equalsIgnoreCase("update")){
|
|
|
96 |
new CheckNotificationExpiry().execute(data);
|
|
|
97 |
}
|
|
|
98 |
String campaignUserId = data.get("url").split("user_id=")[1];
|
|
|
99 |
if (userData.getString("id", "0").equalsIgnoreCase(campaignUserId)) {
|
|
|
100 |
new Analytics(ProfitTillConstants.ANALYTICS_URL,
|
|
|
101 |
AnalyticsUtility.getAnalyticsRequest(getApplicationContext(), userData.getString("id", ""), "Notification", "Check Expiry", data.get("cid") + ""),
|
|
|
102 |
AnalyticsJsonResponse.getAnalyticsRequestInstance(),
|
| 24857 |
tejbeer |
103 |
|
| 21480 |
rajender |
104 |
AnalyticsErrorResponse.getAnalyitcsResponseInstance()).anlyticsRequest(getApplicationContext());
|
|
|
105 |
new CheckNotificationExpiry().execute(data);
|
|
|
106 |
}
|
|
|
107 |
|
|
|
108 |
|
|
|
109 |
}
|
|
|
110 |
catch (Exception e) {
|
|
|
111 |
e.printStackTrace();
|
|
|
112 |
Tracker t = ((GoogleAnalyticsTracker) getApplicationContext()).getTracker(
|
|
|
113 |
GoogleAnalyticsTracker.TrackerName.APP_TRACKER);
|
|
|
114 |
t.send(new HitBuilders.ExceptionBuilder()
|
|
|
115 |
.setDescription("Exception while Checking" + e.getMessage())
|
|
|
116 |
.build());
|
|
|
117 |
}
|
|
|
118 |
|
|
|
119 |
}
|
|
|
120 |
|
|
|
121 |
class CheckNotificationExpiry extends AsyncTask<Map, Integer, String> {
|
|
|
122 |
Map extras;
|
|
|
123 |
|
|
|
124 |
@Override
|
|
|
125 |
protected String doInBackground(Map... params) {
|
|
|
126 |
extras = params[0];
|
|
|
127 |
try {
|
|
|
128 |
HttpClient httpclient = new DefaultHttpClient();
|
|
|
129 |
String notificationCheckUrl = apiData.getString("notification.check.url", "http://api.profittill.com/notification_campaigns/notificationactive");
|
|
|
130 |
HttpPost httppost = new HttpPost(notificationCheckUrl + "?cid=" + extras.get("cid") + "&imeinumber=" + UtilityFunctions.getImeiNumber(getApplicationContext()));
|
|
|
131 |
httppost.setHeader("Authorization", ProfitTillConstants.BASIC_AUTH);
|
|
|
132 |
HttpResponse response = httpclient.execute(httppost);
|
|
|
133 |
HttpEntity entity = response.getEntity();
|
|
|
134 |
JSONObject jObjGmail = new JSONObject(EntityUtils.toString(entity));
|
|
|
135 |
int status = response.getStatusLine().getStatusCode();
|
|
|
136 |
return String.valueOf(jObjGmail.getString("success"));
|
|
|
137 |
// return jObjGmail.getString("success");
|
|
|
138 |
} catch (Exception e) {
|
|
|
139 |
e.printStackTrace();
|
|
|
140 |
Log.e("Fail 1", e.toString());
|
|
|
141 |
return "true";
|
|
|
142 |
}
|
|
|
143 |
|
|
|
144 |
}
|
|
|
145 |
|
| 22381 |
rajender |
146 |
@Override
|
|
|
147 |
public String toString() {
|
|
|
148 |
return super.toString();
|
|
|
149 |
}
|
|
|
150 |
|
|
|
151 |
@Override
|
|
|
152 |
protected void onPostExecute(String result) {
|
|
|
153 |
super.onPostExecute(result);
|
|
|
154 |
if (result.isEmpty() || result.equals(null) || result.equalsIgnoreCase("true")) {
|
|
|
155 |
if (notificationData.getString("cid", "").equalsIgnoreCase(extras.get("cid").toString().split("_")[0])) {
|
|
|
156 |
Log.d("Campaign Id is same " + notificationData.getString("cid", ""), "Campaign Id is same " + extras.get("cid").toString().split("_")[0]);
|
| 21480 |
rajender |
157 |
}
|
| 22381 |
rajender |
158 |
else {
|
|
|
159 |
notificationDataEditor.putString("cid", extras.get("cid").toString().split("_")[0]);
|
|
|
160 |
notificationDataEditor.commit();
|
| 22835 |
rajender |
161 |
try {
|
|
|
162 |
sendNotification(extras.get("message") + "", extras.get("type") + "", extras.get("title") + "", extras.get("url") + "", extras.get("cid") + "", bitmap);
|
|
|
163 |
} catch (MalformedURLException e) {
|
|
|
164 |
e.printStackTrace();
|
|
|
165 |
}
|
| 22381 |
rajender |
166 |
}
|
|
|
167 |
} else {
|
|
|
168 |
new Analytics(ProfitTillConstants.ANALYTICS_URL,
|
|
|
169 |
AnalyticsUtility.getAnalyticsRequest(getApplicationContext(), userData.getString("id", ""), "Notification", "Notification Expired", extras.get("cid") + ""),
|
|
|
170 |
AnalyticsJsonResponse.getAnalyticsRequestInstance(),
|
|
|
171 |
AnalyticsErrorResponse.getAnalyitcsResponseInstance()).anlyticsRequest(getApplicationContext());
|
|
|
172 |
nameValuePairsGcm = new ArrayList<>();
|
|
|
173 |
nameValuePairsGcm.add(new BasicNameValuePair("user_id", userData.getString("id", "")));
|
|
|
174 |
nameValuePairsGcm.add(new BasicNameValuePair("cid", extras.get("cid") + ""));
|
|
|
175 |
nameValuePairsGcm.add(new BasicNameValuePair("timestamp", UtilityFunctions.notificationDate()));
|
|
|
176 |
nameValuePairsGcm.add(new BasicNameValuePair("result", "expired"));
|
|
|
177 |
new NotificationRecievedData().execute(nameValuePairsGcm);
|
| 21480 |
rajender |
178 |
}
|
|
|
179 |
}
|
| 22381 |
rajender |
180 |
}
|
| 21480 |
rajender |
181 |
|
| 22835 |
rajender |
182 |
private void sendNotification(String msg, String type, String title, String url, String cid, Bitmap image) throws MalformedURLException {
|
| 21480 |
rajender |
183 |
JSONObject propsNotificationRecieved = new JSONObject();
|
|
|
184 |
Bundle bundle;
|
|
|
185 |
try {
|
|
|
186 |
mixpanel.identify(userData.getString("id", null));
|
|
|
187 |
propsNotificationRecieved.put("Screen", "Notification Reached");
|
|
|
188 |
mixpanel.track("Notification Recieved", propsNotificationRecieved
|
|
|
189 |
);
|
|
|
190 |
} catch (JSONException e) {
|
|
|
191 |
e.printStackTrace();
|
|
|
192 |
}
|
|
|
193 |
if (title.isEmpty()) {
|
|
|
194 |
title = "ProfitMandi";
|
|
|
195 |
}
|
|
|
196 |
Intent resultIntent;
|
|
|
197 |
mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
|
|
|
198 |
if (userData.getString("id", "").isEmpty()) {
|
|
|
199 |
resultIntent = new Intent(this, LoginActivity.class);
|
|
|
200 |
resultIntent.setAction("Login");
|
|
|
201 |
bundle = new Bundle();
|
|
|
202 |
bundle.putString("cid", cid);
|
|
|
203 |
resultIntent.putExtras(bundle);
|
|
|
204 |
} /*else if (userData.getString("referralCodeRequired", "false").equalsIgnoreCase("true")) {
|
|
|
205 |
//resultIntent = new Intent(this, ReferrerActivity.class);
|
|
|
206 |
//resultIntent.setAction("Login");
|
|
|
207 |
//bundle = new Bundle();
|
|
|
208 |
// bundle.putString("cid", cid);
|
|
|
209 |
//resultIntent.putExtras(bundle);} */
|
| 22381 |
rajender |
210 |
else {
|
| 21480 |
rajender |
211 |
if (type.equalsIgnoreCase("Url")) {
|
|
|
212 |
resultIntent = new Intent(this, MainActivity.class);
|
|
|
213 |
bundle = new Bundle();
|
|
|
214 |
bundle.putString("cid", cid);
|
|
|
215 |
resultIntent.putExtras(bundle);
|
|
|
216 |
resultIntent.setAction("20");
|
|
|
217 |
|
|
|
218 |
if (url.isEmpty()) {
|
| 22835 |
rajender |
219 |
resultIntent.setData(Uri.parse(apiData.getString("app.domain.profitmandi", "app.profitmandi.com")));
|
| 21480 |
rajender |
220 |
} else {
|
| 22835 |
rajender |
221 |
URL urlString = new URL(url);
|
|
|
222 |
String domain = urlString.getAuthority();
|
|
|
223 |
String path =urlString.getFile();
|
|
|
224 |
String protocol=urlString.getProtocol();
|
|
|
225 |
String rr=urlString.getRef();
|
|
|
226 |
|
|
|
227 |
if(domain.equals(apiData.getString("app.domain.profitmandi", "app.profitmandi.com"))){
|
|
|
228 |
resultIntent.setData(Uri.parse(url));
|
|
|
229 |
}
|
|
|
230 |
else{
|
|
|
231 |
url=protocol+"://"+(apiData.getString("app.domain.profitmandi", "app.profitmandi.com")) + path;
|
|
|
232 |
resultIntent.setData(Uri.parse(url));
|
|
|
233 |
}
|
|
|
234 |
|
|
|
235 |
|
| 21480 |
rajender |
236 |
}
|
|
|
237 |
|
|
|
238 |
} else if (type.equalsIgnoreCase("Update")) {
|
|
|
239 |
final String appPackageName = getPackageName(); // getPackageName() from Context or Activity object
|
|
|
240 |
try {
|
|
|
241 |
resultIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + appPackageName));
|
|
|
242 |
} catch (android.content.ActivityNotFoundException anfe) {
|
|
|
243 |
resultIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + appPackageName));
|
|
|
244 |
}
|
|
|
245 |
} else {
|
|
|
246 |
if (url.contains("Profile")) {
|
|
|
247 |
resultIntent = new Intent(this, MainActivity.class);
|
|
|
248 |
bundle = new Bundle();
|
|
|
249 |
bundle.putString("cid", cid);
|
|
|
250 |
resultIntent.putExtras(bundle);
|
|
|
251 |
resultIntent.setAction("15");
|
|
|
252 |
} else if (url.contains("Tutorial")) {
|
|
|
253 |
resultIntent = new Intent(this, MainActivity.class);
|
|
|
254 |
bundle = new Bundle();
|
|
|
255 |
bundle.putString("cid", cid);
|
|
|
256 |
resultIntent.putExtras(bundle);
|
|
|
257 |
resultIntent.setAction("7");
|
|
|
258 |
} else if (url.contains("Contact")) {
|
|
|
259 |
resultIntent = new Intent(this, MainActivity.class);
|
|
|
260 |
bundle = new Bundle();
|
|
|
261 |
bundle.putString("cid", cid);
|
|
|
262 |
resultIntent.putExtras(bundle);
|
|
|
263 |
resultIntent.setAction("6");
|
|
|
264 |
} else {
|
|
|
265 |
resultIntent = new Intent(this, MainActivity.class);
|
|
|
266 |
bundle = new Bundle();
|
|
|
267 |
bundle.putString("cid", cid);
|
|
|
268 |
resultIntent.putExtras(bundle);
|
|
|
269 |
}
|
|
|
270 |
}
|
|
|
271 |
}
|
|
|
272 |
PendingIntent contentIntent = PendingIntent.getActivity(this, NOTIFICATION_ID, resultIntent, 0);
|
|
|
273 |
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
|
| 24857 |
tejbeer |
274 |
Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.notification);
|
| 21480 |
rajender |
275 |
if (image==null) {
|
| 24857 |
tejbeer |
276 |
|
|
|
277 |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
|
|
278 |
|
|
|
279 |
NotificationChannel notificationChannel = new NotificationChannel(channelId, channelName,
|
|
|
280 |
NotificationManager.IMPORTANCE_HIGH);
|
|
|
281 |
notificationChannel.enableLights(true);
|
|
|
282 |
notificationChannel.setLightColor(Color.RED);
|
|
|
283 |
notificationChannel.enableVibration(true);
|
|
|
284 |
notificationChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
|
|
|
285 |
mNotificationManager.createNotificationChannel(notificationChannel);
|
|
|
286 |
Notification.Builder mBuilder = new Notification.Builder(this, channelId);
|
|
|
287 |
if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
|
|
|
288 |
mBuilder.setSmallIcon(R.drawable.sdnotifictation);
|
|
|
289 |
} else{
|
|
|
290 |
mBuilder.setSmallIcon(R.drawable.transpaentnavigation);
|
|
|
291 |
}
|
|
|
292 |
mBuilder.setContentTitle(title);
|
|
|
293 |
mBuilder.setStyle(new Notification.BigTextStyle().bigText(msg));
|
|
|
294 |
mBuilder.setContentText(msg);
|
|
|
295 |
mBuilder.setLargeIcon(bm);
|
|
|
296 |
mBuilder.setAutoCancel(true);
|
|
|
297 |
mBuilder.setContentIntent(contentIntent);
|
|
|
298 |
mNotificationManager.notify(NOTIFICATION_ID,mBuilder.build());
|
| 21480 |
rajender |
299 |
}
|
| 24857 |
tejbeer |
300 |
else {
|
|
|
301 |
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
|
|
|
302 |
if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
|
|
|
303 |
mBuilder.setSmallIcon(R.drawable.sdnotifictation);
|
|
|
304 |
} else {
|
|
|
305 |
mBuilder.setSmallIcon(R.drawable.transpaentnavigation);
|
|
|
306 |
}
|
|
|
307 |
mBuilder.setContentTitle(title);
|
|
|
308 |
mBuilder.setStyle(new NotificationCompat.BigTextStyle().bigText(msg));
|
|
|
309 |
mBuilder.setContentText(msg);
|
|
|
310 |
mBuilder.setLargeIcon(bm);
|
|
|
311 |
mBuilder.setAutoCancel(true);
|
|
|
312 |
mBuilder.setSound(Uri.parse("android.resource://"
|
|
|
313 |
+ getBaseContext().getPackageName() + "/" + R.raw.slow));
|
|
|
314 |
mBuilder.setContentIntent(contentIntent);
|
|
|
315 |
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
|
|
|
316 |
}
|
| 21480 |
rajender |
317 |
} else {
|
| 24857 |
tejbeer |
318 |
|
|
|
319 |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
|
|
320 |
NotificationChannel notificationChannel = new NotificationChannel(channelId, channelName,
|
|
|
321 |
NotificationManager.IMPORTANCE_HIGH);
|
|
|
322 |
notificationChannel.enableLights(true);
|
|
|
323 |
notificationChannel.setLightColor(Color.RED);
|
|
|
324 |
notificationChannel.enableVibration(true);
|
|
|
325 |
notificationChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
|
|
|
326 |
mNotificationManager.createNotificationChannel(notificationChannel);
|
|
|
327 |
Notification.Builder mBuilder = new Notification.Builder(this, channelId);
|
|
|
328 |
if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
|
|
|
329 |
mBuilder.setSmallIcon(R.drawable.sdnotifictation);
|
|
|
330 |
} else {
|
|
|
331 |
mBuilder.setSmallIcon(R.drawable.transpaentnavigation);
|
|
|
332 |
}
|
|
|
333 |
mBuilder.setContentTitle(title);
|
|
|
334 |
mBuilder.setStyle(new Notification.BigTextStyle().bigText(msg));
|
|
|
335 |
mBuilder.setStyle(new Notification.BigPictureStyle().bigPicture(image));
|
|
|
336 |
mBuilder.setContentText(msg);
|
|
|
337 |
mBuilder.setLargeIcon(bm);
|
|
|
338 |
mBuilder.setAutoCancel(true);
|
|
|
339 |
mBuilder.setContentIntent(contentIntent);
|
|
|
340 |
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
|
|
|
341 |
} else {
|
|
|
342 |
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
|
|
|
343 |
if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
|
|
|
344 |
mBuilder.setSmallIcon(R.drawable.sdnotifictation);
|
|
|
345 |
} else {
|
|
|
346 |
mBuilder.setSmallIcon(R.drawable.transpaentnavigation);
|
|
|
347 |
}
|
|
|
348 |
mBuilder.setContentTitle(title);
|
|
|
349 |
mBuilder.setStyle(new NotificationCompat.BigTextStyle().bigText(msg));
|
|
|
350 |
mBuilder.setStyle(new NotificationCompat.BigPictureStyle().bigPicture(image));
|
|
|
351 |
mBuilder.setContentText(msg);
|
|
|
352 |
mBuilder.setLargeIcon(bm);
|
|
|
353 |
mBuilder.setAutoCancel(true);
|
|
|
354 |
mBuilder.setSound(alarmSound);
|
|
|
355 |
mBuilder.setContentIntent(contentIntent);
|
|
|
356 |
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
|
| 21480 |
rajender |
357 |
}
|
| 24857 |
tejbeer |
358 |
|
| 21480 |
rajender |
359 |
}
|
|
|
360 |
Tracker t = ((GoogleAnalyticsTracker) getApplicationContext()).getTracker(
|
|
|
361 |
GoogleAnalyticsTracker.TrackerName.APP_TRACKER);
|
|
|
362 |
t.send(new HitBuilders.EventBuilder()
|
|
|
363 |
.setCategory("Notification")
|
|
|
364 |
.setAction("Building Notification for campaign Id " + cid)
|
|
|
365 |
.setLabel("For User Id " + userData.getString("id", "0"))
|
|
|
366 |
.build());
|
|
|
367 |
new Analytics(ProfitTillConstants.ANALYTICS_URL,
|
|
|
368 |
AnalyticsUtility.getAnalyticsRequest(getApplicationContext(), userData.getString("id", ""), "Notification", "Notification Received", cid),
|
|
|
369 |
AnalyticsJsonResponse.getAnalyticsRequestInstance(),
|
|
|
370 |
AnalyticsErrorResponse.getAnalyitcsResponseInstance()).anlyticsRequest(getApplicationContext());
|
|
|
371 |
++NOTIFICATION_ID;
|
|
|
372 |
nameValuePairsGcm = new ArrayList<>();
|
|
|
373 |
nameValuePairsGcm.add(new BasicNameValuePair("user_id", userData.getString("id", "")));
|
|
|
374 |
nameValuePairsGcm.add(new BasicNameValuePair("cid", cid));
|
|
|
375 |
nameValuePairsGcm.add(new BasicNameValuePair("timestamp", UtilityFunctions.notificationDate()));
|
|
|
376 |
nameValuePairsGcm.add(new BasicNameValuePair("result", "recieved"));
|
|
|
377 |
String userId = userData.getString("id", "");
|
|
|
378 |
String androidId = UtilityFunctions.androidId(GoogleAnalyticsTracker.getAppContext());
|
|
|
379 |
if (!userId.isEmpty() && !androidId.isEmpty()) {
|
|
|
380 |
String notificationUrl = apiData.getString("notification.count", "http://45.33.50.227:3001/getNotificationCount") + "?user_id=" + userId + "&android_id=" + androidId;
|
|
|
381 |
new NotificationCount().getCount(GoogleAnalyticsTracker.getAppContext(), null, notificationUrl);
|
|
|
382 |
}
|
|
|
383 |
new NotificationRecievedData().execute(nameValuePairsGcm);
|
|
|
384 |
}
|
|
|
385 |
|
|
|
386 |
class NotificationRecievedData extends AsyncTask<ArrayList<NameValuePair>, Integer, String> {
|
|
|
387 |
|
|
|
388 |
@Override
|
|
|
389 |
protected void onPreExecute() {
|
|
|
390 |
super.onPreExecute();
|
|
|
391 |
Tracker t = ((GoogleAnalyticsTracker) getApplicationContext()).getTracker(
|
|
|
392 |
GoogleAnalyticsTracker.TrackerName.APP_TRACKER);
|
|
|
393 |
t.send(new HitBuilders.EventBuilder()
|
|
|
394 |
.setCategory("Notification")
|
|
|
395 |
.setAction("Received Notification")
|
|
|
396 |
.setLabel("For User Id " + userData.getString("id", "0"))
|
|
|
397 |
.build());
|
|
|
398 |
}
|
|
|
399 |
|
|
|
400 |
@Override
|
|
|
401 |
protected String doInBackground(ArrayList<NameValuePair>... arg0) {
|
|
|
402 |
|
|
|
403 |
try {
|
|
|
404 |
HttpClient httpclient = new DefaultHttpClient();
|
|
|
405 |
HttpPost httppost = new HttpPost(apiData.getString("notification.data.url", "http://api.profittill.com/pushnotifications/add"));
|
|
|
406 |
httppost.setHeader("Authorization", ProfitTillConstants.BASIC_AUTH);
|
|
|
407 |
httppost.setEntity(new UrlEncodedFormEntity(arg0[0]));
|
|
|
408 |
HttpResponse response = httpclient.execute(httppost);
|
|
|
409 |
HttpEntity entity = response.getEntity();
|
|
|
410 |
int status = response.getStatusLine().getStatusCode();
|
|
|
411 |
nameValuePairsGcm.clear();
|
|
|
412 |
}
|
| 24857 |
tejbeer |
413 |
catch (Exception e) {
|
|
|
414 |
|
|
|
415 |
}
|
| 21480 |
rajender |
416 |
return "success";
|
|
|
417 |
}
|
|
|
418 |
|
|
|
419 |
@Override
|
|
|
420 |
protected void onPostExecute(String result) {
|
|
|
421 |
super.onPostExecute(result);
|
|
|
422 |
}
|
|
|
423 |
}
|
|
|
424 |
|
|
|
425 |
public void sendNotificationByPolling(JSONArray objects,Context c) {
|
|
|
426 |
userData = GoogleAnalyticsTracker.getAppContext().getSharedPreferences("User_Data", Context.MODE_PRIVATE);
|
|
|
427 |
apiData = GoogleAnalyticsTracker.getAppContext().getSharedPreferences("API_Data", Context.MODE_PRIVATE);
|
|
|
428 |
notificationData = GoogleAnalyticsTracker.getAppContext().getSharedPreferences("Notification_Data", Context.MODE_PRIVATE);
|
| 24857 |
tejbeer |
429 |
userDataEditor = userData.edit();
|
| 21480 |
rajender |
430 |
apiSettingsEditor = apiData.edit();
|
|
|
431 |
notificationDataEditor = notificationData.edit();
|
|
|
432 |
|
|
|
433 |
Bundle bundle;
|
|
|
434 |
|
|
|
435 |
try {
|
|
|
436 |
if (objects.length() != 0) {
|
|
|
437 |
JSONObject obj = new JSONObject();
|
|
|
438 |
JSONArray jsArray = new JSONArray();
|
|
|
439 |
for (int i = 0; i < objects.length(); i++) {
|
|
|
440 |
JSONObject j = objects.getJSONObject(i);
|
|
|
441 |
String msg = j.getString("message");
|
|
|
442 |
String type = j.getString("type");
|
|
|
443 |
String title = j.getString("title");
|
|
|
444 |
String url = j.getString("url");
|
|
|
445 |
String cid = j.getString("cid");
|
|
|
446 |
String image=null;
|
|
|
447 |
if(j.has("image")) {
|
|
|
448 |
image = j.getString("image");
|
|
|
449 |
bitmap=getBitmapfromUrl(image);
|
|
|
450 |
}
|
|
|
451 |
if (notificationData.getString("cid", "").equalsIgnoreCase(cid.split("_")[0])) {
|
|
|
452 |
Log.d("Campaign Id is same " + notificationData.getString("cid", ""), "Campaign Id is same " + cid.split("_")[0]);
|
|
|
453 |
} else {
|
|
|
454 |
notificationDataEditor.putString("cid", cid.split("_")[0]);
|
|
|
455 |
notificationDataEditor.commit();
|
|
|
456 |
if (title.isEmpty()) {
|
|
|
457 |
title = "ProfitMandi";
|
|
|
458 |
}
|
|
|
459 |
Intent resultIntent;
|
|
|
460 |
mNotificationManager = (NotificationManager) GoogleAnalyticsTracker.getAppContext().getSystemService(NOTIFICATION_SERVICE);
|
|
|
461 |
if (userData.getString("id", "").isEmpty()) {
|
|
|
462 |
resultIntent = new Intent(GoogleAnalyticsTracker.getAppContext(), LoginActivity.class);
|
|
|
463 |
resultIntent.setAction("Login");
|
|
|
464 |
bundle = new Bundle();
|
|
|
465 |
bundle.putString("cid", cid);
|
|
|
466 |
resultIntent.putExtras(bundle);
|
|
|
467 |
}
|
|
|
468 |
/*else if (userData.getString("referralCodeRequired", "false").equalsIgnoreCase("true")) {
|
|
|
469 |
resultIntent = new Intent(GoogleAnalyticsTracker.getAppContext(), ReferrerActivity.class);
|
|
|
470 |
resultIntent.setAction("Login");
|
|
|
471 |
bundle = new Bundle();
|
|
|
472 |
bundle.putString("cid", cid);
|
|
|
473 |
resultIntent.putExtras(bundle);
|
|
|
474 |
}*/
|
|
|
475 |
else {
|
|
|
476 |
if (type.equalsIgnoreCase("Url")) {
|
|
|
477 |
resultIntent = new Intent(GoogleAnalyticsTracker.getAppContext(), MainActivity.class);
|
|
|
478 |
bundle = new Bundle();
|
|
|
479 |
bundle.putString("cid", cid);
|
|
|
480 |
resultIntent.putExtras(bundle);
|
|
|
481 |
resultIntent.setAction("20");
|
|
|
482 |
|
| 22835 |
rajender |
483 |
|
| 21480 |
rajender |
484 |
if (url.isEmpty()) {
|
| 22835 |
rajender |
485 |
resultIntent.setData(Uri.parse(apiData.getString("app.domain.profitmandi", "http://app.profitmandi.com")));
|
| 21480 |
rajender |
486 |
} else {
|
| 22835 |
rajender |
487 |
URL urlString = new URL(url);
|
|
|
488 |
String domain = urlString.getAuthority();
|
|
|
489 |
String path =urlString.getFile();
|
|
|
490 |
String protocol=urlString.getProtocol();
|
|
|
491 |
String rr=urlString.getRef();
|
|
|
492 |
|
|
|
493 |
if(domain.equals(apiData.getString("app.domain.profitmandi", "app.profitmandi.com"))){
|
|
|
494 |
resultIntent.setData(Uri.parse(url));
|
|
|
495 |
}
|
|
|
496 |
else{
|
|
|
497 |
url=protocol+"://"+(apiData.getString("app.domain.profitmandi", "app.profitmandi.com")) + path;
|
|
|
498 |
resultIntent.setData(Uri.parse(url));
|
|
|
499 |
}
|
|
|
500 |
|
| 21480 |
rajender |
501 |
}
|
|
|
502 |
|
|
|
503 |
} else if (type.equalsIgnoreCase("Update")) {
|
|
|
504 |
final String appPackageName = GoogleAnalyticsTracker.getAppContext().getPackageName();
|
|
|
505 |
try {
|
|
|
506 |
resultIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + appPackageName));
|
|
|
507 |
} catch (android.content.ActivityNotFoundException anfe) {
|
|
|
508 |
resultIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + appPackageName));
|
|
|
509 |
}
|
|
|
510 |
} else {
|
|
|
511 |
if (url.contains("Profile")) {
|
|
|
512 |
resultIntent = new Intent(GoogleAnalyticsTracker.getAppContext(), MainActivity.class);
|
|
|
513 |
bundle = new Bundle();
|
|
|
514 |
bundle.putString("cid", cid);
|
|
|
515 |
resultIntent.putExtras(bundle);
|
|
|
516 |
resultIntent.setAction("15");
|
|
|
517 |
} else if (url.contains("Tutorial")) {
|
|
|
518 |
resultIntent = new Intent(GoogleAnalyticsTracker.getAppContext(), MainActivity.class);
|
|
|
519 |
bundle = new Bundle();
|
|
|
520 |
bundle.putString("cid", cid);
|
|
|
521 |
resultIntent.putExtras(bundle);
|
|
|
522 |
resultIntent.setAction("7");
|
|
|
523 |
} else if (url.contains("Contact")) {
|
|
|
524 |
resultIntent = new Intent(GoogleAnalyticsTracker.getAppContext(), MainActivity.class);
|
|
|
525 |
bundle = new Bundle();
|
|
|
526 |
bundle.putString("cid", cid);
|
|
|
527 |
resultIntent.putExtras(bundle);
|
|
|
528 |
resultIntent.setAction("6");
|
|
|
529 |
} else {
|
|
|
530 |
resultIntent = new Intent(GoogleAnalyticsTracker.getAppContext(), MainActivity.class);
|
|
|
531 |
bundle = new Bundle();
|
|
|
532 |
bundle.putString("cid", cid);
|
|
|
533 |
resultIntent.putExtras(bundle);
|
|
|
534 |
}
|
|
|
535 |
}
|
|
|
536 |
}
|
|
|
537 |
PendingIntent contentIntent = PendingIntent.getActivity(GoogleAnalyticsTracker.getAppContext(), NOTIFICATION_ID, resultIntent, 0);
|
|
|
538 |
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
|
| 24857 |
tejbeer |
539 |
Bitmap bm = BitmapFactory.decodeResource(GoogleAnalyticsTracker.getAppContext().getResources(), R.drawable.notification);
|
| 21480 |
rajender |
540 |
if (bitmap==null) {
|
|
|
541 |
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
|
| 24857 |
tejbeer |
542 |
c );
|
|
|
543 |
if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
|
|
|
544 |
mBuilder.setSmallIcon(R.drawable.sdnotifictation);
|
|
|
545 |
} else {
|
|
|
546 |
mBuilder.setSmallIcon(R.drawable.transpaentnavigation);
|
|
|
547 |
}
|
|
|
548 |
mBuilder .setContentTitle(title);
|
|
|
549 |
mBuilder.setStyle(new NotificationCompat.BigTextStyle().bigText(msg));
|
|
|
550 |
mBuilder.setContentText(msg);
|
|
|
551 |
mBuilder .setLargeIcon(bm);
|
|
|
552 |
mBuilder.setAutoCancel(true);
|
|
|
553 |
mBuilder .setSound(alarmSound);
|
| 21480 |
rajender |
554 |
mBuilder.setContentIntent(contentIntent);
|
|
|
555 |
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
|
|
|
556 |
}
|
|
|
557 |
|
|
|
558 |
else {
|
|
|
559 |
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
|
| 24857 |
tejbeer |
560 |
GoogleAnalyticsTracker.getAppContext());
|
|
|
561 |
if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
|
|
|
562 |
mBuilder.setSmallIcon(R.drawable.sdnotifictation);
|
|
|
563 |
} else {
|
|
|
564 |
mBuilder.setSmallIcon(R.drawable.transpaentnavigation);
|
|
|
565 |
}
|
|
|
566 |
mBuilder.setContentTitle(title);
|
|
|
567 |
mBuilder.setStyle(new NotificationCompat.BigTextStyle().bigText(msg));
|
|
|
568 |
mBuilder.setStyle(new NotificationCompat.BigPictureStyle().bigPicture(bitmap));
|
|
|
569 |
mBuilder.setContentText(msg);
|
|
|
570 |
mBuilder.setLargeIcon(bm);
|
|
|
571 |
mBuilder.setAutoCancel(true);
|
|
|
572 |
mBuilder.setSound(alarmSound);
|
| 21480 |
rajender |
573 |
mBuilder.setContentIntent(contentIntent);
|
|
|
574 |
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
|
|
|
575 |
}
|
|
|
576 |
++NOTIFICATION_ID;
|
|
|
577 |
JSONObject data = new JSONObject();
|
|
|
578 |
data.put("cid", cid);
|
|
|
579 |
data.put("user_id", userData.getString("id", null));
|
|
|
580 |
data.put("timestamp", UtilityFunctions.notificationDate());
|
|
|
581 |
data.put("result", "recieved");
|
|
|
582 |
jsArray.put(data);
|
|
|
583 |
}
|
|
|
584 |
obj.put("pushdata", jsArray);
|
|
|
585 |
String userId = userData.getString("id", "");
|
|
|
586 |
String androidId = UtilityFunctions.androidId(GoogleAnalyticsTracker.getAppContext());
|
|
|
587 |
if (!userId.isEmpty() && !androidId.isEmpty()) {
|
|
|
588 |
String notificationUrl = apiData.getString("notification.count", "") + "?user_id=" + userId + "&android_id=" + androidId;
|
|
|
589 |
new NotificationCount().getCount(GoogleAnalyticsTracker.getAppContext(), null, notificationUrl);
|
|
|
590 |
}
|
|
|
591 |
}
|
|
|
592 |
new PollNotificationRecievedData().execute(obj);
|
|
|
593 |
}
|
|
|
594 |
}
|
|
|
595 |
catch (Exception e) {
|
|
|
596 |
e.printStackTrace();
|
|
|
597 |
}
|
|
|
598 |
}
|
|
|
599 |
|
|
|
600 |
class PollNotificationRecievedData extends AsyncTask<JSONObject, Integer, String> {
|
|
|
601 |
|
|
|
602 |
@Override
|
|
|
603 |
protected void onPreExecute() {
|
|
|
604 |
super.onPreExecute();
|
|
|
605 |
}
|
|
|
606 |
|
|
|
607 |
@Override
|
|
|
608 |
protected String doInBackground(JSONObject... arg0) {
|
|
|
609 |
|
|
|
610 |
try {
|
|
|
611 |
ArrayList<NameValuePair> nameValuePairs = new ArrayList<>();
|
|
|
612 |
nameValuePairs.add(new BasicNameValuePair("pushdata", arg0[0].toString()));
|
|
|
613 |
HttpClient httpclient = new DefaultHttpClient();
|
|
|
614 |
HttpPost httppost = new HttpPost(apiData.getString("pollnotification.url.received", "http://45.79.106.95:3001/addPollNotification"));
|
|
|
615 |
httppost.setHeader("Authorization", ProfitTillConstants.BASIC_AUTH);
|
|
|
616 |
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
|
|
|
617 |
HttpResponse response = httpclient.execute(httppost);
|
|
|
618 |
HttpEntity entity = response.getEntity();
|
|
|
619 |
}
|
|
|
620 |
catch (Exception e) {
|
|
|
621 |
e.printStackTrace();
|
|
|
622 |
}
|
|
|
623 |
return "success";
|
|
|
624 |
}
|
|
|
625 |
|
|
|
626 |
@Override
|
|
|
627 |
protected void onPostExecute(String result) {
|
|
|
628 |
super.onPostExecute(result);
|
|
|
629 |
}
|
|
|
630 |
}
|
|
|
631 |
|
|
|
632 |
|
|
|
633 |
public Bitmap getBitmapfromUrl(String imageUrl) {
|
|
|
634 |
try {
|
|
|
635 |
URL url = new URL(imageUrl);
|
|
|
636 |
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
|
|
637 |
connection.setDoInput(true);
|
|
|
638 |
connection.connect();
|
|
|
639 |
InputStream input = connection.getInputStream();
|
|
|
640 |
Bitmap bitmap = BitmapFactory.decodeStream(input);
|
|
|
641 |
return bitmap;
|
|
|
642 |
}
|
|
|
643 |
catch (Exception e) {
|
|
|
644 |
// TODO Auto-generated catch block
|
|
|
645 |
e.printStackTrace();
|
|
|
646 |
return null;
|
|
|
647 |
|
|
|
648 |
}
|
|
|
649 |
}
|
|
|
650 |
}
|