Skip to content

Commit

Permalink
Update ios badge after receiving a data message
Browse files Browse the repository at this point in the history
  • Loading branch information
veloce committed Feb 2, 2024
1 parent 76420d6 commit 3ced42c
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 0 deletions.
21 changes: 21 additions & 0 deletions ios/Runner/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,27 @@ import Flutter
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {

let controller : FlutterViewController = window?.rootViewController as! FlutterViewController
let badgeChannel = FlutterMethodChannel(name: "mobile.lichess.org/badge",
binaryMessenger: controller.binaryMessenger)

badgeChannel.setMethodCallHandler({
(call: FlutterMethodCall, result: @escaping FlutterResult) -> Void in
guard call.method == "setBadge" else {
result(FlutterMethodNotImplemented)
return
}

if let args = call.arguments as? Dictionary<String, Any>,
let badge = args["badge"] as? Int {
UIApplication.shared.applicationIconBadgeNumber = badge
result(nil)
} else {
result(FlutterError(code: "bad_args", message: "bad arguments", details: nil))
}
})

GeneratedPluginRegistrant.register(with: self)
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
Expand Down
11 changes: 11 additions & 0 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import 'package:lichess_mobile/src/model/common/id.dart';
import 'package:lichess_mobile/src/model/correspondence/correspondence_game_storage.dart';
import 'package:lichess_mobile/src/model/correspondence/offline_correspondence_game.dart';
import 'package:lichess_mobile/src/model/game/playable_game.dart';
import 'package:lichess_mobile/src/utils/badge_service.dart';
import 'package:lichess_mobile/src/utils/layout.dart';
import 'package:path/path.dart' as path;
import 'package:sqflite/sqflite.dart';
Expand Down Expand Up @@ -118,4 +119,14 @@ Future<void> firebaseMessagingBackgroundHandler(RemoteMessage message) async {
conflictAlgorithm: ConflictAlgorithm.replace,
);
}

// update badge
final badge = message.data['lichess.iosBadge'] as String?;
if (badge != null) {
try {
badgeService.setBadge(int.parse(badge));
} catch (e) {
debugPrint('Could not parse badge: $badge');
}
}
}
11 changes: 11 additions & 0 deletions lib/src/notification_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import 'package:lichess_mobile/src/model/auth/auth_session.dart';
import 'package:lichess_mobile/src/model/common/id.dart';
import 'package:lichess_mobile/src/model/correspondence/correspondence_service.dart';
import 'package:lichess_mobile/src/model/game/playable_game.dart';
import 'package:lichess_mobile/src/utils/badge_service.dart';
import 'package:logging/logging.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';

Expand Down Expand Up @@ -87,5 +88,15 @@ class NotificationService {
}
ref.read(correspondenceServiceProvider).updateGame(fullId, game);
}

// update badge
final badge = message.data['lichess.iosBadge'] as String?;
if (badge != null) {
try {
badgeService.setBadge(int.parse(badge));
} catch (e) {
_log.severe('Could not parse badge: $badge');
}
}
}
}
32 changes: 32 additions & 0 deletions lib/src/utils/badge_service.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import 'dart:async';

import 'package:flutter/services.dart';
import 'package:logging/logging.dart';

final badgeService = BadgeService(Logger('BadgeService'));

class BadgeService {
static const _channel = MethodChannel('mobile.lichess.org/badge');

const BadgeService(this._log);

final Logger _log;

Future<void> setBadge(int value) async {
try {
await _channel.invokeMethod<int>('setBadge', <String, dynamic>{
'badge': value,
});
} on PlatformException catch (e) {
_log.severe(e);
}
}

Future<void> clearBadge() async {
try {
await _channel.invokeMethod<int>('setBadge', 0);
} on PlatformException catch (e) {
_log.severe(e);
}
}
}

0 comments on commit 3ced42c

Please sign in to comment.