Skip to content

Commit d2afe42

Browse files
committed
feature:complete the upload prompt function
1 parent f7e2181 commit d2afe42

File tree

8 files changed

+167
-2
lines changed

8 files changed

+167
-2
lines changed
Loading

ios/Podfile.lock

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ PODS:
44
- MTBBarcodeScanner
55
- SwiftProtobuf
66
- Flutter (1.0.0)
7+
- flutter_local_notifications (0.0.1):
8+
- Flutter
79
- flutter_plugin_android_lifecycle (0.0.1):
810
- Flutter
911
- FMDB (2.7.5):
@@ -42,6 +44,7 @@ PODS:
4244
DEPENDENCIES:
4345
- barcode_scan (from `.symlinks/plugins/barcode_scan/ios`)
4446
- Flutter (from `Flutter`)
47+
- flutter_local_notifications (from `.symlinks/plugins/flutter_local_notifications/ios`)
4548
- flutter_plugin_android_lifecycle (from `.symlinks/plugins/flutter_plugin_android_lifecycle/ios`)
4649
- image_picker (from `.symlinks/plugins/image_picker/ios`)
4750
- package_info (from `.symlinks/plugins/package_info/ios`)
@@ -68,6 +71,8 @@ EXTERNAL SOURCES:
6871
:path: ".symlinks/plugins/barcode_scan/ios"
6972
Flutter:
7073
:path: Flutter
74+
flutter_local_notifications:
75+
:path: ".symlinks/plugins/flutter_local_notifications/ios"
7176
flutter_plugin_android_lifecycle:
7277
:path: ".symlinks/plugins/flutter_plugin_android_lifecycle/ios"
7378
image_picker:
@@ -100,6 +105,7 @@ EXTERNAL SOURCES:
100105
SPEC CHECKSUMS:
101106
barcode_scan: a5c27959edfafaa0c771905bad0b29d6d39e4479
102107
Flutter: 0e3d915762c693b495b44d77113d4970485de6ec
108+
flutter_local_notifications: 9e4738ce2471c5af910d961a6b7eadcf57c50186
103109
flutter_plugin_android_lifecycle: dc0b544e129eebb77a6bfb1239d4d1c673a60a35
104110
FMDB: 2ce00b547f966261cd18927a3ddb07cb6f3db82a
105111
image_picker: 66aa71bc96850a90590a35d4c4a2907b0d823109

ios/Runner/AppDelegate.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ import Flutter
88
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
99
) -> Bool {
1010
GeneratedPluginRegistrant.register(with: self)
11+
if #available(iOS 10.0, *) {
12+
UNUserNotificationCenter.current().delegate = self as? UNUserNotificationCenterDelegate
13+
}
1114
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
1215
}
1316
}

lib/main.dart

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,19 @@ import 'package:flutter_picgo/resources/theme_colors.dart';
55
import 'package:flutter_picgo/routers/application.dart';
66
import 'package:flutter_picgo/routers/routers.dart';
77
import 'package:flutter_picgo/utils/db_provider.dart';
8+
import 'package:flutter_picgo/utils/local_notification.dart';
89
import 'package:provider/provider.dart';
910

