-
-
Save harshsoni1110/80ef8c9bf6c7b901d49a3920c36021e3 to your computer and use it in GitHub Desktop.
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
const notificationChannel = "Notification channel"; | |
const notificationChannelId = "Channel_id_1"; | |
const notificationChannelDescription = | |
"Notification channel description"; | |
class NotificationService extends Bloc<NotificationEvent, NotificationState> { | |
FirebaseMessaging _firebaseMessaging; | |
FlutterLocalNotificationsPlugin _localNotifications; | |
String _fcmToken; | |
static int _notificationId = 1; //Id for every notification | |
bool _hasLaunched = false; | |
String _payLoad; | |
NotificationService() { | |
_localNotifications = FlutterLocalNotificationsPlugin(); | |
_firebaseMessaging = new FirebaseMessaging(); | |
} | |
initialize() async { | |
NotificationAppLaunchDetails _appLaunchDetails = | |
await _localNotifications.getNotificationAppLaunchDetails(); | |
var initializationSettings = _getPlatformSettings(); | |
await _localNotifications.initialize(initializationSettings, | |
onSelectNotification: _handleNotificationTap); | |
_createNotificationChannel(); | |
if (Platform.isIOS) { | |
var hasPermission = await _requestIOSPermissions(); | |
if (hasPermission) { | |
await _fcmInitialization(); | |
} else { | |
add(NotificationErrorEvent( | |
"You can provide permission by going into Settings later.")); | |
} | |
} else { | |
await _fcmInitialization(); | |
} | |
_hasLaunched = _appLaunchDetails.didNotificationLaunchApp; | |
if (_hasLaunched) { | |
if (_appLaunchDetails.payload != null) { | |
_payLoad = _appLaunchDetails.payload; | |
} | |
} | |
} | |
Future<bool> _requestIOSPermissions() async { | |
var platformImplementation = | |
_localNotifications.resolvePlatformSpecificImplementation< | |
IOSFlutterLocalNotificationsPlugin>(); | |
bool permission = false; | |
if (platformImplementation != null) { | |
permission = await platformImplementation.requestPermissions( | |
alert: true, | |
badge: true, | |
sound: true, | |
); | |
} | |
return permission; | |
} | |
@override | |
NotificationState get initialState => StartUpNotificationState(); | |
@override | |
Stream<NotificationState> mapEventToState(NotificationEvent event) async* { | |
switch (event.runtimeType) { | |
case NotificationEvent: | |
Notification notification = Notification.fromJson(event.payload); | |
if (notification.notificationType == BindAppConstants.dayReminderType) { | |
yield IndexedNotification(1); | |
} | |
break; | |
case NotificationErrorEvent: | |
yield NotificationErrorState((event as NotificationErrorEvent).error); | |
break; | |
} | |
} | |
Future<void> _showNotification(Notification payload) async { | |
var vibrationPattern = Int64List(4); | |
vibrationPattern[0] = 0; | |
vibrationPattern[1] = 200; | |
vibrationPattern[2] = 200; | |
vibrationPattern[3] = 200; | |
var bigTextStyleInformation = BigTextStyleInformation( | |
payload.notificationBody, | |
contentTitle: payload.notificationTitle); | |
var androidPlatformChannelSpecifics = AndroidNotificationDetails( | |
_notificationId.toString(), | |
notificationChannel, | |
notificationChannelDescription, | |
icon: 'app_icon', | |
color: BindColors.primaryColor, | |
vibrationPattern: vibrationPattern, | |
importance: Importance.Max, | |
priority: Priority.Max, | |
styleInformation: bigTextStyleInformation); | |
var iOSPlatformChannelSpecifics = IOSNotificationDetails(); | |
var platformChannelSpecifics = NotificationDetails( | |
androidPlatformChannelSpecifics, iOSPlatformChannelSpecifics); | |
await _localNotifications.show( | |
payload.notificationId, | |
payload.notificationTitle, | |
payload.notificationBody, | |
platformChannelSpecifics, | |
payload: payload.toJson()); | |
} | |
Notification convertToNotification( | |
int notificationId, Map<String, dynamic> message) { | |
Notification notification; | |
if (Platform.isAndroid) { | |
notification = new Notification((object) => object | |
..notificationId = notificationId | |
..notificationTitle = message['notification']['title'] | |
..notificationBody = message['notification']['body'] | |
..notificationType = message['data']['notification_type']); | |
} else { | |
notification = new Notification((object) => object | |
..notificationId = notificationId | |
..notificationTitle = message['aps']['alert']['title'] | |
..notificationBody = message['aps']['alert']['body'] | |
..notificationType = message['notification_type']); | |
print(notification.toJson()); | |
} | |
return notification; | |
} | |
void _createNotificationChannel() async { | |
var androidNotificationChannel = AndroidNotificationChannel( | |
notificationChannelId, | |
notificationChannel, | |
notificationChannelDescription, | |
); | |
await _localNotifications | |
.resolvePlatformSpecificImplementation< | |
AndroidFlutterLocalNotificationsPlugin>() | |
?.createNotificationChannel(androidNotificationChannel); | |
} | |
_getPlatformSettings() { | |
var initializationSettingsAndroid = | |
AndroidInitializationSettings('app_icon'); | |
var initializationSettingsIOS = IOSInitializationSettings( | |
requestAlertPermission: true, | |
requestBadgePermission: true, | |
requestSoundPermission: true); | |
return InitializationSettings( | |
initializationSettingsAndroid, initializationSettingsIOS); | |
} | |
Future _handleNotificationTap(String payload) async { | |
if (payload != null) { | |
add(NotificationEvent(payload)); | |
} | |
} | |
Future _fcmInitialization() async { | |
try { | |
_fcmToken = await _firebaseMessaging.getToken(); | |
_firebaseMessaging.onTokenRefresh.listen((event) { | |
//API call can be done here to update token in back-end | |
_fcmToken = event; | |
}); | |
_firebaseMessaging.configure( | |
onMessage: (Map<String, dynamic> message) async { | |
Notification notification = | |
convertToNotification(_notificationId++, message); | |
await _showNotification(notification); | |
}, | |
onLaunch: (Map<String, dynamic> message) async { | |
print("onLaunch: $message"); | |
Notification notification = | |
convertToNotification(_notificationId++, message); | |
_hasLaunched = true; | |
_payLoad = notification.toJson(); | |
}, | |
onResume: (Map<String, dynamic> message) async { | |
print("onResume: $message"); | |
Notification notification = | |
convertToNotification(_notificationId++, message); | |
add(NotificationEvent(notification.toJson())); | |
}, | |
); | |
} catch (e) { | |
add(NotificationErrorEvent(e.toString())); | |
} | |
} | |
void checkForLaunchedNotifications() { | |
if (_hasLaunched && _payLoad != null) add(NotificationEvent(_payLoad)); | |
} | |
String getFcmToken() => _fcmToken; | |
} |
Hi,
Can you check following things?
- Check whether you've called initialize() method before you access getFcmToken() method?
- If yes, please check whether your FCM initialisation works or not? (By adding debugger over there)
It works, I just took out inizialize() from main.dart and put it in the bloc constructor
Hello,
How do u call u bloc from onBackgroundMessage ie the new messaging methods
regard
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello.
getFcmToken()
Doesn't work (return null), returns null when I call it this way: