Zah*_*hra 2 push-notification ios flutter firebase-cloud-messaging firebase-notifications
我正在订阅一个主题并使用 Firebase 控制台/云消息传递推送通知;在 Android 上,一切正常。在 iOS 上,当应用程序处于前台/后台/终止(配置文件模式)时,我会在通知中心收到通知
问题: iOS 应用程序图标上没有出现徽章。
我一直在检查不同的文章和质量保证,但仍然没有解决方案。任何可以帮助我进行更多调查的线索或想法都可能解决这个问题,我们将不胜感激。
已尝试 >在调试和配置文件模式下运行,使用配备 iOS 15 和 iPhone Xs 的 iPhone 12 pro
我也尝试过flutter_app_badger仍然没有得到任何徽章计数。
在我的main档案中;
Future<void> _messageHandler(RemoteMessage message) async {
print('background message ${message.notification!.body}');
}
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform,
).then((value) {
FirebaseMessaging.onBackgroundMessage(_messageHandler);
});
runApp(MyApp());
}
Run Code Online (Sandbox Code Playgroud)
在initState我的应用程序的第一个屏幕中;
@override
void initState() {
super.initState();
PushNotify().init(context);
messaging = FirebaseMessaging.instance;
messaging.subscribeToTopic("messaging");
messaging.setForegroundNotificationPresentationOptions(
alert: true, // Required to display a heads up notification
badge: true,
sound: true,
);
if (Platform.isIOS) {
messaging.requestPermission(
alert: true,
announcement: false,
badge: true,
carPlay: false,
criticalAlert: false,
provisional: false,
sound: true,
);
}
FirebaseMessaging.onMessage.listen((RemoteMessage event) {
// FlutterAppBadger.updateBadgeCount(1);
if (event.notification != null) {
print('Message also contained a notification: ${event.notification}');
}
});
FirebaseMessaging.onMessageOpenedApp.listen((message) {
print('Message clicked!');
// FlutterAppBadger.removeBadge();
});
}
Run Code Online (Sandbox Code Playgroud)
过去一个月我自己就在这个问题上挣扎。
我意识到,在发送通知时,您能够显示徽章(在 iOS 上)的唯一方法是从自定义服务器而不是 Firebase 控制台发送该消息。
这里的关键问题是用于发送通知的有效负载。您必须在有效负载中显式包含徽章计数,才能将其显示在 iOS 应用程序图标上。
如果您熟悉 Typescript 或 Javascript,使用此有效负载发送消息将在 iOS 应用程序图标上显示给定的徽章(适用于应用程序在后台或终止时发送的消息)
const createTokenPayload = (
token: string,
title: string,
message: string,
id?: string,
badge?: number
): TokenMessage => {
const payload: TokenMessage = {
token: token,
notification: {
title: title,
body: message,
},
apns: {
payload: {
aps: {
contentAvailable: true,
badge: badge, // this one here
},
},
},
android: {
priority: "high",
},
};
return payload;
};
Run Code Online (Sandbox Code Playgroud)
这是针对发送到单个令牌的通知。
当我想发送给订阅某个主题的所有用户时,我找不到添加徽章的方法。我认为背后的原因是;向订阅某个主题的所有用户发送具有相同值的徽章,而不管他们当前的徽章数量是多少,这种做法并不好。
因此,我针对此问题建议的解决方案是发送具有相同消息但不同徽章的单独通知,具体取决于每个用户徽章的当前值。
我希望这有帮助。