Created
January 15, 2019 15:05
-
-
Save irshu355/2fcb4fa6b0954d4b626397a3e62f1832 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class CustomFcmMessageListenerService extends FirebaseMessagingService { | |
@Override | |
public void onMessageReceived(final RemoteMessage remoteMessage) { | |
super.onMessageReceived(remoteMessage); | |
Log.i("UID",SPUtils.getSharedStringData(this,BundleKey.Uid)); | |
//if user already logout, don't trigger push notification | |
if(SPUtils.getSharedStringData(this,BundleKey.Uid).equals("")){ | |
return; | |
} | |
/** | |
* track the push recevied event | |
*/ | |
try { | |
if (remoteMessage.getData().size() > 0) { | |
final Bundle extras = new Bundle(); | |
List<String> keys = new ArrayList<>(); | |
for (Map.Entry<String, String> entry : remoteMessage.getData().entrySet()) { | |
extras.putString(entry.getKey(), entry.getValue()); | |
keys.add(entry.getKey()); | |
} | |
NotificationInfo info = CleverTapAPI.getNotificationInfo(extras); | |
EventHelper.trackNotificationReceived(info, extras); | |
if (info.fromCleverTap) { | |
// Log.v(getClass().getCanonicalName(),"app status:"+ status); | |
if (extras.containsKey("sound")) { | |
extras.putString("wzrk_sound", extras.getString("sound")); | |
} | |
extras.putString("LaunchType",LaunchTypeNotif); | |
extras.putString("Type", "Campaign"); | |
extras.putString("Text", extras.getString("nt")); | |
//sendNotificationClevertap(remoteMessage,extras); | |
CleverTapAPI.createNotification(getApplicationContext(), extras); | |
} else { | |
sendNotificationCustom(remoteMessage, extras); | |
} | |
// not from CleverTap handle yourself or pass to another provider | |
} | |
} catch (Throwable ignored) { | |
} | |
} | |
private PendingIntent getLiveScreenIntent(int notifId,String action,String screenType, String cid){ | |
Intent intent = new Intent(this,PlayerActivity.class); | |
MediaInfo mediaInfo = new MediaInfo("" + cid, ScreenType.getScreenTypeByType(screenType)); | |
intent.putExtra(TamagoConstant.MEDIA_INFO, mediaInfo); | |
Bundle bundle = new Bundle(); | |
bundle.putString("action", action); | |
intent.putExtra("LaunchType",LaunchTypeNotif); | |
intent.putExtra("Type", "Followers"); | |
intent.putExtra("Room Id", cid); | |
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED); | |
intent.putExtras(bundle); | |
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this); | |
// Adds the back stack for the Intent | |
stackBuilder.addNextIntentWithParentStack(new Intent(this, MainActivity.class)); | |
stackBuilder.addNextIntent(intent); | |
PendingIntent pendingIntent = stackBuilder.getPendingIntent(notifId, PendingIntent.FLAG_ONE_SHOT); | |
return pendingIntent; | |
} | |
private void sendNotificationCustom(RemoteMessage remoteMessage, Bundle extras) { | |
Intent intent; | |
PendingIntent pendingIntent; | |
String message = extras.getString("message"); | |
String title = extras.getString("title"); | |
String action = extras.getString("action"); | |
String thumbImg = extras.getString("mediaUrl"); | |
// message = "mesage hre"; | |
// title = "title here"; | |
// action = AppConstant.BROADCASTER_LIVE; | |
// thumbImg = "https://miscmedia-9gag-fun.9cache.com/images/thumbnail-facebook/1481536354.227_YsUzaZ_100x100.jpg"; | |
int notificationId = 0; | |
if (!TextUtils.isEmpty(action) && action.equals(TamagoConstant.BROADCASTER_LIVE)) { | |
String screenType = extras.getString("screenType"); | |
String cid = extras.getString("cid"); | |
// screenType = "2"; | |
// cid = "186195"; | |
try { | |
notificationId = Integer.parseInt(cid); | |
} catch (Exception ex) { | |
Crashlytics.logException(ex); | |
} | |
if(isInForeground(getApplicationContext())) { //in foreground | |
if (TextUtils.isEmpty(screenType)) screenType = "2"; | |
// Create an Intent for the activity you want to | |
pendingIntent = getLiveScreenIntent(notificationId,action,screenType,cid); | |
}else{ | |
if (TextUtils.isEmpty(screenType)) screenType = "2"; | |
// Create an Intent for the activity you want to start | |
pendingIntent = getLiveScreenIntent(notificationId, action,screenType,cid); | |
} | |
} else { | |
intent = new Intent(this, MainActivity.class); | |
intent.putExtra("LaunchType",LaunchTypeNotif); | |
intent.putExtra("Type", "Campaign"); | |
intent.putExtra("Text", message); | |
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED); | |
pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT); | |
} | |
if(thumbImg!=null) { | |
JSONObject obj; | |
try { | |
obj = new JSONObject(thumbImg); | |
thumbImg = obj.getString("normal"); | |
} catch (JSONException e) { | |
Log.e("My App", "Could not parse malformed JSON: \"" + thumbImg + "\""); | |
} | |
} | |
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this,"live_updates") | |
.setSmallIcon(R.drawable.logo_default) | |
.setLargeIcon(!TextUtils.isEmpty(thumbImg)? getBitmapFromURL(thumbImg) : BitmapFactory.decodeResource(getResources(), R.drawable.logo_default)) | |
// .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.logo_default)) | |
.setContentTitle(title) | |
.setContentText(message) | |
.setAutoCancel(true) | |
.setWhen(System.currentTimeMillis()+(6 * 1000)) | |
.setContentIntent(pendingIntent); | |
NotificationManager notificationManager = | |
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); | |
notificationManager.notify(notificationId, notificationBuilder.build()); | |
} | |
private boolean isInForeground(Context context) { | |
int numActivities = 0; | |
if(numActivities==0){ | |
ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); | |
List<ActivityManager.RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses(); | |
if (appProcesses == null) { | |
Log.v(getClass().getCanonicalName(),"app processes are emoty"); | |
return false; | |
} | |
final String packageName = context.getPackageName(); | |
for (ActivityManager.RunningAppProcessInfo appProcess : appProcesses) { | |
if (appProcess.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND && appProcess.processName.equals(packageName)) { | |
return true; | |
} | |
} | |
} | |
return false; | |
} | |
public Bitmap getBitmapFromURL(String src) { | |
try { | |
URL url = new URL(src); | |
HttpURLConnection connection = (HttpURLConnection) url.openConnection(); | |
connection.setDoInput(true); | |
connection.connect(); | |
InputStream input = connection.getInputStream(); | |
Bitmap bitmap = BitmapFactory.decodeStream(input); | |
return BitmapUtils.getRoundedBitmap(bitmap,10); | |
} catch (IOException e) { | |
// Log exception | |
return null; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment