/** * Processing chat room push message this message will be broadcasts to all the activities * registered */ private void processChatRoomPush(String title, boolean isBackground, String data) { if (!isBackground) { try { JSONObject datObj = new JSONObject(data); JSONObject mObj = datObj.getJSONObject("message"); FeedNotifications message = new FeedNotifications(); String for_unique_id = mObj.getString("for_unique_id"); Log.e("for_unique_id : ", for_unique_id); message.setUnique_id(mObj.getString("unique_id")); message.setFor_unique_id(mObj.getString("for_unique_id")); message.setMessage(mObj.getString("message")); message.setId(mObj.getString("notification_id")); message.setCreatedAt(mObj.getString("created_at")); JSONObject uObj = datObj.getJSONObject("user"); // skip the message if the message belongs to same user as // the user would be having the same message when he was sending // but it might differs in your scenario if (uObj.getString("user_id") .equals(AppController.getInstance().getPrefManager().getUser().getUid())) { Log.e(TAG, "Skipping the push message as it belongs to same user"); return; } FeedUser user = new FeedUser(); user.setUid(uObj.getString("user_id")); user.setEmail(uObj.getString("email")); // user.setPhoto(uObj.getString("photo")); user.setName_user(uObj.getString("name")); message.setUser(user); // verifying whether the app is in background or foreground if (!NotificationUtils.isAppIsInBackground(getApplicationContext())) { // app is in foreground, broadcast the push message Intent pushNotification = new Intent(Config.PUSH_NOTIFICATION); pushNotification.putExtra("type", Config.PUSH_TYPE_CHATROOM); pushNotification.putExtra("message", message); pushNotification.putExtra("for_unique_id", for_unique_id); LocalBroadcastManager.getInstance(this).sendBroadcast(pushNotification); // play notification sound NotificationUtils notificationUtils = new NotificationUtils(); notificationUtils.playNotificationSound(); } else { // app is in background. show the message in notification try Intent resultIntent = new Intent(getApplicationContext(), Notifications.class); resultIntent.putExtra("for_unique_id", for_unique_id); showNotificationMessage( getApplicationContext(), title, user.getName_user() + " : " + message.getMessage(), message.getCreatedAt(), resultIntent); // play notification sound NotificationUtils notificationUtils = new NotificationUtils(); notificationUtils.playNotificationSound(); } } catch (JSONException e) { Log.e(TAG, "json parsing error: " + e.getMessage()); Toast.makeText( getApplicationContext(), "Json parse error: " + e.getMessage(), Toast.LENGTH_LONG) .show(); } } else { // the push notification is silent, may be other operations needed // like inserting it in to SQLite } }
/** * Processing user specific push message It will be displayed with / without image in push * notification tray */ private void processUserMessage(String title, boolean isBackground, String data) { if (!isBackground) { try { JSONObject datObj = new JSONObject(data); String imageUrl = datObj.getString("image"); JSONObject mObj = datObj.getJSONObject("message"); FeedNotifications message = new FeedNotifications(); message.setMessage(mObj.getString("message")); message.setId(mObj.getString("message_id")); message.setCreatedAt(mObj.getString("created_at")); JSONObject uObj = datObj.getJSONObject("user"); FeedUser user = new FeedUser(); user.setUid(uObj.getString("user_id")); user.setEmail(uObj.getString("email")); user.setName_user(uObj.getString("name")); message.setUser(user); // verifying whether the app is in background or foreground if (!NotificationUtils.isAppIsInBackground(getApplicationContext())) { // app is in foreground, broadcast the push message Intent pushNotification = new Intent(Config.PUSH_NOTIFICATION); pushNotification.putExtra("type", Config.PUSH_TYPE_USER); pushNotification.putExtra("message", message); LocalBroadcastManager.getInstance(this).sendBroadcast(pushNotification); // play notification sound NotificationUtils notificationUtils = new NotificationUtils(); notificationUtils.playNotificationSound(); } else { // app is in background. show the message in notification try Intent resultIntent = new Intent(getApplicationContext(), Notifications.class); // check for push notification image attachment if (TextUtils.isEmpty(imageUrl)) { showNotificationMessage( getApplicationContext(), title, user.getName_user() + " : " + message.getMessage(), message.getCreatedAt(), resultIntent); } else { // push notification contains image // show it with the image showNotificationMessageWithBigImage( getApplicationContext(), title, message.getMessage(), message.getCreatedAt(), resultIntent, imageUrl); } } } catch (JSONException e) { Log.e(TAG, "json parsing error: " + e.getMessage()); Toast.makeText( getApplicationContext(), "Json parse error: " + e.getMessage(), Toast.LENGTH_LONG) .show(); } } else { // the push notification is silent, may be other operations needed // like inserting it in to SQLite } }