Skip to content

Commit

Permalink
feat: add option to reopen app from notification
Browse files Browse the repository at this point in the history
  • Loading branch information
kevlatus committed Mar 6, 2021
1 parent 4b5cdff commit 8d34083
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ data class NotificationOptions(
val subtitle: String? = null,
val description: String? = null,
val color: Int? = null,
val onTapBringToFront: Boolean = false,
)

class BackgroundNotification(
Expand All @@ -39,19 +40,26 @@ class BackgroundNotification(
.setPriority(NotificationCompat.PRIORITY_HIGH)

init {
updateNotification(
options.title,
options.iconName,
options.subtitle,
options.description,
options.color,
false)
updateNotification(options, false)
}

private fun getDrawableId(iconName: String): Int {
return context.resources.getIdentifier(iconName, "drawable", context.packageName)
}

private fun buildBringToFrontIntent(): PendingIntent? {
val intent: Intent? = context.packageManager
.getLaunchIntentForPackage(context.packageName)
?.setPackage(null)
?.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED)

return if (intent != null) {
PendingIntent.getActivity(context, 0, intent, 0)
} else {
null
}
}

private fun updateChannel(channelName: String) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val notificationManager = NotificationManagerCompat.from(context)
Expand All @@ -67,27 +75,29 @@ class BackgroundNotification(
}

private fun updateNotification(
title: String,
iconName: String,
subtitle: String?,
description: String?,
color: Int?,
options: NotificationOptions,
notify: Boolean
) {
val iconId = getDrawableId(iconName).let {
val iconId = getDrawableId(options.iconName).let {
if (it != 0) it else getDrawableId(kDefaultNotificationIconName)
}
builder = builder
.setContentTitle(title)
.setContentTitle(options.title)
.setSmallIcon(iconId)
.setContentText(options.subtitle)
.setSubText(options.description)

builder = if (color != null) {
builder.setColor(color).setColorized(true)
builder = if (options.color != null) {
builder.setColor(options.color).setColorized(true)
} else {
builder.setColor(0).setColorized(false)
}
builder = builder.setContentText(subtitle)
builder = builder.setSubText(description)

builder = if (options.onTapBringToFront) {
builder.setContentIntent(buildBringToFrontIntent())
} else {
builder.setContentIntent(null)
}

if (notify) {
val notificationManager = NotificationManagerCompat.from(context)
Expand All @@ -100,13 +110,7 @@ class BackgroundNotification(
updateChannel(options.channelName)
}

updateNotification(
options.title,
options.iconName,
options.subtitle,
options.description,
options.color,
isVisible)
updateNotification(options, isVisible)

this.options = options
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,11 @@ private void onChangeNotificationOptions(MethodCall call, Result result) {

String subtitle = call.argument("subtitle");
String description = call.argument("description");
Boolean onTapBringToFront = call.argument("onTapBringToFront");
if (onTapBringToFront == null) {
onTapBringToFront = false;
}

String hexColor = call.argument("color");
Integer color = null;
if (hexColor != null) {
Expand All @@ -220,7 +225,8 @@ private void onChangeNotificationOptions(MethodCall call, Result result) {
iconName,
subtitle,
description,
color);
color,
onTapBringToFront);
Map<String, Object> notificationMeta = this.locationService.changeNotificationOptions(options);
result.success(notificationMeta);
} catch (Exception e) {
Expand Down
5 changes: 5 additions & 0 deletions packages/location/lib/location.dart
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ class Location {
/// the sub text will be set to [description]. The notification [color] can
/// also be customized.
///
/// When [onTapBringToFront] is set to true, tapping the notification will
/// bring the activity back to the front.
///
/// Both [title] and [channelName] will be set to defaults, if no values are
/// provided. All other null arguments will be ignored.
///
Expand All @@ -114,6 +117,7 @@ class Location {
String? subtitle,
String? description,
Color? color,
bool? onTapBringToFront,
}) {
return LocationPlatform.instance.changeNotificationOptions(
channelName: channelName,
Expand All @@ -122,6 +126,7 @@ class Location {
subtitle: subtitle,
description: description,
color: color,
onTapBringToFront: onTapBringToFront,
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,9 @@ class LocationPlatform extends PlatformInterface {
/// the sub text will be set to [description]. The notification [color] can
/// also be customized.
///
/// When [onTapBringToFront] is set to true, tapping the notification will
/// bring the activity back to the front.
///
/// Both [title] and [channelName] will be set to defaults, if no values are
/// provided. All other null arguments will be ignored.
///
Expand All @@ -129,6 +132,7 @@ class LocationPlatform extends PlatformInterface {
String? subtitle,
String? description,
Color? color,
bool? onTapBringToFront,
}) {
throw UnimplementedError();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,9 @@ class MethodChannelLocation extends LocationPlatform {
/// the sub text will be set to [description]. The notification [color] can
/// also be customized.
///
/// When [onTapBringToFront] is set to true, tapping the notification will
/// bring the activity back to the front.
///
/// Both [title] and [channelName] will be set to defaults, if no values are
/// provided. All other null arguments will be ignored.
///
Expand All @@ -178,6 +181,7 @@ class MethodChannelLocation extends LocationPlatform {
String? subtitle,
String? description,
Color? color,
bool? onTapBringToFront,
}) async {
if (!Platform.isAndroid) {
// This method only applies to Android.
Expand All @@ -203,6 +207,10 @@ class MethodChannelLocation extends LocationPlatform {
data['color'] = '#${color.value.toRadixString(16)}';
}

if (onTapBringToFront != null) {
data['onTapBringToFront'] = onTapBringToFront;
}

final Map<dynamic, dynamic>? result =
await _methodChannel!.invokeMethod('changeNotificationOptions', data);

Expand Down
1 change: 1 addition & 0 deletions packages/location_web/lib/location_web.dart
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ class LocationWebPlugin extends LocationPlatform {
String? subtitle,
String? description,
Color? color,
bool? onTapBringToFront,
}) async {
// This method only applies to Android.
// Do nothing to prevent user from handling a potential UnimplementedError.
Expand Down

0 comments on commit 8d34083

Please sign in to comment.