10-
void main() async {
11+
Future<void> main() async {
12+
/// needed if you intend to initialize in the `main` function
1113
WidgetsFlutterBinding.ensureInitialized();
1214
final provider = DbProvider();
1315
await provider.init();
16+
17+
/// notification initialization
18+
LocalNotificationUtil.getInstance().initialization();
19+
20+
/// run App
1421
runApp(App());
1522
}
1623

lib/model/received_notification.dart

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import 'package:flutter/material.dart';
2+
3+
class ReceivedNotification {
4+
final int id;
5+
final String title;
6+
final String body;
7+
final String payload;
8+
9+
ReceivedNotification({
10+
@required this.id,
11+
@required this.title,
12+
@required this.body,
13+
@required this.payload,
14+
});
15+
}

lib/utils/local_notification.dart

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
import 'dart:io';
2+
3+
import 'package:flutter/material.dart';
4+
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
5+
6+
class LocalNotificationUtil {
7+
static LocalNotificationUtil _instance;
8+
9+
FlutterLocalNotificationsPlugin _flutterLocalNotificationsPlugin;
10+
NotificationAppLaunchDetails _notificationAppLaunchDetails;
11+
12+
FlutterLocalNotificationsPlugin get flutterLocalNotificationsPlugin =>
13+
_flutterLocalNotificationsPlugin;
14+
15+
NotificationAppLaunchDetails get notificationAppLaunchDetails =>
16+
_notificationAppLaunchDetails;
17+
18+
LocalNotificationUtil._();
19+
20+
/// 必须先调用
21+
initialization() async {
22+
_flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();
23+
// notification
24+
_notificationAppLaunchDetails = await _flutterLocalNotificationsPlugin
25+
.getNotificationAppLaunchDetails();
26+
27+
var initializationSettingsAndroid =
28+
AndroidInitializationSettings('ic_launcher');
29+
// Note: permissions aren't requested here just to demonstrate that can be done later using the `requestPermissions()` method
30+
// of the `IOSFlutterLocalNotificationsPlugin` class
31+
var initializationSettingsIOS = IOSInitializationSettings(
32+
requestAlertPermission: true,
33+
requestBadgePermission: true,
34+
requestSoundPermission: true,
35+
onDidReceiveLocalNotification: null);
36+
var initializationSettings = InitializationSettings(
37+
initializationSettingsAndroid, initializationSettingsIOS);
38+
await _flutterLocalNotificationsPlugin.initialize(initializationSettings,
39+
onSelectNotification: (String payload) async {
40+
if (payload != null) {
41+
debugPrint('notification payload: ' + payload);
42+
}
43+
});
44+
}
45+
46+
/// 需要先请求权限
47+
requestPermissions() {
48+
/// iOS获取权限
49+
if (Platform.isIOS) {
50+
_flutterLocalNotificationsPlugin
51+
.resolvePlatformSpecificImplementation<
52+
IOSFlutterLocalNotificationsPlugin>()
53+
?.requestPermissions(
54+
alert: true,
55+
badge: true,
56+
sound: true,
57+
);
58+
}
59+
if (Platform.isAndroid) {
60+
_flutterLocalNotificationsPlugin
61+
.resolvePlatformSpecificImplementation<
62+
AndroidFlutterLocalNotificationsPlugin>()
63+
?.createNotificationChannel(AndroidNotificationChannel(
64+
AndroidChannelId.upload_channel, '通知提示', '上传通知提示'));
65+
}
66+
}
67+
68+
/// 显示
69+
show(int id, String title, String body,
70+
NotificationDetails notificationDetails) async {
71+
await _flutterLocalNotificationsPlugin.show(
72+
id, title, body, notificationDetails);
73+
}
74+
75+
/// 实例获取
76+
static LocalNotificationUtil getInstance() {
77+
if (_instance == null) {
78+
_instance = LocalNotificationUtil._();
79+
}
80+
return _instance;
81+
}
82+
83+
/// 上传Channel
84+
static AndroidNotificationDetails uploadAndroidChannel() {
85+
return AndroidNotificationDetails(
86+
AndroidChannelId.upload_channel, '上传通知', '上传通知提示',
87+
importance: Importance.Max, priority: Priority.High, ticker: 'ticker');
88+
}
89+
90+
/// 默认IOS
91+
static IOSNotificationDetails normalIOSNotificationDetails() {
92+
return IOSNotificationDetails();
93+
}
94+
95+
static NotificationDetails createNotificationDetails(
96+
AndroidNotificationDetails android, IOSNotificationDetails iOS) {
97+
return NotificationDetails(android, iOS);
98+
}
99+
}
100+
101+
class AndroidChannelId {
102+
static const upload_channel = '10000';
103+
}

lib/views/picgo_setting_page/picgo_setting_page.dart

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import 'package:flutter/material.dart';
33
import 'package:flutter/cupertino.dart';
44
import 'package:flutter_picgo/routers/application.dart';
55
import 'package:flutter_picgo/routers/routers.dart';
6+
import 'package:flutter_picgo/utils/local_notification.dart';
67
import 'package:flutter_picgo/utils/shared_preferences.dart';
78
import 'package:toast/toast.dart';
89

@@ -75,7 +76,12 @@ class _PicGoSettingPageState extends State<PicGoSettingPage> {
7576
title: Text('开启上传提示'),
7677
trailing: CupertinoSwitch(
7778
value: this.isUploadedTip,
78-
onChanged: (value) {
79+
onChanged: (value) async {
80+
if (value) {
81+
/// Local Notification 请求权限
82+
await LocalNotificationUtil.getInstance()
83+
.requestPermissions();
84+
}
7985
this._save(
8086
SharedPreferencesKeys.settingIsUploadedTip, value);
8187
setState(() {

lib/views/upload_page/upload_page.dart

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import 'dart:io';
22
import 'package:flutter/material.dart';
33
import 'package:flutter_picgo/components/loading.dart';
4+
import 'package:flutter_picgo/utils/local_notification.dart';
45
import 'package:flutter_picgo/utils/permission.dart';
56
import 'package:flutter_picgo/utils/shared_preferences.dart';
67
import 'package:flutter_picgo/utils/strings.dart';
@@ -25,6 +26,9 @@ class _UploadPageState extends State<UploadPage>
2526
TextEditingController _controller;
2627
int _selectButton = 1;
2728

29+
// 通知提示
30+
bool needNotify = false;
31+
2832
// 按钮id
2933
static const MARKDOWN = 1;
3034
static const HTML = 2;
@@ -35,6 +39,10 @@ class _UploadPageState extends State<UploadPage>
3539

3640
_UploadPageState() {
3741
_presenter = UploadPagePresenter(this);
42+
SpUtil.getInstance().then((sp) {
43+
this.needNotify =
44+
sp.getBool(SharedPreferencesKeys.settingIsUploadedTip) ?? false;
45+
});
3846
}
3947

4048
@override
@@ -273,9 +281,13 @@ class _UploadPageState extends State<UploadPage>
273281

274282
@override
275283
uploadFaild(String errorMsg) {
284+
if (needNotify) {
285+
_showNotification(0, '上传失败:$errorMsg');
286+
}
276287
Toast.show(errorMsg ?? '', context);
277288
}
278289

290+
/// 设置剪切板
279291
setClipData([bool needShowTip = true]) {
280292
if (_clipUrl == null || _clipUrl == '') {
281293
Toast.show('暂无可获取图片', context);
@@ -313,8 +325,21 @@ class _UploadPageState extends State<UploadPage>
313325

314326
@override
315327
uploadSuccess(String imageUrl) async {
328+
if (needNotify) {
329+
await _showNotification(1, '上传成功:图片链接已复制到剪切板,原链接:$imageUrl');
330+
}
316331
this._clipUrl = imageUrl;
317332
setClipData(false);
318333
Toast.show('上传成功:已复制到剪切板,图片链接为:$imageUrl', context);
319334
}
335+
336+
Future<void> _showNotification(int id, String body) async {
337+
LocalNotificationUtil.getInstance().show(
338+
id,
339+
'上传提示',
340+
body,
341+
LocalNotificationUtil.createNotificationDetails(
342+
LocalNotificationUtil.uploadAndroidChannel(),
343+
LocalNotificationUtil.normalIOSNotificationDetails()));
344+
}
320345
}

0 commit comments

Comments
 (0)