You cannot select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
		
			
		
			
				
	
	
		
			180 lines
		
	
	
		
			5.7 KiB
		
	
	
	
		
			Dart
		
	
			
		
		
	
	
			180 lines
		
	
	
		
			5.7 KiB
		
	
	
	
		
			Dart
		
	
| /*
 | |
| import 'dart:io' show Platform;
 | |
| import 'package:firebase_messaging/firebase_messaging.dart';
 | |
| import 'package:flutter/material.dart';
 | |
| import 'package:flutter_local_notifications/flutter_local_notifications.dart';
 | |
| 
 | |
| abstract class IFirebaseService {
 | |
|   Future<String> get token;
 | |
| 
 | |
|   Future<dynamic> backgroundMessageHandler(Map<String, dynamic> message);
 | |
| 
 | |
|   Future<dynamic> messageHandler(Map<String, dynamic> message);
 | |
| 
 | |
|   Future<dynamic> onLaunch(Map<String, dynamic> message);
 | |
| 
 | |
|   Future<dynamic> onResume(Map<String, dynamic> message);
 | |
| 
 | |
|   void subscribeForPromotions();
 | |
| 
 | |
|   void unsubscribeFromPromotions();
 | |
| }
 | |
| 
 | |
| //https://medium.com/@SebastianEngel/easy-push-notifications-with-flutter-and-firebase-cloud-messaging-d96084f5954f
 | |
| class FirebaseService implements IFirebaseService {
 | |
|   FirebaseMessaging _firebaseMessaging;
 | |
| 
 | |
|   FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin;
 | |
| 
 | |
|   FirebaseService() {.
 | |
|     _firebaseMessaging = FirebaseMessaging();
 | |
| 
 | |
|     //https://github.com/FirebaseExtended/flutterfire/issues/1695
 | |
|     _firebaseMessaging.configure(
 | |
|       onMessage: messageHandler,
 | |
|       onBackgroundMessage:
 | |
|       Platform.isAndroid ? myBackgroundMessageHandler : null,
 | |
|       onLaunch: onLaunch,
 | |
|       onResume: onResume,
 | |
|     );
 | |
| 
 | |
|     //monitor firebase token changes
 | |
|     //https://firebase.google.com/docs/cloud-messaging/android/client#sample-register
 | |
|     ///The registration token may change when:
 | |
|     //
 | |
|     //The app deletes Instance ID
 | |
|     //The app is restored on a new device
 | |
|     //The user uninstalls/reinstall the app
 | |
|     //The user clears app data.
 | |
|     ///
 | |
| 
 | |
|     //for the first release we don't care about token refreshes
 | |
|     /*Stream<String> fcmStream = _firebaseMessaging.onTokenRefresh;
 | |
|     fcmStream.listen((token) {
 | |
| 
 | |
|     });*/
 | |
| 
 | |
|     //ios specific settings
 | |
|     //taken from https://github.com/FirebaseExtended/flutterfire/blob/master/packages/firebase_messaging/example/lib/main.dart
 | |
|     _firebaseMessaging.requestNotificationPermissions(
 | |
|         const IosNotificationSettings(
 | |
|             sound: true, badge: true, alert: true, provisional: true));
 | |
|     _firebaseMessaging.onIosSettingsRegistered
 | |
|         .listen((IosNotificationSettings settings) {
 | |
|       print("Settings registered: $settings");
 | |
|     });
 | |
| 
 | |
|     var initializationSettingsAndroid =
 | |
|     AndroidInitializationSettings('app_icon');
 | |
|     var initializationSettingsIOS =
 | |
|     IOSInitializationSettings(onDidReceiveLocalNotification: onDidReceiveLocalNotification);
 | |
|     var initializationSettings = InitializationSettings(
 | |
|         initializationSettingsAndroid, initializationSettingsIOS);
 | |
|     flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();
 | |
|     flutterLocalNotificationsPlugin.initialize(initializationSettings,
 | |
|         onSelectNotification: selectNotification);
 | |
|   }
 | |
| 
 | |
