我目前使用firebase的cloud messaging做推播,訊息可以正常推送,但在APP為背景模式下卻不會進到onBackgroundMessage裏面,只有IOS無法,Android是會執行onBackgroundMessage的,查了很多資料還是無解,要請大家幫幫忙了
main.dart
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);
runApp(const App());
}
@pragma('vm:entry-point')
Future<void> _firebaseMessagingBackgroundHandler(
RemoteMessage message,
) async {
if (kDebugMode) {
debugPrint('Handling a background message: ${message.messageId}');
}
}
推播內容
{
"to" : "df9JUN6C1E8Omxez7TINHv:APA91bHZ2SlXU_uSJHQsBck9mc0zsUdgz_lz34DDpiZc0Ck-I2iBazWcNl2V8EZsEGQNt0NRXsP6VDXw79lsxr4MXLVLZOBCRgl6hSrLg88bhj4dMeIVzsoMTXEaE05j3wOyD-7omH2K",
"collapse_key" : "type_a",
"notification" : {
"body" : "Body of Your Notification",
"title": "Title of Your Notification",
"content_available" : true,
"priority" : "high"
},
"data" : {
"body" : "Body of Your Notification in Data",
"title": "Title of Your Notification in Title",
"key_1" : "Value for key_1",
"key_2" : "Value for key_2"
}
}
在APP前景模式下可以打印出content_available為true
info.plist
<key>UIBackgroundModes</key>
<array>
<string>fetch</string>
<string>processing</string>
<string>remote-notification</string>
</array>
這個部分應該是影響iOS的部分,Firebase傳送推播訊息到iOS系統前,需經過Apple的APN伺服器驗證,Android就不會有這個問題,只要把chanel實作出來就行了。