push notifications added.
parent
acee8f5488
commit
58f5fbab5e
@ -1,65 +1,80 @@
|
|||||||
|
import 'dart:io';
|
||||||
|
|
||||||
|
import 'package:flutter/foundation.dart';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
|
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
|
||||||
|
import 'package:permission_handler/permission_handler.dart';
|
||||||
|
|
||||||
class NotificationManger {
|
class NotificationManger {
|
||||||
// private constructor to avoid create object
|
// private constructor to avoid create object
|
||||||
NotificationManger._();
|
NotificationManger._();
|
||||||
|
|
||||||
static FlutterLocalNotificationsPlugin localNotificationsPlugin = FlutterLocalNotificationsPlugin();
|
static FlutterLocalNotificationsPlugin localNotificationsPlugin; //= FlutterLocalNotificationsPlugin();
|
||||||
|
|
||||||
/// initialisation setting for all platform
|
/// initialisation setting for all platform
|
||||||
/// onNotificationPressed action when notification pressed to open tap
|
/// onNotificationPressed action when notification pressed to open tap
|
||||||
/// onIOSNotificationPressed action when notification pressed
|
/// onIOSNotificationPressed action when notification pressed
|
||||||
/// to open tap in iOS versions older than 10
|
/// to open tap in iOS versions older than 10
|
||||||
|
Future<void> requestPermissions() async {
|
||||||
|
|
||||||
|
}
|
||||||
static initialisation(Function(NotificationResponse) onNotificationPressed, DidReceiveLocalNotificationCallback onIOSNotificationPressed) async {
|
static initialisation(Function(NotificationResponse) onNotificationPressed, DidReceiveLocalNotificationCallback onIOSNotificationPressed) async {
|
||||||
FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();
|
FlutterLocalNotificationsPlugin _flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();
|
||||||
// initialise the plugin. app_icon needs to be a added as a drawable resource to the Android head project
|
// initialise the plugin. app_icon needs to be a added as a drawable resource to the Android head project
|
||||||
const AndroidInitializationSettings initializationSettingsAndroid = AndroidInitializationSettings(
|
const AndroidInitializationSettings initializationSettingsAndroid = AndroidInitializationSettings('@drawable/ic_stat_name');
|
||||||
'app_icon',
|
|
||||||
);
|
|
||||||
|
|
||||||
final DarwinInitializationSettings initializationSettingsDarwin = DarwinInitializationSettings(onDidReceiveLocalNotification: onIOSNotificationPressed);
|
final DarwinInitializationSettings initializationSettingsDarwin = DarwinInitializationSettings(onDidReceiveLocalNotification: onIOSNotificationPressed);
|
||||||
|
|
||||||
final InitializationSettings initializationSettings = InitializationSettings(android: initializationSettingsAndroid, iOS: initializationSettingsDarwin, macOS: initializationSettingsDarwin);
|
final InitializationSettings initializationSettings = InitializationSettings(android: initializationSettingsAndroid, iOS: initializationSettingsDarwin, macOS: initializationSettingsDarwin);
|
||||||
|
localNotificationsPlugin = _flutterLocalNotificationsPlugin;
|
||||||
|
|
||||||
|
if (Platform.isIOS) {
|
||||||
|
await localNotificationsPlugin.resolvePlatformSpecificImplementation<IOSFlutterLocalNotificationsPlugin>()?.requestPermissions(alert: true, badge: true, sound: true);
|
||||||
|
} else if (Platform.isAndroid) {
|
||||||
|
AndroidFlutterLocalNotificationsPlugin androidImplementation = localNotificationsPlugin.resolvePlatformSpecificImplementation<AndroidFlutterLocalNotificationsPlugin>();
|
||||||
|
bool granted = await androidImplementation?.requestPermission();
|
||||||
|
granted = granted ?? false;
|
||||||
|
if (granted == false) {
|
||||||
|
if (kDebugMode) {
|
||||||
|
print("-------------------- Permission Granted ------------------------");
|
||||||
|
print(granted);
|
||||||
|
}
|
||||||
|
await Permission.notification.request();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
await flutterLocalNotificationsPlugin.initialize(initializationSettings, onDidReceiveNotificationResponse: onNotificationPressed);
|
await localNotificationsPlugin.initialize(initializationSettings, onDidReceiveNotificationResponse: onNotificationPressed);
|
||||||
|
//localNotificationsPlugin = flutterLocalNotificationsPlugin;
|
||||||
}
|
}
|
||||||
|
|
||||||
// push new notification
|
|
||||||
static Future showNotification({@required BuildContext context, @required String title, @required String subtext, @required int hashcode, String payload}) async {
|
|
||||||
final AndroidNotificationDetails androidChannel = AndroidNotificationDetails(
|
// push new notificationBuildContext
|
||||||
'com.newtrack.testsa',
|
static Future showNotification({@required context, @required String title, @required String subtext, @required int hashcode, String payload}) async {
|
||||||
'Test SA',
|
const AndroidNotificationDetails androidChannel = AndroidNotificationDetails(
|
||||||
channelDescription: 'Push notification service for Test SA',
|
'com.hmg.atoms',
|
||||||
|
'ATOMS',
|
||||||
|
channelDescription: 'Push notification service for ATOMS',
|
||||||
importance: Importance.max,
|
importance: Importance.max,
|
||||||
priority: Priority.max,
|
priority: Priority.max,
|
||||||
|
icon: "@drawable/ic_stat_name",
|
||||||
playSound: true,
|
playSound: true,
|
||||||
channelShowBadge: true,
|
channelShowBadge: true,
|
||||||
enableLights: true,
|
// enableLights: false,
|
||||||
visibility: NotificationVisibility.public,
|
visibility: NotificationVisibility.public,
|
||||||
ledColor: Theme.of(context).colorScheme.secondary,
|
//ledColor: Theme.of(context).colorScheme.secondary,
|
||||||
ledOnMs: 1,
|
// ledOnMs: 1,
|
||||||
ledOffMs: 0,
|
// ledOffMs: 0,
|
||||||
enableVibration: true,
|
enableVibration: true,
|
||||||
groupKey: 'com.newtrack.testsa',
|
groupKey: 'com.hmg.atoms',
|
||||||
);
|
);
|
||||||
|
|
||||||
const DarwinNotificationDetails iosNotificationDetails = DarwinNotificationDetails(
|
const DarwinNotificationDetails iosNotificationDetails = DarwinNotificationDetails(
|
||||||
categoryIdentifier: "testSA",
|
categoryIdentifier: "atoms",
|
||||||
);
|
);
|
||||||
|
|
||||||
final platformChannel = NotificationDetails(
|
const platformChannel = NotificationDetails(android: androidChannel, iOS: iosNotificationDetails, macOS: iosNotificationDetails);
|
||||||
android: androidChannel,
|
|
||||||
iOS: iosNotificationDetails,
|
|
||||||
macOS: iosNotificationDetails,
|
|
||||||
);
|
|
||||||
|
|
||||||
await localNotificationsPlugin.show(
|
await localNotificationsPlugin.show(hashcode, title, subtext, platformChannel, payload: payload);
|
||||||
hashcode,
|
|
||||||
title,
|
|
||||||
subtext,
|
|
||||||
platformChannel,
|
|
||||||
payload: payload,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue