Created
April 24, 2024 19:47
-
-
Save iAmWillShepherd/4f8fc883cf5bc08556b94e2080267a7e to your computer and use it in GitHub Desktop.
Snippet for initializing OneSignal User Model SDK in Flutter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
import 'package:onesignal_flutter/onesignal_flutter.dart'; | |
import 'package:flutter_application_la/live_actiivties_manager.dart'; | |
void main() { | |
runApp(const MyApp()); | |
} | |
class MyApp extends StatelessWidget { | |
const MyApp({super.key}); | |
@override | |
Widget build(BuildContext context) { | |
LiveActivitiesManager.register(); | |
OneSignal.initialize("<<YOUR_APP_ID>>"); | |
OneSignal.Notifications.requestPermission(true); | |
return MaterialApp( | |
title: 'Flutter Demo', | |
theme: ThemeData( | |
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple), | |
useMaterial3: true, | |
), | |
home: const MyHomePage(title: 'Flutter Demo Home Page'), | |
); | |
} | |
} | |
class MyHomePage extends StatefulWidget { | |
const MyHomePage({super.key, required this.title}); | |
final String title; | |
@override | |
State<MyHomePage> createState() => _MyHomePageState(); | |
} | |
class _MyHomePageState extends State<MyHomePage> { | |
final TextEditingController _activityIdController = TextEditingController(text: ''); | |
final TextEditingController _nameController = TextEditingController(text: 'Test LA Name'); | |
final TextEditingController _emojiController = TextEditingController(text: '😀'); | |
void _startLiveActivity() { | |
LiveActivitiesManager.startLiveActivity(_activityIdController.text, _nameController.text, _emojiController.text); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
backgroundColor: Theme.of(context).colorScheme.inversePrimary, | |
title: Text(widget.title), | |
), | |
body: Center( | |
child: Padding( | |
padding: const EdgeInsets.all(8.0), | |
child: Column( | |
mainAxisAlignment: MainAxisAlignment.center, | |
children: <Widget>[ | |
TextFormField( | |
controller: _activityIdController, | |
decoration: const InputDecoration( | |
border: UnderlineInputBorder(), | |
labelText: 'Activity ID:', | |
), | |
), | |
TextFormField( | |
controller: _nameController, | |
decoration: const InputDecoration( | |
border: UnderlineInputBorder(), | |
labelText: 'Name:', | |
), | |
), | |
TextFormField( | |
controller: _emojiController, | |
decoration: const InputDecoration( | |
border: UnderlineInputBorder(), | |
labelText: 'Emoji:', | |
), | |
), | |
], | |
) | |
), | |
), | |
floatingActionButton: FloatingActionButton( | |
onPressed: _startLiveActivity, | |
tooltip: 'Start Live Activity', | |
child: const Icon(Icons.add), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment