Skip to content

Instantly share code, notes, and snippets.

View Ayush783's full-sized avatar

Aayush sharma Ayush783

View GitHub Profile
@Ayush783
Ayush783 / sendEvent.kt
Created March 7, 2024 12:06
Iterate through methodChannels and invoke method
fun sendEvent(event: String, body: Map<String, Any>) {
eventHandlers.reapCollection().forEach { it.get()?.send(event, body) }
if (event == CallkitConstants.ACTION_CALL_DECLINE) {
for ((name, channel) in methodChannels) {
try {
channel.invokeMethod("CALL_DECLINED_CUSTOM", "")
} catch (e: Exception) {
Log.d(EXTRA_CALLKIT_CALL_DATA, e.toString())
}
@Ayush783
Ayush783 / callkit_service.dart
Last active March 8, 2024 05:05
updated calling kit service
class CallingKitService {
static final CallingKitService _instance = CallingKitService._internal();
factory CallingKitService() {
return _instance;
}
CallingKitService._internal() {
MethodChannel('YOUR_CHANNEL_NAME').setMethodCallHandler(
(call) async {
@Ayush783
Ayush783 / callingkit_service.dart
Created March 7, 2024 11:40
Service class for FlutterCallingKit
class CallingKitService {
static final CallingKitService _instance = CallingKitService._internal();
factory CallingKitService() {
return _instance;
}
CallingKitService._internal() {
MethodChannel('YOUR_CHANNEL_NAME').setMethodCallHandler(
(call) async {
@Ayush783
Ayush783 / MainActivity.kt
Last active March 7, 2024 11:40
Intercept the call accepted action intent from flutter_callkit_incoming when app is launched and passing it to flutter
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
channel = MethodChannel(flutterEngine?.dartExecutor?.binaryMessenger!!, "YOUR_CHANNEL_NAME")
val appOpenedIntent = intent
if (
appOpenedIntent != null &&
appOpenedIntent.action == "com.hiennv.flutter_callkit_incoming.ACTION_CALL_ACCEPT"
) {
val extras = appOpenedIntent.extras
if (extras != null) {
@Ayush783
Ayush783 / show_call_notification.dart
Last active March 7, 2024 11:32
Show a call notification in Flutter using flutter_callkit_incoming
void showCallNotification(Map payload) {
var params = CallKitParams(
id: Uuid().v1(),
nameCaller: payload['name'],
appName: 'App Name',
avatar: payload['imageUrl'],
handle: '0123456789',
appLogo: 'assets/images/app_logo.png',
duration: 45000,
// This is the data that you will be extracting once app is opened
@Ayush783
Ayush783 / push_notification_work_requests.java
Created February 10, 2024 18:06
Register push notification worker requests
List < OneTimeWorkRequest > notificationScheduleList = new ArrayList();
// create list of OneTimeRequests for re triggering notifications every 5 minutes after
// a new notification is received
for (int i = 1; i < 3; i++) {
notificationScheduleList.add(
new OneTimeWorkRequest.Builder(PushNotificationSchedulerWork.class) // Specify the worker
.setInitialDelay(30 * i, TimeUnit.MINUTES) // First one at 30th minute, Next at 60th minute
.addTag("PushNotificationSchedulerWork") // Specify a work tag
.build()
@Ayush783
Ayush783 / push_notification_scheduler_worker.kt
Created February 10, 2024 17:50
Push notification retrigger worker
class PushNotificationSchedulerWork(context: Context, workerParams: WorkerParameters) : Worker(context, workerParams) {
val tag = "PushNotificationSchedulerWork"
@RequiresApi(Build.VERSION_CODES.O)
override fun doWork(): Result {
try {
val context = applicationContext
val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
val activeNotifications = notificationManager.activeNotifications
@Ayush783
Ayush783 / retrigger.java
Created February 10, 2024 16:05
Re triggering a notification on Android
@SuppressLint("NotificationTrampoline")
void reTriggerNotification(Context context, Bundle extras, StatusBarNotification notification, String channelId, NotificationManager nm) {
NotificationCompat.Builder nb = new NotificationCompat.Builder(context, channelId);
Notification n = notification.getNotification();
// ...
// Set all attributes you want on your notification.
// Set unique group IDs if you're disabling Notification bundling
// Set custom layouts, stale notification etc
// ...
@Ayush783
Ayush783 / sort_active_notifications.java
Last active February 10, 2024 13:18
Sorting active notifications on android
// METHOD TO SORT
sort() {
activeNotifications = notificationManager.getActiveNotifications();
Arrays.sort(activeNotifications, PushNotificationUtil.postTimeComparator);
while (activeNotifications.size() > 6) { // You can change this to any number. This defines the limit on notifications
StatusBarNotification notification = activeNotifications.remove(0);
notificationManager.cancel(notification.getId());
}
}
@Ayush783
Ayush783 / prevent_notification_bundling.kt
Created February 1, 2024 08:23
Setting unique group key for each notification
createNotification() {
...
notificationBuilder.setGroup(getUniqueGroupKey(10))
...
}
fun getUniqueGroupKey(sizeOfRandomString: Int): String {
val random = Random()
val sb = StringBuilder(sizeOfRandomString)
for (i in 0 until sizeOfRandomString)