ios - 当应用程序处于后台时,不会通过 FCM 调用 iOS 上的后台消息处理程序
问题描述
谁能告诉我为什么我不能在 iOS 中收到 onBackgroundMessage 的触发器,但我可以通过 FCM 收到通知,因此如果我点击通知 onMessageOpenedApp 就会被触发。我将如何触发 onBackgroundMessage?与 Android 相比,一切正常。
这是我的代码
Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
if (Firebase.apps.isEmpty) await Firebase.initializeApp();
print('Handling a background message ${message.data['id']}');
}
handleNotifications() async {
final FirebaseMessaging _firebaseMessaging = FirebaseMessaging.instance;
NotificationSettings settings = await _firebaseMessaging.requestPermission(
alert: true,
announcement: false,
badge: true,
carPlay: false,
criticalAlert: false,
provisional: false,
sound: true,
);
print('User granted permission: ${settings.authorizationStatus}');
_firebaseMessaging.getToken().then((String token) async {
assert(token != null);
print(token);
final sessionPrefs = await SharedPreferences.getInstance();
if (Platform.isAndroid) {
sessionPrefs.setString('token', token);
sessionPrefs.setString('platform', 'android');
} else if (Platform.isIOS) {
sessionPrefs.setString('token', token);
sessionPrefs.setString('platform', 'ios');
}
});
FirebaseMessaging.instance.setForegroundNotificationPresentationOptions(badge: false, alert: false, sound: true); //presentation options for Apple notifications when received in the foreground.
FirebaseMessaging.onMessage.listen((message) async {
print('Got a message whilst in the FOREGROUND!');
return;
}).onData((data) async {
print('Got a DATA message whilst in the FOREGROUND!');
print('data from stream: ${data.data['id']}');
});
FirebaseMessaging.onMessageOpenedApp.listen((message) async {
print('NOTIFICATION MESSAGE TAPPED');
return;
}).onData((data) async {
print('NOTIFICATION MESSAGE TAPPED');
print('data from streamOpen: ${data.data}');
});
FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);
FirebaseMessaging.instance.getInitialMessage().then((value) => value != null ? _firebaseMessagingBackgroundHandler : false);
return;
}
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
handleNotifications();
runApp(
MaterialApp(
debugShowCheckedModeBanner: false,
initialRoute: '/',
home: session //Menu if the user has not loged out the app else Home
? Menu()
: HomePage(), // CONDITION FOR CHECKING IF THE SESSION IS TRUE OR FALSE
),
);
}
解决方案
推荐阅读
- database - Redshift 查询数组
返回编码值 - matlab - 如何在 MATLAB 中围绕中心元素创建矩阵?
- file - 在 Java 8 中优化替换某些文件的字节
- java - 如何为未知数量的数字显示方表
- raspberry-pi - 如何使 chromecast 成为 dlna 接收器/客户端(间接)
- python - GitHub 上类似 Trello 的程序的执行命令
- c# - 如何在非完美 60Hz (59.92Hz) 显示器上修复 60 fps 循环?
- android - Android Exoplayer 播放 UDP 流无音频
- c++ - 投影矩阵导致剪辑截止问题
- javascript - 如何使用谷歌脚本向 Trello 上的自定义字段添加值?