Skip to content

Commit

Permalink
Merge pull request #11 from MaikuB/dev
Browse files Browse the repository at this point in the history
Add inbox notification style for Android
  • Loading branch information
MaikuB authored Apr 17, 2018
2 parents 5f500db + 1949b5d commit 8412c4f
Show file tree
Hide file tree
Showing 15 changed files with 173 additions and 34 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,7 @@
* Bug fix in calculating when to show a scheduled Android notification. Ensure scheduled Android notifications will remain scheduled even after rebooting.

## [0.1.3]
* Fix broken example app for iOS due to incorrect reference to custom sound file. Added ability to handle when a notification is tapped. See updated example for details on how to do this and will navigate to another page. Note that the second page isn't rendering full-screen on Android if the notification was tapped on while the app was in the foreground. Suspect that this is Flutter rendering issue and have logged this on the Flutter repository at https://github.com/flutter/flutter/issues/16636
* Fix broken example app for iOS due to incorrect reference to custom sound file. Added ability to handle when a notification is tapped. See updated example for details on how to do this and will navigate to another page. Note that the second page isn't rendering full-screen on Android if the notification was tapped on while the app was in the foreground. Suspect that this is Flutter rendering issue and have logged this on the Flutter repository at https://github.com/flutter/flutter/issues/16636

## [0.1.4]
* Add inbox notification style
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
import android.text.Html;
import android.text.Spanned;

import com.dexterous.flutterlocalnotifications.models.styles.BigTextStyleInformation;
import com.dexterous.flutterlocalnotifications.models.styles.DefaultStyleInformation;
import com.dexterous.flutterlocalnotifications.models.styles.InboxStyleInformation;
import com.dexterous.flutterlocalnotifications.models.styles.StyleInformation;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
Expand Down Expand Up @@ -219,6 +223,24 @@ private static void ApplyNotificationStyle(NotificationDetails notificationDetai
}
builder.setStyle(bigTextStyle);
break;
case Inbox:
InboxStyleInformation inboxStyleInformation = (InboxStyleInformation) notificationDetails.styleInformation;
NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
if (inboxStyleInformation.contentTitle != null) {
CharSequence contentTitle = inboxStyleInformation.htmlFormatContentTitle ? fromHtml(inboxStyleInformation.contentTitle) : inboxStyleInformation.contentTitle;
inboxStyle.setBigContentTitle(contentTitle);
}
if (inboxStyleInformation.summaryText != null) {
CharSequence summaryText = inboxStyleInformation.htmlFormatSummaryText ? fromHtml(inboxStyleInformation.summaryText) : inboxStyleInformation.summaryText;
inboxStyle.setSummaryText(summaryText);
}
if (inboxStyleInformation.lines != null) {
for (String line : inboxStyleInformation.lines) {
inboxStyle.addLine(inboxStyleInformation.htmlFormatLines ? fromHtml(line) : line);
}
}
builder.setStyle(inboxStyle);
break;
}
}

Expand Down Expand Up @@ -330,7 +352,7 @@ public boolean onNewIntent(Intent intent) {
}

