Skip to content

Instantly share code, notes, and snippets.

@valterh4ck3r
Last active August 19, 2020 19:17
Show Gist options
  • Save valterh4ck3r/e469e952ef2d4af47b86b598adefd9ec to your computer and use it in GitHub Desktop.
Save valterh4ck3r/e469e952ef2d4af47b86b598adefd9ec to your computer and use it in GitHub Desktop.
How to implement notification in flutter
_configureNotification(){
FirebaseMessaging _firebaseMessaging = FirebaseMessaging();
_firebaseMessaging.configure(
onMessage: (Map<String, dynamic> message) async {
print("onMessage");
print(message);
if(Platform.isIOS){
if(message["aps"] != null){
NotificationService.showNotification(message["aps"]["alert"]["title"],
message["aps"]["alert"]["body"]);
} else {
NotificationService.showNotification(message["notification"]["title"],
message["notification"]["body"]);
}
} else {
NotificationService.showNotification(message["notification"]["title"],
message["notification"]["body"]);
}
},
onResume: (Map<String, dynamic> message) async {
print("onResume");
print(message);
if(Platform.isIOS){
if(message["aps"] != null){
NotificationService.showNotification(message["aps"]["alert"]["title"],
message["aps"]["alert"]["body"]);
} else {
NotificationService.showNotification(message["notification"]["title"],
message["notification"]["body"]);
}
} else {
NotificationService.showNotification(message["notification"]["title"],
message["notification"]["body"]);
}
},
onLaunch: (Map<String, dynamic> message) async {
print("onLaunch");
print(message);
if(Platform.isIOS){
if(message["aps"] != null){
NotificationService.showNotification(message["aps"]["alert"]["title"],
message["aps"]["alert"]["body"]);
} else {
NotificationService.showNotification(message["notification"]["title"],
message["notification"]["body"]);
}
} else {
NotificationService.showNotification(message["notification"]["title"],
message["notification"]["body"]);
}
},
);
_firebaseMessaging.requestNotificationPermissions();
_firebaseMessaging.getToken().then((token){
print(token);
});
}
@override
void initState() {
super.initState();
_configureNotification();
}

How to Implement Firebase Cloud Messaging with Flutter

- Install and configure this package : https://pub.dev/packages/firebase_messaging#-readme-tab-
- Only Install this package : https://pub.dev/packages/flutter_local_notifications#-installing-tab-
- Add Notification Service file to your project files.
- Create ic_notification : https://romannurik.github.io/AndroidAssetStudio/index.html
- Copy ic_notification.png into android/app/src/main/res/drawable/ic_notification.png
- Implement _configureNotification() into your main screen
- Send your notification
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
class NotificationService {
static showNotification(String title , String body) async {
FlutterLocalNotificationsPlugin _flutterLocalNotificationsPlugin = new FlutterLocalNotificationsPlugin();
var initializationSettingsAndroid =
new AndroidInitializationSettings('ic_notification');
var initializationSettingsIOS = new IOSInitializationSettings();
var initializationSettings = new InitializationSettings(
initializationSettingsAndroid, initializationSettingsIOS);
_flutterLocalNotificationsPlugin.initialize(initializationSettings);
var androidPlatformChannelSpecifics = new AndroidNotificationDetails(
'0', 'Nome do App', 'Canal de Notificações',
importance: Importance.Max, priority: Priority.High , color: YOUR_COLOR_PRIMARY);
var iOSPlatformChannelSpecifics = new IOSNotificationDetails();
var platformChannelSpecifics = new NotificationDetails(
androidPlatformChannelSpecifics, iOSPlatformChannelSpecifics);
await _flutterLocalNotificationsPlugin.show(
0, title, body, platformChannelSpecifics,
payload: 'Notificação');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment