我让这个云功能在iOS中正常工作,当创建新消息时,它会发送Firebase推送通知:
export const sendChatNotification = functions.firestore.document('chatrooms/{chatroomId}/chats/{chatId}').onCreate(async (snap, ctx) => {
const token = snap.get('sendToDeviceToken');
const sender = snap.get('fullSenderName');
const body = snap.get('message');
var tokens = [];
tokens.push(token);
const payload = {
notification: {
title: sender,
body: body,
},
android: {
notification: {
channelId: "roofdeck_default",
click_action: 'FLUTTER_NOTIFICATION_CLICK',
title: sender,
body: body,
}
},
apns: {
headers: {
"apns-push-type": "alert"
},
payload: {
aps: {
category: "FLUTTER_NOTIFICATION_CLICK"
}
},
},
data: {
postID: snap.id,
type: "POST_TAG",
},
tokens: tokens
}
if (tokens.length > 0) {
await fcm.sendMulticast(payload);
}
});
我以为问题出在安卓通知频道,但今天我能够通过以下教程生成它:安卓推送通知
我在AndroidManifes.xml中添加了以下行:
<meta-data
android:name="com.google.firebase.messaging.default_notification_channel_id"
android:value="roofdeck_default"/>
我还创建了本地通知服务(使用Flutter本地通知进行前景模式(。
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
class LocalNotificationService {
static final FlutterLocalNotificationsPlugin _notificationsPlugin =
FlutterLocalNotificationsPlugin();
static void initialize() {
final InitializationSettings initializationSettings =
InitializationSettings(
android: AndroidInitializationSettings("@mipmap/ic_launcher"),
);
_notificationsPlugin.initialize(initializationSettings);
print('Initialized Notifications');
}
static void display(RemoteMessage message) async {
try {
final id = DateTime.now().millisecondsSinceEpoch / 1000;
final NotificationDetails notificationDetails = NotificationDetails(
android: AndroidNotificationDetails(
"roofdeck_default",
"roofdeck_channel",
channelDescription: "RoofDeck Main Channel",
importance: Importance.max,
priority: Priority.high,
),
);
await _notificationsPlugin.show(
id.toInt(),
message.notification.title,
message.notification.body,
notificationDetails,
);
} catch (e) {
print('Channel Error: $e');
}
}
}
同时在Main:中初始化应用程序的Init上的本地通知
Future<void> backgroundHandler(RemoteMessage message) async {
print(message.data.toString());
print(message.notification.title);
}
void main() async {
WidgetsFlutterBinding.ensureInitialized();
LocalNotificationService.initialize();
await Firebase.initializeApp();
FirebaseMessaging.onBackgroundMessage(backgroundHandler);
runApp(MyApp());
}
现在,我尝试使用我的Android频道从Firebase云消息控制进行测试,它运行得很好,但自动云功能在Android中不起作用。
知道会出什么问题吗?
嘿,让你知道,问题似乎是Payload的工作方式与我在Android部分的工作方式不同:
android:{通知:{channelId:";roofdeck_default";,click_action:"FLUTTER_NOTIFICATION_click",title:发件人,身体:身体,}},
将channelId改为channel_id。
问候