Skip to content

Commit

Permalink
add method that can be used to determine if an app was launched due t…
Browse files Browse the repository at this point in the history
…o tapping on a notification
  • Loading branch information
MaikuB committed Sep 26, 2018
1 parent 6545ad0 commit 0d63ee3
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

Expand All @@ -60,14 +61,16 @@ public class FlutterLocalNotificationsPlugin implements MethodCallHandler, Plugi
private static final String CANCEL_ALL_METHOD = "cancelAll";
private static final String SCHEDULE_METHOD = "schedule";
private static final String PERIODICALLY_SHOW_METHOD = "periodicallyShow";
private static final String SHOW_DAILY_AT_TIME = "showDailyAtTime";
private static final String SHOW_WEEKLY_AT_DAY_AND_TIME = "showWeeklyAtDayAndTime";
private static final String SHOW_DAILY_AT_TIME_METHOD = "showDailyAtTime";
private static final String SHOW_WEEKLY_AT_DAY_AND_TIME_METHOD = "showWeeklyAtDayAndTime";
private static final String GET_NOTIFICATION_APP_LAUNCH_DETAILS_METHOD = "getNotificationAppLaunchDetails";
private static final String METHOD_CHANNEL = "dexterous.com/flutter/local_notifications";
private static final String PAYLOAD = "payload";
private static final String INVALID_ICON_ERROR_CODE = "INVALID_ICON";
private static final String INVALID_LARGE_ICON_ERROR_CODE = "INVALID_LARGE_ICON";
private static final String INVALID_BIG_PICTURE_ERROR_CODE = "INVALID_BIG_PICTURE";
private static final String INVALID_SOUND_ERROR_CODE = "INVALID_SOUND";
private static final String NOTIFICATION_LAUNCHED_APP = "notificationLaunchedApp";
private static final String INVALID_DRAWABLE_RESOURCE_ERROR_MESSAGE = "The resource %s could not be found. Please make sure it has been added as a drawable resource to your Android head project.";
private static final String INVALID_RAW_RESOURCE_ERROR_MESSAGE = "The resource %s could not be found. Please make sure it has been added as a raw resource to your Android head project.";

