简体   繁体   English

flutter_local_notifications 路由全屏

[英]flutter_local_notifications route fullscreen

flutter_local_notifications flutter_local_notifications

i create fullscreen function with FCM background msg.我使用 FCM 背景消息创建全屏功能。

when i receive a notification, the home page is brought to the front where this function is declared当我收到通知时,主页被带到声明此函数的最前面

How to force a switch to another page when triggered?触发时如何强制切换到另一个页面?

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);
}

Here is the solution.这是解决方案。

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 ?? ""));
          },
        ),
      ],
    ),
  ),
);
}

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM