Flutter Firebase Cloud Messaging-后台运行应用程序时发出通知 [英] Flutter Firebase Cloud Messaging - Notification when app in background
问题描述
我当前正在使用FCM进行推送通知.当我的应用程序打开时,我会收到通知,但是当应用程序关闭或在后台时,我不会收到任何通知,直到我重新打开该应用程序为止.在XCode上,我启用了后台获取并启用了远程通知.接下来我应该检查什么?谢谢.
I am currently using FCM for push notifications. When my app is open, I receive the notification, however when the app is closed or in the background - I do not receive anything until I reopen the app. On XCode, I have background fetch enabled and remote notifications enabled. What should I check next? Thank you.
我正在使用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 toolchain - develop for Android devices (Android SDK version 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.
[✓] Xcode - develop for iOS and macOS (Xcode 11.0)
• Xcode at /Applications/Xcode.app/Contents/Developer
• Xcode 11.0, Build version 11A420a
• CocoaPods version 1.7.4
[✓] Android Studio (version 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)
[✓] VS Code (version 1.38.1)
• VS Code at /Applications/Visual Studio Code.app/Contents
• Flutter extension version 3.4.1
[✓] Connected device (1 available)
• iPhone • e1100c84b1fc7871a6790337ef23c0fd7af397d5 • ios • iOS 12.4.1
推荐答案
我设法通过删除对Flutter Local Notification插件的任何引用来解决此问题.我随后将其删除:
I managed to solve this by removing any references to Flutter Local Notification plugin. I subsequently removed:
if (@available(iOS 10.0, *)) {
[UNUserNotificationCenter currentNotificationCenter].delegate = (id<UNUserNotificationCenterDelegate>) self;
}
从ios/runner/AppDelegate.m或ios/runner/AppDelegate.swift文件中.
from the ios/runner/AppDelegate.m or ios/runner/AppDelegate.swift file.
通知随后开始正常运行.
Notifications then began working as normal.
这篇关于Flutter Firebase Cloud Messaging-后台运行应用程序时发出通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!