admin管理员组文章数量:1355522
// Top-level function to handle notification tap in the background
@pragma('vm:entry-point')
void notificationTapBackground(NotificationResponse notificationResponse) {
NotificationHandler().handleMessageFromPayload(notificationResponse.payload);
}
// Initialize local notifications
FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin();
void initLocalNotification(RemoteMessage message) async {
var androidInitializationSettings =
const AndroidInitializationSettings('@mipmap/ic_launcher');
var iosAppInitializationSettings = const DarwinInitializationSettings(
defaultPresentSound: true,
defaultPresentBadge: true,
defaultPresentAlert: true,
);
var initializationSettings = InitializationSettings(
android: androidInitializationSettings,
iOS: iosAppInitializationSettings,
);
await flutterLocalNotificationsPlugin.initialize(
initializationSettings,
onDidReceiveNotificationResponse: (notificationResponse) {
handleMessage(message);
},
onDidReceiveBackgroundNotificationResponse: notificationTapBackground,
);
}
// Function to show notification
Future<void> showNotification(RemoteMessage message, String title, String body) async {
try {
AndroidNotificationChannel channel = const AndroidNotificationChannel(
"12345678",
'Chat',
importance: Importance.high,
showBadge: true,
enableVibration: true,
enableLights: true,
playSound: true,
);
AndroidNotificationDetails androidNotificationDetails =
AndroidNotificationDetails(
channel.id.toString(),
channel.name.toString(),
channelDescription: 'Chat',
importance: Importance.high,
priority: Priority.high,
ticker: 'ticker',
fullScreenIntent: true,
visibility: NotificationVisibility.public,
);
DarwinNotificationDetails darwinNotificationDetails =
const DarwinNotificationDetails(
presentAlert: true,
presentSound: true,
presentBadge: true,
);
NotificationDetails notificationDetails = NotificationDetails(
android: androidNotificationDetails,
iOS: darwinNotificationDetails,
);
flutterLocalNotificationsPlugin.show(
0,
title,
body,
notificationDetails,
);
} catch (e) {
logger('Show notification error: ${e.toString()}');
}
}
// Initialize Firebase & Listen for Notifications
void initFirebase() async {
logger('Initializing Firebase');
await Firebase.initializeApp();
FirebaseMessaging.onMessage.listen((message) async {
initLocalNotification(message);
logger('Received Message Data From Notification: ${message.data}');
final data = message.data;
final commonProvider = sl<CommonProvider>();
final authProvider = sl<AuthProvider>();
if (data.isNotEmpty) {
if (data['group'] == "true") {
if (commonProvider.activeId == data['toId']) {
logger('No Notification: Active ID matches for group chat');
} else {
showNotification(
message,
message.notification?.title ?? '',
message.notification?.body ?? '',
);
}
} else {
if (commonProvider.activeId == data['fromId']) {
logger('No Notification: Active ID matches for personal chat');
} else {
if (authProvider.userId == data['userId']) {
logger('No Notification: Task-related event');
} else {
showNotification(
message,
message.notification?.title ?? '',
message.notification?.body ?? '',
);
}
}
}
} else {
showNotification(
message,
message.notification?.title ?? '',
message.notification?.body ?? '',
);
}
});
FirebaseMessaging.instance.setForegroundNotificationPresentationOptions(
alert: true,
badge: true,
sound: true,
);
}
this code is works with android but not for iOS
本文标签:
版权声明:本文标题:android - How to show & hide firebase push notificationin on condition basis on iOS with flutter? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744047987a2581889.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论