【发布时间】:2021-06-24 02:12:59
【问题描述】:
我正在尝试在应用关闭或处于后台时停止来自 firebase_message 的默认通知。
我想在应用关闭时或使用 local_notifications 插件在后台显示自定义通知,该插件按预期工作,发送消息时问题仍然存在,应用显示相同的通知两次,默认通知 1 次,另一次使用自定义通知。
我无法实现停止默认通知。谁能在这里指出我的错误?
初始代码来自 firebase 消息传递示例,删除了不必要的 sn-ps 并添加了自定义通知。
还附上了当前(不良行为)的图像
如前所述,相同的通知显示了两次,想去掉图像上显示的第一个。
这是我的代码:
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'dart:async';
import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter/material.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'utils/utils.dart';
Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
print('_firebaseMessagingBackgroundHandler');
Future onDidReceiveLocalNotification(
int? id, String? title, String? body, String? payload) async {
print('onDidReceiveLocalNotification');
}
final channel = AndroidNotificationChannel(
'high_importance_channel', // id
'High Importance Notifications', // title
'This channel is used for important notifications.', // description
importance: Importance.high,
);
var flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();
final initializationSettingsAndroid =
AndroidInitializationSettings('icon_app');
final initializationSettingsIOS = IOSInitializationSettings(
onDidReceiveLocalNotification: onDidReceiveLocalNotification);
final initializationSettingsMacOS = MacOSInitializationSettings();
await flutterLocalNotificationsPlugin
.resolvePlatformSpecificImplementation<
AndroidFlutterLocalNotificationsPlugin>()
?.createNotificationChannel(channel);
final initializationSettings = InitializationSettings(
android: initializationSettingsAndroid,
iOS: initializationSettingsIOS,
macOS: initializationSettingsMacOS);
await flutterLocalNotificationsPlugin.initialize(
initializationSettings,
onSelectNotification: onSelectNotification,
);
///Not able to stop default notification
///there fore when custom notification is called
///result is 2 notifications displayed.
NotificationDetails _notificationDetails;
_notificationDetails = await customNotification(message: message);
flutterLocalNotificationsPlugin.show(
message.notification.hashCode,
message.notification!.title,
message.notification!.body,
_notificationDetails,
payload: '',
);
// await Firebase.initializeApp();
print('Handling a background message ${message.messageId}');
}
Future<NotificationDetails> customNotification(
{required RemoteMessage message}) async {
print('notificationDetailsBigImage');
final utils = Utils();
final bigPicturePath = await utils.downloadAndSaveFile(
url: 'https://picsum.photos/536/354', fileName: 'bigPicture');
final bigPictureStyleInformation = BigPictureStyleInformation(
FilePathAndroidBitmap(bigPicturePath),
hideExpandedLargeIcon: true,
);
final androidPlatformChannelSpecifics = AndroidNotificationDetails(
'big text channel name',
'big text channel name',
'big text channel description',
styleInformation: bigPictureStyleInformation,
icon: 'icon_app',
color: const Color.fromARGB(255, 255, 0, 0),
ledColor: const Color.fromARGB(255, 255, 0, 0),
ledOnMs: 1000,
ledOffMs: 500,
);
return NotificationDetails(android: androidPlatformChannelSpecifics);
}
Future<void> onDidReceiveLocalNotification(
int? id, String? title, String? body, String? payload) async {
print('onDidReceiveLocalNotification');
}
Future<void> onSelectNotification(String? payload) async {
print('onSelectNotification');
}
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);
await FirebaseMessaging.instance.setForegroundNotificationPresentationOptions(
alert: true,
badge: true,
sound: true,
);
runApp(MessagingExampleApp());
}
/// Entry point for the example application.
class MessagingExampleApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Messaging Example App',
theme: ThemeData.dark(),
routes: {
'/': (context) => Application(),
},
);
}
}
/// Renders the example application.
class Application extends StatefulWidget {
@override
State<StatefulWidget> createState() => _Application();
}
class _Application extends State<Application> {
late String _token;
@override
void initState() {
super.initState();
FirebaseMessaging.instance.getInitialMessage().then((message) {
print('instance');
if (message != null) {
print('do stuff');
}
});
FirebaseMessaging.onMessage.listen((message) {
print('onMessage');
});
FirebaseMessaging.onMessageOpenedApp.listen((message) {
print('A new onMessageOpenedApp event was published!');
});
}
@override
Widget build(BuildContext context) {
return Scaffold();
}
}
【问题讨论】:
标签: flutter push-notification firebase-cloud-messaging