private Boolean sendNotificationPayloadMessage(Intent intent) {
if(SELECT_NOTIFICATION.equals(intent.getAction())) {
if (SELECT_NOTIFICATION.equals(intent.getAction())) {
String payload = intent.getStringExtra(PAYLOAD);
channel.invokeMethod("selectNotification", payload);
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

import android.os.Build;

import com.dexterous.flutterlocalnotifications.models.styles.BigTextStyleInformation;
import com.dexterous.flutterlocalnotifications.models.styles.DefaultStyleInformation;
import com.dexterous.flutterlocalnotifications.models.styles.InboxStyleInformation;
import com.dexterous.flutterlocalnotifications.models.styles.StyleInformation;

import java.util.ArrayList;
import java.util.Map;

public class NotificationDetails {
Expand Down Expand Up @@ -35,7 +41,7 @@ public static NotificationDetails from(Map<String, Object> arguments) {
}
@SuppressWarnings("unchecked")
Map<String, Object> platformChannelSpecifics = (Map<String, Object>) arguments.get("platformSpecifics");
notificationDetails.style = NotificationStyle.values()[(Integer)platformChannelSpecifics.get("style")];
notificationDetails.style = NotificationStyle.values()[(Integer) platformChannelSpecifics.get("style")];
ProcessStyleInformation(notificationDetails, platformChannelSpecifics);
notificationDetails.icon = (String) platformChannelSpecifics.get("icon");
notificationDetails.priority = (Integer) platformChannelSpecifics.get("priority");
Expand All @@ -57,25 +63,30 @@ private static void ProcessStyleInformation(NotificationDetails notificationDeta
@SuppressWarnings("unchecked")
Map<String, Object> styleInformation = (Map<String, Object>) platformSpecifics.get("styleInformation");
DefaultStyleInformation defaultStyleInformation = getDefaultStyleInformation(styleInformation);
switch(notificationDetails.style) {
case Default:
notificationDetails.styleInformation = defaultStyleInformation;
break;
case BigText:
String bigText = (String)styleInformation.get("bigText");
Boolean htmlFormatBigText = (Boolean)styleInformation.get("htmlFormatBigText");
String contentTitle = (String)styleInformation.get("contentTitle");
Boolean htmlFormatContentTitle = (Boolean)styleInformation.get("htmlFormatContentTitle");
String summaryText = (String)styleInformation.get("summaryText");
Boolean htmlFormatSummaryText = (Boolean)styleInformation.get("htmlFormatSummaryText");
notificationDetails.styleInformation = new BigTextStyleInformation(defaultStyleInformation.htmlFormatTitle, defaultStyleInformation.htmlFormatBody, bigText, htmlFormatBigText, contentTitle, htmlFormatContentTitle, summaryText, htmlFormatSummaryText);
break;
if (notificationDetails.style == NotificationStyle.Default) {
notificationDetails.styleInformation = defaultStyleInformation;
} else if (notificationDetails.style == NotificationStyle.BigText) {
String bigText = (String) styleInformation.get("bigText");
Boolean htmlFormatBigText = (Boolean) styleInformation.get("htmlFormatBigText");
String contentTitle = (String) styleInformation.get("contentTitle");
Boolean htmlFormatContentTitle = (Boolean) styleInformation.get("htmlFormatContentTitle");
String summaryText = (String) styleInformation.get("summaryText");
Boolean htmlFormatSummaryText = (Boolean) styleInformation.get("htmlFormatSummaryText");
notificationDetails.styleInformation = new BigTextStyleInformation(defaultStyleInformation.htmlFormatTitle, defaultStyleInformation.htmlFormatBody, bigText, htmlFormatBigText, contentTitle, htmlFormatContentTitle, summaryText, htmlFormatSummaryText);
} else if(notificationDetails.style == NotificationStyle.Inbox) {
String contentTitle = (String) styleInformation.get("contentTitle");
Boolean htmlFormatContentTitle = (Boolean) styleInformation.get("htmlFormatContentTitle");
String summaryText = (String) styleInformation.get("summaryText");
Boolean htmlFormatSummaryText = (Boolean) styleInformation.get("htmlFormatSummaryText");
ArrayList<String> lines = (ArrayList<String>) styleInformation.get("lines");
Boolean htmlFormatLines = (Boolean) styleInformation.get("htmlFormatLines");
notificationDetails.styleInformation = new InboxStyleInformation(defaultStyleInformation.htmlFormatTitle, defaultStyleInformation.htmlFormatBody, contentTitle, htmlFormatContentTitle, summaryText, htmlFormatSummaryText, lines, htmlFormatLines);
}
}

private static DefaultStyleInformation getDefaultStyleInformation(Map<String, Object> styleInformation) {
Boolean htmlFormatTitle = (Boolean)styleInformation.get("htmlFormatTitle");
Boolean htmlFormatBody = (Boolean)styleInformation.get("htmlFormatContent");
Boolean htmlFormatTitle = (Boolean) styleInformation.get("htmlFormatTitle");
Boolean htmlFormatBody = (Boolean) styleInformation.get("htmlFormatContent");
return new DefaultStyleInformation(htmlFormatTitle, htmlFormatBody);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@

public enum NotificationStyle{
Default,
BigText
BigText,
Inbox
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.dexterous.flutterlocalnotifications;
package com.dexterous.flutterlocalnotifications.models.styles;

public class BigTextStyleInformation extends DefaultStyleInformation {
public String bigText;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.dexterous.flutterlocalnotifications;
package com.dexterous.flutterlocalnotifications.models.styles;

public class DefaultStyleInformation extends StyleInformation {
public Boolean htmlFormatTitle;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.dexterous.flutterlocalnotifications.models.styles;

import java.util.ArrayList;

public class InboxStyleInformation extends DefaultStyleInformation {
public Boolean htmlFormatLines;
public ArrayList<String> lines;
public String contentTitle;
public Boolean htmlFormatContentTitle;
public String summaryText;
public Boolean htmlFormatSummaryText;

public InboxStyleInformation(Boolean htmlFormatTitle, Boolean htmlFormatBody, String contentTitle, Boolean htmlFormatContentTitle, String summaryText, Boolean htmlFormatSummaryText, ArrayList<String> lines, Boolean htmlFormatLines) {
super(htmlFormatTitle, htmlFormatBody);
this.contentTitle = contentTitle;
this.htmlFormatContentTitle = htmlFormatContentTitle;
this.summaryText = summaryText;
this.htmlFormatSummaryText = htmlFormatSummaryText;
this.lines = lines;
this.htmlFormatLines = htmlFormatLines;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.dexterous.flutterlocalnotifications.models.styles;

public abstract class StyleInformation {
}
32 changes: 32 additions & 0 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import 'package:flutter_local_notifications/platform_specifics/notification_deta
import 'package:flutter_local_notifications/platform_specifics/notification_details/notification_details_android.dart';
import 'package:flutter_local_notifications/platform_specifics/notification_details/notification_details_ios.dart';
import 'package:flutter_local_notifications/platform_specifics/android_styles/big_text_style_information.dart';
import 'package:flutter_local_notifications/platform_specifics/android_styles/inbox_style_information.dart';

void main() {
runApp(
Expand Down Expand Up @@ -86,6 +87,13 @@ class _MyAppState extends State<MyApp> {
onPressed: () async {
await showBigTextNotification();
})),
new Padding(
padding: new EdgeInsets.fromLTRB(0.0, 0.0, 0.0, 8.0),
child: new RaisedButton(
child: new Text('Show inbox notification [Android]'),
onPressed: () async {
await showInboxNotification();
})),
],
),
),
Expand Down Expand Up @@ -174,6 +182,30 @@ class _MyAppState extends State<MyApp> {
0, 'big text title', 'silent body', platformChannelSpecifics);
}

showInboxNotification() async {
List<String> lines = new List<String>();
lines.add('line <b>1</b>');
lines.add('line <i>2</i>');
InboxStyleInformation inboxStyleInformation = new InboxStyleInformation(
lines,
htmlFormatLines: true,
contentTitle: 'overridden <b>inbox/b> context title',
htmlFormatContentTitle: true,
summaryText: 'summary <i>text</i>',
htmlFormatSummaryText: true);
NotificationDetailsAndroid androidPlatformChannelSpecifics =
new NotificationDetailsAndroid('inbox channel id', 'inboxchannel name',
'inbox channel description',
style: NotificationStyleAndroid.Inbox,
styleInformation: inboxStyleInformation);
NotificationDetailsIOS iOSPlatformChannelSpecifics =
new NotificationDetailsIOS();
NotificationDetails platformChannelSpecifics = new NotificationDetails(
androidPlatformChannelSpecifics, iOSPlatformChannelSpecifics);
await FlutterLocalNotifications.show(
0, 'inbox title', 'inbox body', platformChannelSpecifics);
}

Future onSelectNotification(String payload) async {
if (payload != null) {
debugPrint('notification payload: ' + payload);
Expand Down
14 changes: 8 additions & 6 deletions lib/flutter_local_notifications.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ class FlutterLocalNotifications {
static MessageHandler onSelectNotification;

/// Initializes the plugin. Call this method on application before using the plugin further
static Future<bool> initialize(
InitializationSettings initializationSettings, {MessageHandler selectNotification}) async {
onSelectNotification = selectNotification;
Map<String, dynamic> serializedPlatformSpecifics;
static Future<bool> initialize(InitializationSettings initializationSettings,
{MessageHandler selectNotification}) async {
onSelectNotification = selectNotification;
Map<String, dynamic> serializedPlatformSpecifics;
if (Platform.isAndroid) {
serializedPlatformSpecifics = initializationSettings.android.toJson();
} else if (Platform.isIOS) {
Expand All @@ -31,7 +31,8 @@ class FlutterLocalNotifications {

/// Show a notification with an optional payload that will be passed back to the app when a notification is tapped
static Future show(int id, String title, String body,
NotificationDetails notificationDetails, {String payload}) async {
NotificationDetails notificationDetails,
{String payload}) async {
Map<String, dynamic> serializedPlatformSpecifics;
if (Platform.isAndroid) {
serializedPlatformSpecifics = notificationDetails.android.toJson();
Expand All @@ -54,7 +55,8 @@ class FlutterLocalNotifications {

/// Schedules a notification to be shown at the specified time with an optional payload that is passed through when a notification is tapped
static Future schedule(int id, String title, String body,
DateTime scheduledDate, NotificationDetails notificationDetails, {String payload}) async {
DateTime scheduledDate, NotificationDetails notificationDetails,
{String payload}) async {
Map<String, dynamic> serializedPlatformSpecifics;
if (Platform.isAndroid) {
serializedPlatformSpecifics = notificationDetails.android.toJson();
Expand Down
46 changes: 46 additions & 0 deletions lib/platform_specifics/android_styles/inbox_style_information.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import 'package:flutter_local_notifications/platform_specifics/android_styles/default_style_information.dart';

class InboxStyleInformation extends DefaultStyleInformation {
/// Overrides ContentTitle in the big form of the template.
final String contentTitle;

/// Set the first line of text after the detail section in the big form of the template.
final String summaryText;

/// The lines that form part of the digest section for inbox-style notifications
List<String> lines;

/// Specifies if the lines should have formatting applied through HTML markup
final bool htmlFormatLines;

/// Specifies if the overridden ContentTitle should have formatting applied through HTML markup
final bool htmlFormatContentTitle;

/// Specifies if formatting should be applied to the first line of text after the detail section in the big form of the template.
final bool htmlFormatSummaryText;

InboxStyleInformation(this.lines,
{this.htmlFormatLines = false,
this.contentTitle,
this.htmlFormatContentTitle = false,
this.summaryText,
this.htmlFormatSummaryText = false,
bool htmlFormatContent = false,
bool htmlFormatTitle = false})
: super(htmlFormatContent, htmlFormatTitle);

Map<String, dynamic> toJson() {
var styleJson = super.toJson();

var bigTextStyleJson = <String, dynamic>{
'contentTitle': contentTitle,
'htmlFormatContentTitle': htmlFormatContentTitle,
'summaryText': summaryText,
'htmlFormatSummaryText': htmlFormatSummaryText,
'lines': lines ?? new List<String>(),
'htmlFormatLines': htmlFormatLines
};
styleJson.addAll(bigTextStyleJson);
return styleJson;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ class NotificationDetails {

/// Notification details for iOS
final NotificationDetailsIOS iOS;

const NotificationDetails(this.android, this.iOS);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import 'package:flutter_local_notifications/platform_specifics/android_styles/de
import 'package:flutter_local_notifications/platform_specifics/android_styles/style_information.dart';

/// The available notification styles on Android
enum NotificationStyleAndroid { Default, BigText }
enum NotificationStyleAndroid { Default, BigText, Inbox }

/// Defines the available importance levels for Android notifications
class Importance {
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: flutter_local_notifications
description: A cross platform plugin for displaying and scheduling local notifications for Flutter applications with the ability to customise for each platform.
version: 0.1.3
version: 0.1.4
author: Michael Bui <[email protected]>
homepage: https://github.com/MaikuB/flutter_local_notifications

Expand Down

0 comments on commit 8412c4f

Please sign in to comment.