我目前正在使用FCM进行推送通知。当我的应用程序打开时,我会收到通知,但是当应用程序关闭或处于后台时,我不会收到任何信息,直到我重新打开应用程序。在XCode上,我启用了后台获取和远程通知。接下来我该查什么?谢谢。
我正在使用firebase_messaging: ^5.1.6
带着密码
_firebaseMessaging.configure(
onMessage: (Map<String, dynamic> message) async {
print('message is $message');
setState(
() {
showOverlayNotification((context) {
return GestureDetector(
onTap: () {},
child: Platform.isIOS
? MessageNotification(
title: message['notification']['title'],
body: message['notification']['body'],
)
: MessageNotification(
title: message['notification']['title'],
body: message['notification']['body'],
),
);
// }
}, duration: Duration(milliseconds: 4000));
},
);
},
onLaunch: (Map<String, dynamic> message) async {
print('launching');
},
onResume: (Map<String, dynamic> message) async {
print('resuming');
print("onResume: $message");
},
);
_firebaseMessaging.requestNotificationPermissions(
const IosNotificationSettings(sound: true, badge: true, alert: true));
_firebaseMessaging.onIosSettingsRegistered
.listen((IosNotificationSettings settings) {
print("Settings registered: $settings");
});
_firebaseMessaging.getToken().then((String token) {
assert(token != null);
setState(() {
_firebaseMessaging.subscribeToTopic('all');
print('subscribed');
_homeScreenText = "Push Messaging token: $token";
_saveDeviceToken(token);
});
print(_homeScreenText);
}); ```
My flutter doctor response is:
```[✓] Flutter (Channel stable, v1.9.1+hotfix.2, on Mac OS X 10.14.6 18G103, locale en-GB)• Flutter version 1.9.1+hotfix.2 at /Users/student/flutter• Framework revision 2d2a1ffec9 (3 weeks ago), 2019-09-06 18:39:49 -0700• Engine revision b863200c37• Dart version 2.5.0✓安卓工具链-为安卓设备开发(Android版本29.0.0)
• Android SDK at /Users/student/Library/Android/sdk• Android NDK location not configured (optional; useful for native profiling support)• Platform android-29, build-tools 29.0.0• Java binary at: /Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/bin/java• Java version OpenJDK Runtime Environment (build 1.8.0_152-release-1343-b01)• All Android licenses accepted.iOS和macOS的✓Xcode (Xcode 11.0)
• Xcode at /Applications/Xcode.app/Contents/Developer• Xcode 11.0, Build version 11A420a• CocoaPods version 1.7.4✓Android (版本3.4)
• Android Studio at /Applications/Android Studio.app/Contents• Flutter plugin version 38.2.1• Dart plugin version 183.6270• Java version OpenJDK Runtime Environment (build 1.8.0_152-release-1343-b01)✓与代码(1.38.1版)
• VS Code at /Applications/Visual Studio Code.app/Contents• Flutter extension version 3.4.1✓连接设备(1个可用)
• iPhone • e1100c84b1fc7871a6790337ef23c0fd7af397d5 • ios • iOS 12.4.1发布于 2020-01-13 12:26:54
我成功地解决了这个问题,删除了对的任何引用。我随后删除了:
if (@available(iOS 10.0, *)) {
[UNUserNotificationCenter currentNotificationCenter].delegate = (id<UNUserNotificationCenterDelegate>) self;
}来自ios/runner/AppDelegate.m或ios/runner/AppDelegate.Swiver文件。
通知随后开始正常工作。
发布于 2020-11-29 12:49:29
移动侧
我努力了一下,终于想出了这个解决办法。
将get_it: ^4.0.4添加到pubspec.yaml中
创建具有以下内容的文件Locator.dart:
import 'package:flutter/widgets.dart';
//Open Screen Without Context Service
class NavigationService {
final GlobalKey<NavigatorState> navigatorKey =
new GlobalKey<NavigatorState>();
navigateTo(String routeName , String name) {
return navigatorKey.currentState.pushNamed(routeName , arguments: name);
}
goBack() {
return navigatorKey.currentState.pop();
}
}创建具有以下内容的文件Locator.dart:
import 'package:get_it/get_it.dart';
import 'package:MyProject/Services/NavigationService.dart';
//Open Screen Without Context
GetIt locator = GetIt.instance;
void setupLocator() {
locator.registerLazySingleton(() => NavigationService());
}最后,您的main.dart必须是这样的:
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get_it/get_it.dart';
import 'package:MyProject/Services/NavigationService.dart';
import 'package:MyProject/Utils/locator.dart';
void main() {
WidgetsFlutterBinding.ensureInitialized();
SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp, DeviceOrientation.portraitDown]);
runApp(MyApp());
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
final FirebaseMessaging _firebaseMessaging = FirebaseMessaging();
@override
void initState() {
GetIt.instance.registerSingleton<NavigationService>(NavigationService());
getMessage();
super.initState();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'App name',
navigatorKey: locator<NavigationService>().navigatorKey,
debugShowCheckedModeBanner: false,
theme: ThemeData(
primaryColorDark: Color(0xff440ABC),
primaryColor: Color(0xff703FF7),
primaryColorLight: Color(0xff7e51fa),
accentColor: Color(0xffc09b01),
hintColor: Color(0xff616161),
backgroundColor: Color(0xffFEF9F9),
bottomAppBarColor: Color(0xffFEF9F9),
fontFamily: 'Sans'),
///MyRequestsScreen
onGenerateRoute: (routeSettings) {
switch (routeSettings.name) {
case 'DestinationScreen':
return MaterialPageRoute(
builder: (context) => DestinationScreen());
default:
return null;
}
},
home: MenuScreen(),
);
}
void getMessage() {
_firebaseMessaging.configure(onMessage: (Map<String, dynamic> message) async {
locator<NavigationService>().navigateTo('DestinationScreen', "go");
}, onResume: (Map<String, dynamic> message) async {
locator<NavigationService>().navigateTo('DestinationScreen', "data");
}, onLaunch: (Map<String, dynamic> message) async {
locator<NavigationService>().navigateTo('DestinationScreen', "data");
});
}
}服务器端
加:
"data": {
"click_action": "FLUTTER_NOTIFICATION_CLICK"
}在你的json中,如下:
{
"to": "YOUR_PUSH_ID",
"notification": {
"body": "YOUR_MESSAGE",
"OrganizationId": "2",
"content_available": true,
"priority": "high",
"subtitle": "Elementary School",
"title": "YOUR_TITLE"
},
"data": {
"click_action": "FLUTTER_NOTIFICATION_CLICK"
}
}发布于 2019-09-30 11:00:53
如果您想通过firebase函数进行背景通知,那么您可以查看下面的代码并实现输出.颤振自动通知功能
https://stackoverflow.com/questions/58164702
复制相似问题