我将我的应用设置为即使使用workmanager和flutter_local_notification关闭并且通过模拟器运行也可以接收通知,但是当我构建APK时我没有收到任何通知 我试图更改图标,但没有任何反应
我认为权限问题或向androidmanifest.xml添加内容 这是代码的一部分
import 'dart:convert';
import 'package:flutter/material.dart';
import 'choosingpage.dart';
import 'package:workmanager/workmanager.dart';
import 'package:flutter/material.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:http/http.dart' as http;
import 'package:hive/hive.dart';
import 'package:path_provider/path_provider.dart';
const simplePeriodicTask = "simplePeriodicTask";
// flutter local notification setup
void showNotification( v, flp) async {
var android = AndroidNotificationDetails(
'channel id', 'channel NAME', 'CHANNEL DESCRIPTION',
priority: Priority.High, importance: Importance.Max);
var iOS = IOSNotificationDetails();
var platform = NotificationDetails(android, iOS);
await flp.show(0, 'Message from Manager', '$v', platform,
payload: '$v');
}
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
await Workmanager.initialize(callbackDispatcher, isInDebugMode: true); //to true if still in testing lev turn it to false whenever you are launching the app
await Workmanager.registerPeriodicTask("5", simplePeriodicTask,
existingWorkPolicy: ExistingWorkPolicy.replace,
frequency: Duration(minutes: 15),//when should it check the link
initialDelay: Duration(seconds: 5),//duration before showing the notification
constraints: Constraints(
networkType: NetworkType.connected,
));
runApp(new MaterialApp(
// routes: {
// // When navigating to the "/" route, build the FirstScreen widget.
// '/': (context) => firstpage(),
// // When navigating to the "/second" route, build the SecondScreen widget.
// 'allresult': (context) => allresult(),
// 'userpage': (context) => userpage(hi),
// },
// initialRoute: 'allresult',
theme:
ThemeData(primaryColor: Colors.red, accentColor: Colors.yellowAccent),
debugShowCheckedModeBanner: false,
home: choosingpage(),
));
}
void callbackDispatcher() {
Workmanager.executeTask((task, inputData) async {
var box ;
String username;
String userpass;
String company;
final dir = await getApplicationDocumentsDirectory();
Hive.init(dir.path);
box = await Hive.openBox('app_data');
username = box.get('usernamelogin');
userpass = box.get('userpasslogin');
company = box.get('company');
print(username);
print(userpass);
print(company);
FlutterLocalNotificationsPlugin flp = FlutterLocalNotificationsPlugin();
var android = AndroidInitializationSettings('@mipmap/ic_launcher');
var iOS = IOSInitializationSettings();
var initSetttings = InitializationSettings(android, iOS);
flp.initialize(initSetttings);
var map = Map<String, dynamic>();
map['user'] = username;
map['pas'] = userpass;
map['com'] = company;
var url ="web site" ;
final response = await http.post(
url,
body: map);
var userdata = json.decode(response.body);
print(userdata["notf"]);
String mass=userdata["notf"];
String datenotf=userdata["datenotf"];
print(userdata);
// if(mass!=""){
// showNotification("$mass", flp);
// } else {
// print("no messgae");
// }
if(mass!=""){
showNotification("$mass", flp);
}
if(datenotf!=""){
showNotification("$datenotf", flp);
}
return Future.value(true);
});
}
答案 0 :(得分:0)
如果这仅是apk发行版的问题,则可能与ProGuard / R8的缩小有关。您可以通过向flutter build命令传递“ --no-shrink”标志来禁用缩小作为测试。
即: flutter build apk --noshrink
发布构建配置
在创建应用的发布版本(这是构建APK或应用捆绑包时的默认设置)之前,您可能需要按照此link自定义ProGuard配置文件并添加以下行: / p>
-保持类com.dexterous。** {*; }
这样做之后,还需要添加特定于插件所使用的GSON依赖关系的规则。这些规则可以在here中找到。示例应用程序具有合并的Proguard规则(proguard-rules.pro)文件,将这些规则组合在一起以供参考here。
⚠️请确保已配置应保留的资源,以使R8编译器不会按照通知here丢弃诸如通知图标之类的资源。如果不这样做,您可能看不到在应用程序的通知中指定的图标。可以在here中找到示例应用程序使用的配置,其中指定应保留所有可绘制资源以及用于播放自定义通知声音的文件(声音文件位于here)