Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to include action buttons on android #38

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
import android.support.v4.app.NotificationManagerCompat;
import android.text.Html;
import android.text.Spanned;
import android.util.Log;

import com.dexterous.flutterlocalnotifications.models.ActionButton;
import com.dexterous.flutterlocalnotifications.models.NotificationDetails;
import com.dexterous.flutterlocalnotifications.models.Time;
import com.dexterous.flutterlocalnotifications.models.styles.BigTextStyleInformation;
Expand All @@ -36,7 +38,11 @@
import java.util.Calendar;
import java.util.Iterator;
import java.util.Map;
import java.util.HashMap;

import io.flutter.plugin.common.EventChannel;
import io.flutter.plugin.common.EventChannel.EventSink;
import io.flutter.plugin.common.EventChannel.StreamHandler;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
Expand All @@ -48,7 +54,8 @@
* FlutterLocalNotificationsPlugin
*/
public class FlutterLocalNotificationsPlugin implements MethodCallHandler, PluginRegistry.NewIntentListener {
private static final String SELECT_NOTIFICATION = "SELECT_NOTIFICATION";
private static final String ACTION_SELECT_NOTIFICATION = "SELECT_NOTIFICATION";
private static final String ACTION_FLUTTER_BUTTON = "FLUTTER_BUTTON";
private static final String SCHEDULED_NOTIFICATIONS = "scheduled_notifications";
private static final String INITIALIZE_METHOD = "initialize";
private static final String SHOW_METHOD = "show";
Expand All @@ -59,10 +66,10 @@ public class FlutterLocalNotificationsPlugin implements MethodCallHandler, Plugi
private static final String SHOW_DAILY_AT_TIME = "showDailyAtTime";
private static final String SHOW_WEEKLY_AT_DAY_AND_TIME = "showWeeklyAtDayAndTime";
private static final String METHOD_CHANNEL = "dexterous.com/flutter/local_notifications";
private static final String PAYLOAD = "payload";
public static String NOTIFICATION_ID = "notification_id";
public static String NOTIFICATION = "notification";
public static String REPEAT = "repeat";
public static final String PAYLOAD = "payload";
public static final String NOTIFICATION_ID = "notification_id";
public static final String NOTIFICATION = "notification";
public static final String REPEAT = "repeat";
private static MethodChannel channel;
private static int defaultIconResourceId;
private final Registrar registrar;
Expand Down Expand Up @@ -125,6 +132,8 @@ public static void registerWith(Registrar registrar) {
channel = new MethodChannel(registrar.messenger(), METHOD_CHANNEL);
FlutterLocalNotificationsPlugin plugin = new FlutterLocalNotificationsPlugin(registrar);
channel.setMethodCallHandler(plugin);
Log.i("LocalNotifications", "Initialized the local notifications plugin");

}

public static void removeNotificationFromCache(Integer notificationId, Context context) {
Expand Down Expand Up @@ -231,7 +240,7 @@ private static Notification createNotification(Context context, NotificationDeta
}
setupNotificationChannel(context, notificationDetails);
Intent intent = new Intent(context, getMainActivityClass(context));
intent.setAction(SELECT_NOTIFICATION);
intent.setAction(ACTION_SELECT_NOTIFICATION);
intent.putExtra(PAYLOAD, notificationDetails.payload);
PendingIntent pendingIntent = PendingIntent.getActivity(context, notificationDetails.id, intent, PendingIntent.FLAG_UPDATE_CURRENT);
DefaultStyleInformation defaultStyleInformation = (DefaultStyleInformation) notificationDetails.styleInformation;
Expand All @@ -245,6 +254,24 @@ private static Notification createNotification(Context context, NotificationDeta
.setOngoing(BooleanUtils.getValue(notificationDetails.ongoing));


for (ActionButton button: notificationDetails.buttons) {
System.out.println(button.text);
Intent buttonIntent = new Intent(context, getMainActivityClass(context));
buttonIntent.setAction(ACTION_FLUTTER_BUTTON);
buttonIntent.putExtra(NOTIFICATION_ID, notificationDetails.id);
buttonIntent.putExtra(PAYLOAD, button.payload);
PendingIntent buttonPendingIntent =
PendingIntent.getActivity(context, 0, buttonIntent, 0);

if (button.iconResourceId == null) {
if (button.icon != null) {
button.iconResourceId = context.getResources().getIdentifier(notificationDetails.icon, "drawable", context.getPackageName());
} else {
button.iconResourceId = defaultIconResourceId;
}
}
builder.addAction(button.iconResourceId, button.text, buttonPendingIntent);
}
applyGrouping(notificationDetails, builder);
setSound(context, notificationDetails, builder);
setVibrationPattern(notificationDetails, builder);
Expand Down Expand Up @@ -482,11 +509,20 @@ public boolean onNewIntent(Intent intent) {
}

private Boolean sendNotificationPayloadMessage(Intent intent) {
if (SELECT_NOTIFICATION.equals(intent.getAction())) {
if (ACTION_SELECT_NOTIFICATION.equals(intent.getAction())) {
String payload = intent.getStringExtra(PAYLOAD);
channel.invokeMethod("selectNotification", payload);
return true;
}
if (ACTION_FLUTTER_BUTTON.equals(intent.getAction())) {
Map<String, Object> data = new HashMap<String, Object>();
data.put(PAYLOAD, intent.getStringExtra(PAYLOAD));
data.put(NOTIFICATION_ID, intent.getIntExtra(NOTIFICATION_ID, 0));
//String payload = intent.getStringExtra(PAYLOAD);
//Integer notificationId = intent.getIntExtra(NOTIFICATION_ID);
channel.invokeMethod("buttonPressed", data);
return true;
}
return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.dexterous.flutterlocalnotifications.models;

public class ActionButton {
public String icon;
public Integer iconResourceId;
public String text;
public String payload;
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public class NotificationDetails {
private static final String HTML_FORMAT_TITLE = "htmlFormatTitle";
private static final String HTML_FORMAT_CONTENT = "htmlFormatContent";
private static final String DAY = "day";
private static final String ACTIONS = "actions";

public Integer id;
public String title;
Expand Down Expand Up @@ -77,6 +78,7 @@ public class NotificationDetails {
public Boolean autoCancel;
public Boolean ongoing;
public Integer day;
public ArrayList<ActionButton> buttons;

// Note: this is set on the Android to save details about the icon that should be used when re-hydrating scheduled notifications when a device has been restarted
public Integer iconResourceId;
Expand Down Expand Up @@ -120,11 +122,43 @@ public static NotificationDetails from(Map<String, Object> arguments) {
notificationDetails.groupKey = (String) platformChannelSpecifics.get(GROUP_KEY);
notificationDetails.setAsGroupSummary = (Boolean) platformChannelSpecifics.get(SET_AS_GROUP_SUMMARY);
notificationDetails.groupAlertBehavior = (Integer) platformChannelSpecifics.get(GROUP_ALERT_BEHAVIOR);
if (platformChannelSpecifics.containsKey(ACTIONS)) {
@SuppressWarnings("unchecked")
ArrayList<Object> buttons = (ArrayList<Object>)platformChannelSpecifics.get(ACTIONS);
if (buttons != null) {
notificationDetails.buttons = getActionButtons(buttons);
} else {
notificationDetails.buttons = new ArrayList<ActionButton>();
}
} else {
notificationDetails.buttons = new ArrayList<ActionButton>();
}
getChannelInformation(notificationDetails, platformChannelSpecifics);
}
return notificationDetails;
}

private static ArrayList<ActionButton> getActionButtons(ArrayList<Object> buttonData) {
ArrayList<ActionButton> buttons = new ArrayList<ActionButton>();
for (Object ob: buttonData) {
@SuppressWarnings("unchecked")
Map<String, Object> data = (Map<String, Object>)ob;
ActionButton newButton = new ActionButton();

if (data.containsKey(ICON)) {
newButton.icon = (String) data.get(ICON);
}
if (data.containsKey(TITLE)) {
newButton.text = (String) data.get(TITLE);
}
if (data.containsKey(PAYLOAD)) {
newButton.payload = (String) data.get(PAYLOAD);
}
buttons.add(newButton);
}
return buttons;
}

private static void getChannelInformation(NotificationDetails notificationDetails, Map<String, Object> platformChannelSpecifics) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
notificationDetails.channelId = (String) platformChannelSpecifics.get(CHANNEL_ID);
Expand Down
Loading