flatter_local_notifications
我用FCM后台消息创建全屏功能。
当我收到通知时,主页被带到前面,这个功能被声明为
如何在触发时强制切换到另一个页面?
Future<dynamic> myBackgroundMessageHandler(Map<String, dynamic> message) async {
// print("myBackgroundMessageHandler message: $message");
int msgId = int.tryParse(message["data"]["msgId"].toString()) ?? 0;
// print("msgId $msgId");
_showFullScreenNotification();
}
Future<void> _showFullScreenNotification() async {
const AndroidNotificationDetails androidPlatformChannelSpecifics =
AndroidNotificationDetails('your other channel id',
'your other channel name', 'your other channel description',
sound: RawResourceAndroidNotificationSound('slow_spring_board'),
priority: Priority.high,
importance: Importance.high,
fullScreenIntent: true);
const IOSNotificationDetails iOSPlatformChannelSpecifics =
IOSNotificationDetails(sound: 'slow_spring_board.aiff');
const MacOSNotificationDetails macOSPlatformChannelSpecifics =
MacOSNotificationDetails(sound: 'slow_spring_board.aiff');
const NotificationDetails platformChannelSpecifics = NotificationDetails(
android: androidPlatformChannelSpecifics,
iOS: iOSPlatformChannelSpecifics,
macOS: macOSPlatformChannelSpecifics);
await flutterLocalNotificationsPlugin.show(
0,
'custom sound notification title',
'custom sound notification body',
platformChannelSpecifics);
}
这是解决方案。
final BehaviorSubject<String> selectNotificationSubject =
BehaviorSubject<String>();
final FlutterLocalNotificationsPlugin localNotifications =
FlutterLocalNotificationsPlugin();
final BehaviorSubject<ReceivedNotification>
didReceiveLocalNotificationSubject=BehaviorSubject<ReceivedNotification>();
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
final NotificationAppLaunchDetails notificationAppLaunchDetails = await localNotifications.getNotificationAppLaunchDetails();
const AndroidInitializationSettings initializationSettingsAndroid = AndroidInitializationSettings('app_icon');
final IOSInitializationSettings initializationSettingsIOS = IOSInitializationSettings(
requestAlertPermission: false,
requestBadgePermission: false,
requestSoundPermission: false,
onDidReceiveLocalNotification: (int id, String title, String body, String payload) async {
didReceiveLocalNotificationSubject.add(ReceivedNotification(
id: id, title: title, body: body, payload: payload));
});
final InitializationSettings initializationSettings = InitializationSettings(
android: initializationSettingsAndroid,
iOS: initializationSettingsIOS);
await localNotifications.initialize(initializationSettings,
onSelectNotification: (String payload) async {
if (payload != null) {
debugPrint('notification payload: $payload');
localNotifications.cancelAll();
}
selectNotificationSubject.add(payload);
});
runApp(
EasyLocalization(
supportedLocales: [
const Locale('en', ''),
],
path: 'assets/i18n',
fallbackLocale: const Locale('en', ''),
useOnlyLangCode: true,
saveLocale: true,
child: MyApp(),
),
);
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MultiBlocProvider(
providers: [
BlocProvider<CallBloc>(
create: (context) => CallBloc(),
)
],
child: MaterialApp(
navigatorKey: navigatorKey,
title: 'Flutter Demo',
theme: themeLight,
localizationsDelegates: context.localizationDelegates,
supportedLocales: context.supportedLocales,
locale: context.locale,
routes: {
"/" : (context) => TestScreen()
},
navigatorObservers: [PushNotificationsHandler()],
),
);
}
}
class TestScreen extends StatefulWidget {
const TestScreen({Key key}) : super(key: key);
@override
_TestScreenState createState() => _TestScreenState();
}
class _TestScreenState extends State<TestScreen>{
@override
void initState() {
// TODO: implement initState
super.initState();
_configureSelectNotificationSubject();
_configureDidReceiveLocalNotificationSubject();
checkCall();
checkPrefs();
}
void _configureSelectNotificationSubject() {
selectNotificationSubject.stream.listen((String payload) async {
print("pahload data here");
print(payload);
print(jsonDecode(jsonDecode(payload)['response']));
RepoData().setNotificationData(jsonDecode(payload)['response']);
await Navigator.push(context,
MaterialPageRoute<dynamic>(
builder: (context) => CallScreen(
message:
NotificationModel.fromJson(jsonDecode(jsonDecode(payload). ['response']))
)),
);
});
}
void _configureDidReceiveLocalNotificationSubject() {
didReceiveLocalNotificationSubject.stream
.listen((ReceivedNotification receivedNotification) async {
print("pahload data here for ios");
print(jsonDecode(jsonDecode(receivedNotification.payload)['response']));
RepoData().setNotificationData(jsonDecode(receivedNotification.payload).
['response']);
await Navigator.push(context,
MaterialPageRoute<dynamic>(
builder: (context) => CallScreen(
message:
NotificationModel.fromJson(
jsonDecode(jsonDecode(receivedNotification.payload).
['response']))
)),
);
});
}
checkPrefs(){
SharedPreferences.getInstance().then((value){
print(value.getKeys());
});
}
@override
void dispose() {
selectNotificationSubject.close();
didReceiveLocalNotificationSubject.close();
localNotifications.cancelAll();
super.dispose();
}
checkCall(){
RepoData().getNotificationDataHome().then((value){
if(value != null){
if(value == '0'){
ReviewBottomSheet(context).showSheet();
}else{
Future.delayed(Duration(seconds: 0),(){
if(jsonDecode(value)['room_type'] == "group"){
MeetingBottomSheet().showSheet(context,jsonDecode(value),RepoData().roomData);
}else{
MeetingBottomSheet().showSingleMeetingSheet(context,jsonDecode(value),
RepoData().roomData);
}
});
}
}
});
}
@override
Widget build(BuildContext context) {
print(token);
return Scaffold(
body: SafeArea(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Padding(
padding: EdgeInsets.symmetric(
horizontal: 50,
),
child: Text(token ?? ""),
),
RaisedButton(
child: Text("copy"),
onPressed: (){
Clipboard.setData(new ClipboardData(text: token ?? ""));
},
),
],
),
),
);
}
}