Skip to content

Commit

Permalink
Merge pull request #42 from flarelane/dev-1.6.1
Browse files Browse the repository at this point in the history
1.6.1
  • Loading branch information
minhyeok4dev authored Jun 6, 2024
2 parents 4e44323 + cebfc28 commit f40108c
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 27 deletions.
31 changes: 25 additions & 6 deletions FlareLane/src/main/java/com/flarelane/ChannelManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,45 @@
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;

import com.flarelane.util.AndroidUtils;

class ChannelManager {
private static final String DEFAULT_CHANNEL_ID = "com.flarelane.default_notification_channel_id";
private static final String DEFAULT_CHANNEL_NAME = "flarelane_default_channel_name";

protected static String getDefaultChannelId() {
return DEFAULT_CHANNEL_ID;
protected static String getDefaultChannelId(Context context) {
String customChannelId = getCustomChannelId(context);
return customChannelId != null ? customChannelId : DEFAULT_CHANNEL_ID;
}

protected static String getChannelName(Context context) {
String customChannelName = Helper.getResourceString(context.getApplicationContext(), DEFAULT_CHANNEL_NAME);
protected static String getCustomChannelId(Context context) {
try {
ApplicationInfo appInfo = context.getPackageManager().getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA);
return appInfo.metaData.getString(Constants.DEFAULT_CHANNEL_ID);
} catch (PackageManager.NameNotFoundException e) {
return null;
}
}

protected static String getDefaultChannelName(Context context) {
String customChannelName = Helper.getResourceString(context.getApplicationContext(), Constants.DEFAULT_CHANNEL_NAME);
return customChannelName != null ? customChannelName : context.getString(R.string.default_notification_channel_name);
}

protected static void createNotificationChannel(Context context) {
// Create the NotificationChannel, but only on API 26+ because
// the NotificationChannel class is new and not in the support library
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(getDefaultChannelId(), getChannelName(context), NotificationManager.IMPORTANCE_HIGH);
// If custom channel id exists, not create a notification channel by FlareLane.
if (getCustomChannelId(context) != null) {
return;
}

NotificationChannel channel = new NotificationChannel(getDefaultChannelId(context), getDefaultChannelName(context), NotificationManager.IMPORTANCE_HIGH);
channel.setShowBadge(true);
// Register the channel with the system; you can't change the importance
// or other notification behaviors after this
Expand Down
7 changes: 6 additions & 1 deletion FlareLane/src/main/java/com/flarelane/Constants.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@ package com.flarelane

object Constants {
internal const val ID_IC_STAT_DEFAULT = "ic_stat_flarelane_default"
internal const val ID_NOTIFICATION_ACCENT_COLOR = "flarelane_notification_accent_color"
internal const val DEFAULT_CHANNEL_ID = "flarelane_default_channel_id"
internal const val DEFAULT_CHANNEL_NAME = "flarelane_default_channel_name"

// Reserved key from notification's data
internal const val NOTIFICATION_ACCENT_COLOR = "flarelane_notification_accent_color"
internal const val DISMISS_LAUNCH_URL = "flarelane_dismiss_launch_url"
internal const val DISMISS_FOREGROUND_NOTIFICATION = "flarelane_dismiss_foreground_notification"
internal const val NOTIFICATION_CHANNEL_ID = "flarelane_notification_channel_id"
internal const val NOTIFICATION_ID = "flarelane_notification_id"
}
2 changes: 1 addition & 1 deletion FlareLane/src/main/java/com/flarelane/FlareLane.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
public class FlareLane {
public static class SdkInfo {
public static SdkType type = SdkType.NATIVE;
public static String version = "1.6.0";
public static String version = "1.6.1";
}

protected static com.flarelane.NotificationForegroundReceivedHandler notificationForegroundReceivedHandler = null;
Expand Down
23 changes: 23 additions & 0 deletions FlareLane/src/main/java/com/flarelane/Notification.kt
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package com.flarelane

import android.content.Context
import android.os.Bundle
import android.os.Parcelable
import com.flarelane.util.AndroidUtils
import kotlinx.parcelize.IgnoredOnParcel
import kotlinx.parcelize.Parcelize
import org.json.JSONObject
import java.util.Date

@Parcelize
data class Notification(
Expand Down Expand Up @@ -58,4 +61,24 @@ data class Notification(
it.putString("data", data)
}
}

fun currentAndroidNotificationId(): Int {
val notificationId = dataJsonObject?.optString(Constants.NOTIFICATION_ID)

return if (notificationId != null && !notificationId.contentEquals("")) {
notificationId.toInt()
} else {
Date().time.toInt()
}
}

fun currentChannelId(context: Context): String {
val channelId = dataJsonObject?.optString(Constants.NOTIFICATION_CHANNEL_ID)

return if (channelId != null && !channelId.contentEquals("")) {
channelId
} else {
ChannelManager.getDefaultChannelId(context)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,25 +43,32 @@ internal class NotificationClickedActivity : Activity() {
return
}

IntentUtil.createIntentIfResolveActivity(this, notification.url)?.let {
try {
it.flags = Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_NEW_TASK
startActivity(it)
} catch (_: Exception) {
Logger.verbose("Url is not available. url=${notification.url}")
try {
val url = Uri.parse(notification.url)
if (url.scheme == null) {
Logger.verbose("Url scheme is null. url=${notification.url}")
launchApp()
return
}
} ?: FlareLaneWebViewActivity.show(this, notification.url)

IntentUtil.createIntentIfResolveActivity(this, url)?.let {
try {
it.flags = Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_NEW_TASK
startActivity(it)
} catch (_: Exception) {
Logger.verbose("Url is not available. url=${notification.url}")
launchApp()
}
} ?: FlareLaneWebViewActivity.show(this, notification.url)
} catch (_: Exception) {
}
}
}

private fun launchApp() {
if (isTaskRoot) {
Logger.verbose("This is last activity in the stack")
packageManager.getLaunchIntentForPackage(packageName)?.let {
it.flags = Intent.FLAG_ACTIVITY_NEW_TASK or
Intent.FLAG_ACTIVITY_CLEAR_TOP or
Intent.FLAG_ACTIVITY_SINGLE_TOP
startActivity(it)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

import com.flarelane.util.ExtensionsKt;

import org.json.JSONObject;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
Expand Down Expand Up @@ -65,11 +67,11 @@ public void run() {
in = connection.getInputStream();
image = BitmapFactory.decodeStream(in);
} catch (Exception e) {
com.flarelane.BaseErrorHandler.handle(e);
BaseErrorHandler.handle(e);
}
}

NotificationCompat.Builder builder = new NotificationCompat.Builder(context, ChannelManager.getDefaultChannelId())
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, flarelaneNotification.currentChannelId(context))
.setSmallIcon(getNotificationIcon(context))
.setContentText(flarelaneNotification.body)
.setContentTitle(flarelaneNotification.title == null ? context.getApplicationInfo().loadLabel(context.getPackageManager()).toString() : flarelaneNotification.title)
Expand All @@ -79,12 +81,12 @@ public void run() {
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC);

try {
String accentColor = Helper.getResourceString(context.getApplicationContext(), Constants.ID_NOTIFICATION_ACCENT_COLOR);
String accentColor = Helper.getResourceString(context.getApplicationContext(), Constants.NOTIFICATION_ACCENT_COLOR);
if (accentColor != null) {
builder = builder.setColor(Color.parseColor(accentColor));
}
} catch (Exception e) {
com.flarelane.BaseErrorHandler.handle(e);
BaseErrorHandler.handle(e);
}

if (image != null) {
Expand All @@ -102,16 +104,15 @@ public void run() {
notification.defaults |= android.app.Notification.DEFAULT_VIBRATE;

NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify((int) new Date().getTime(), notification);

notificationManager.notify(flarelaneNotification.currentAndroidNotificationId(), notification);

if (isForeground) {
EventService.createForegroundReceived(projectId, deviceId, flarelaneNotification);
} else {
EventService.createBackgroundReceived(projectId, deviceId, flarelaneNotification);
}
} catch (Exception e) {
com.flarelane.BaseErrorHandler.handle(e);
BaseErrorHandler.handle(e);
}
}
}).start();
Expand Down
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ android {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
signingConfig signingConfigs.debug
}
}
compileOptions {
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<string name="app_name">FlareLane-Android-SDK</string>
<string name="notification_channel_id">default</string>

<!-- Strings for FlareLane-->
<!-- Strings for FlareLane -->
<string name="flarelane_notification_accent_color">#f242f5</string>
<string name="flarelane_default_channel_name">My Custom Channel</string>
<string name="flarelane_default_channel_name">My Channel Name</string>
</resources>

0 comments on commit f40108c

Please sign in to comment.