|   Future<dynamic> onDidReceiveLocalNotification(int id, String title, String body, String payload)  async{
 | |
|     var androidPlatformChannelSpecifics = AndroidNotificationDetails(
 | |
|         'new_message_channel_id',
 | |
|         'Neue Nachricht',
 | |
|         'Channel für neue Nachrichten',
 | |
|         importance: Importance.Max,
 | |
|         priority: Priority.High,
 | |
|         ticker: 'ticker');
 | |
|     var iOSPlatformChannelSpecifics = IOSNotificationDetails();
 | |
|     var platformChannelSpecifics = NotificationDetails(
 | |
|         androidPlatformChannelSpecifics, iOSPlatformChannelSpecifics);
 | |
|     await flutterLocalNotificationsPlugin.show(
 | |
|         0,
 | |
|         title,
 | |
|         body,
 | |
|         platformChannelSpecifics);
 | |
|   }
 | |
| 
 | |
|   @override
 | |
|   Future backgroundMessageHandler(Map<String, dynamic> message) async {
 | |
|     await myBackgroundMessageHandler(message);
 | |
|   }
 | |
| 
 | |
|   @override
 | |
|   Future messageHandler(Map<String, dynamic> message) async {
 | |
|     print("onMessage: $message");
 | |
| 
 | |
| // initialise the plugin. app_icon needs to be a added as a drawable resource to the Android head project
 | |
| 
 | |
|     var androidPlatformChannelSpecifics = AndroidNotificationDetails(
 | |
|         'new_message_channel_id',
 | |
|         'Neue Nachricht',
 | |
|         'Channel für neue Nachrichten',
 | |
|         importance: Importance.Max,
 | |
|         priority: Priority.High,
 | |
|         ticker: 'ticker');
 | |
|     var iOSPlatformChannelSpecifics = IOSNotificationDetails();
 | |
|     var platformChannelSpecifics = NotificationDetails(
 | |
|         androidPlatformChannelSpecifics, iOSPlatformChannelSpecifics);
 | |
| 
 | |
|     if(Platform.isAndroid) {
 | |
|       await flutterLocalNotificationsPlugin.show(
 | |
|           0,
 | |
|           message["notification"]["title"],
 | |
|           message["notification"]["body"],
 | |
|           platformChannelSpecifics);
 | |
|     }else if(Platform.isIOS){
 | |
|       await flutterLocalNotificationsPlugin.show(
 | |
|           0,
 | |
|           message["aps"]["alert"]["title"],
 | |
|           message["aps"]["alert"]["body"],
 | |
|           platformChannelSpecifics);
 | |
|     }
 | |
|   }
 | |
| 
 | |
|   Future selectNotification(String payload) async {
 | |
|     if (payload != null) {
 | |
|       debugPrint('notification payload: ' + payload);
 | |
|     }
 | |
|   }
 | |
| 
 | |
|   @override
 | |
|   Future onLaunch(Map<String, dynamic> message) async {
 | |
|     print("onLaunch: $message");
 | |
|   }
 | |
| 
 | |
|   @override
 | |
|   Future onResume(Map<String, dynamic> message) async {
 | |
|     print("onResume: $message");
 | |
|   }
 | |
| 
 | |
|   @override
 | |
|   Future<String> get token => _firebaseMessaging.getToken();
 | |
| 
 | |
|   @override
 | |
|   void subscribeForPromotions() {
 | |
|     _firebaseMessaging.subscribeToTopic("promotions");
 | |
|   }
 | |
| 
 | |
|   @override
 | |
|   void unsubscribeFromPromotions() {
 | |
|     _firebaseMessaging.unsubscribeFromTopic("promotions");
 | |
|   }
 | |
| }
 | |
| 
 | |
| Future<dynamic> myBackgroundMessageHandler(Map<String, dynamic> message) {
 | |
|   debugPrint("BACKGROUND MESSAGE RECEIVED");
 | |
|   print("BACKGROUND MESSAGE RECEIVED");
 | |
|   return Future.value(() => true);
 | |
| 
 | |
|   /*if (message.containsKey('data')) {
 | |
|     // Handle data message
 | |
|     final dynamic data = message['data'];
 | |
|   }
 | |
| 
 | |
|   if (message.containsKey('notification')) {
 | |
|     // Handle notification message
 | |
|     final dynamic notification = message['notification'];
 | |
|   }*/
 | |
| 
 | |
|   // Or do other work.
 | |
| }
 | |
| */ |