Expand Down Expand Up @@ -468,6 +471,7 @@ private static boolean isValidDrawableResource(Context context, String name, Res
@Override
public void onMethodCall(MethodCall call, Result result) {
switch (call.method) {

case INITIALIZE_METHOD: {
Map<String, Object> arguments = call.arguments();
String defaultIcon = (String) arguments.get(DEFAULT_ICON);
Expand All @@ -482,6 +486,18 @@ public void onMethodCall(MethodCall call, Result result) {
result.success(true);
break;
}
case GET_NOTIFICATION_APP_LAUNCH_DETAILS_METHOD: {
Map<String, Object> notificationAppLaunchDetails = new HashMap<>();
String payload = null;
Boolean notificationLaunchedApp = (registrar.activity() != null && SELECT_NOTIFICATION.equals(registrar.activity().getIntent().getAction()));
notificationAppLaunchDetails.put(NOTIFICATION_LAUNCHED_APP, notificationLaunchedApp);
if(notificationLaunchedApp) {
payload = registrar.activity().getIntent().getStringExtra(PAYLOAD);
}
notificationAppLaunchDetails.put(PAYLOAD, payload);
result.success(notificationAppLaunchDetails);
break;
}
case SHOW_METHOD: {
Map<String, Object> arguments = call.arguments();
NotificationDetails notificationDetails = extractNotificationDetails(result, arguments);
Expand All @@ -501,8 +517,8 @@ public void onMethodCall(MethodCall call, Result result) {
break;
}
case PERIODICALLY_SHOW_METHOD:
case SHOW_DAILY_AT_TIME:
case SHOW_WEEKLY_AT_DAY_AND_TIME: {
case SHOW_DAILY_AT_TIME_METHOD:
case SHOW_WEEKLY_AT_DAY_AND_TIME_METHOD: {
Map<String, Object> arguments = call.arguments();
NotificationDetails notificationDetails = extractNotificationDetails(result, arguments);
if (notificationDetails != null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>BuildSystemType</key>
<string>Original</string>
</dict>
</plist>
22 changes: 15 additions & 7 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,30 @@ import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:http/http.dart' as http;
import 'package:path_provider/path_provider.dart';

FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin;

/// IMPORTANT: running the following code on its own won't work as there is setup required for each platform head project.
/// Please download the complete example app from the GitHub repository where all the setup has been done
void main() {
void main() async {
flutterLocalNotificationsPlugin = new FlutterLocalNotificationsPlugin();

// NOTE: if you want to find out if the app was launched via notification then you could use the following call and then do something like
// change the default route of the app
// var notificationAppLaunchDetails =
// await flutterLocalNotificationsPlugin.getNotificationAppLaunchDetails();
runApp(
new MaterialApp(home: new MyApp()),
new MaterialApp(
home: HomePage(),
),
);
}

class MyApp extends StatefulWidget {
class HomePage extends StatefulWidget {
@override
_MyAppState createState() => new _MyAppState();
_HomePageState createState() => new _HomePageState();
}

class _MyAppState extends State<MyApp> {
FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin;
class _HomePageState extends State<HomePage> {
@override
initState() {
super.initState();
Expand All @@ -30,7 +39,6 @@ class _MyAppState extends State<MyApp> {
var initializationSettingsIOS = new IOSInitializationSettings();
var initializationSettings = new InitializationSettings(
initializationSettingsAndroid, initializationSettingsIOS);
flutterLocalNotificationsPlugin = new FlutterLocalNotificationsPlugin();
flutterLocalNotificationsPlugin.initialize(initializationSettings,
selectNotification: onSelectNotification);
}
Expand Down
17 changes: 16 additions & 1 deletion ios/Classes/FlutterLocalNotificationsPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ @implementation FlutterLocalNotificationsPlugin
NSString *const SHOW_WEEKLY_AT_DAY_AND_TIME_METHOD = @"showWeeklyAtDayAndTime";
NSString *const CANCEL_METHOD = @"cancel";
NSString *const CANCEL_ALL_METHOD = @"cancelAll";
NSString *const GET_NOTIFICATION_APP_LAUNCH_DETAILS_METHOD = @"getNotificationAppLaunchDetails";
NSString *const CHANNEL = @"dexterous.com/flutter/local_notifications";
NSString *const DAY = @"day";

Expand All @@ -41,11 +42,14 @@ @implementation FlutterLocalNotificationsPlugin

NSString *const NOTIFICATION_ID = @"NotificationId";
NSString *const PAYLOAD = @"payload";
NSString *const NOTIFICATION_LAUNCHED_APP = @"notificationLaunchedApp";
NSString *launchPayload;
bool displayAlert;
bool playSound;
bool updateBadge;
bool initialized;
bool launchingAppFromNotification;

+ (bool) resumingFromBackground { return appResumingFromBackground; }
UILocalNotification *launchNotification;

Expand Down Expand Up @@ -221,14 +225,22 @@ - (void)cancelAllNotifications:(FlutterResult _Nonnull) result {
- (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
if([INITIALIZE_METHOD isEqualToString:call.method]) {
[self initialize:call result:result];

} else if ([SHOW_METHOD isEqualToString:call.method] || [SCHEDULE_METHOD isEqualToString:call.method] || [PERIODICALLY_SHOW_METHOD isEqualToString:call.method] || [SHOW_DAILY_AT_TIME_METHOD isEqualToString:call.method]
|| [SHOW_WEEKLY_AT_DAY_AND_TIME_METHOD isEqualToString:call.method]) {
[self showNotification:call result:result];
} else if([CANCEL_METHOD isEqualToString:call.method]) {
[self cancelNotification:call result:result];
} else if([CANCEL_ALL_METHOD isEqualToString:call.method]) {
[self cancelAllNotifications:result];
} else if([GET_NOTIFICATION_APP_LAUNCH_DETAILS_METHOD isEqualToString:call.method]) {
NSString *payload;
if(launchNotification != nil) {
payload = launchNotification.userInfo[PAYLOAD];
} else {
payload = launchPayload;
}
NSDictionary *notificationAppLaunchDetails = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:launchingAppFromNotification], NOTIFICATION_LAUNCHED_APP, payload, PAYLOAD, nil];
result(notificationAppLaunchDetails);
}
else {
result(FlutterMethodNotImplemented);
Expand Down Expand Up @@ -407,6 +419,7 @@ - (void)userNotificationCenter:(UNUserNotificationCenter *)center
[FlutterLocalNotificationsPlugin handleSelectNotification:payload];
} else {
launchPayload = payload;
launchingAppFromNotification = true;
}

}
Expand All @@ -415,7 +428,9 @@ - (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
if (launchOptions != nil) {
launchNotification = (UILocalNotification *)[launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
launchingAppFromNotification = launchNotification != nil;
}

return YES;
}

Expand Down
1 change: 1 addition & 0 deletions lib/flutter_local_notifications.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ part 'src/platform_specifics/ios/notification_details.dart';
part 'src/notification_details.dart';
part 'src/initialization_settings.dart';
part 'src/flutter_local_notifications.dart';
part 'src/notification_app_launch_details.dart';
6 changes: 6 additions & 0 deletions lib/src/flutter_local_notifications.dart
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,12 @@ class FlutterLocalNotificationsPlugin {
return result;
}

Future<NotificationAppLaunchDetails> getNotificationAppLaunchDetails() async {
var result = await _channel.invokeMethod('getNotificationAppLaunchDetails');
return NotificationAppLaunchDetails(result['notificationLaunchedApp'],
result.containsKey('payload') ? result['payload'] : null);
}

/// Show a notification with an optional payload that will be passed back to the app when a notification is tapped
Future show(int id, String title, String body,
NotificationDetails notificationDetails,
Expand Down
9 changes: 9 additions & 0 deletions lib/src/notification_app_launch_details.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
part of flutter_local_notifications;

class NotificationAppLaunchDetails {
final bool didNotificationLaunchApp;
final String payload;

const NotificationAppLaunchDetails(
this.didNotificationLaunchApp, this.payload);
}

0 comments on commit 0d63ee3

Please sign in to comment.