Skip to content

Commit

Permalink
example
Browse files Browse the repository at this point in the history
  • Loading branch information
ueman committed Sep 25, 2024
1 parent 9abddff commit 477eb6d
Show file tree
Hide file tree
Showing 3 changed files with 171 additions and 0 deletions.
91 changes: 91 additions & 0 deletions passkit_server/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,95 @@

-------

PassKit allows you to work with Apple's PkPass and Order files. This is a Dart library, which allows you to integrate the PassKit enpoint in your shelf (and potentially dart_frog) application.

In order to show PassKit and Order files in Flutter, use the [`passkit_ui`](https://pub.dev/packages/passkit_ui) package, which includes ready made widgets.

Want to work with Apple's native PassKit APIs in Flutter? Consider using [`apple_passkit`](https://pub.dev/packages/apple_passkit).

Please read through the [Apple Documentation](https://developer.apple.com/documentation/walletpasses/adding-a-web-service-to-update-passes) for the server implementation first.

A brief example looks roughly like this. Unfortunately, it's not possible to move more logic into
the library, since it depends too much on your application logic.

```dart
import 'dart:async';
import 'package:passkit/src/pkpass/pkpass.dart';
import 'package:passkit_server/passkit_server.dart';
import 'package:shelf/shelf.dart';
import 'package:shelf/shelf_io.dart' as io;
import 'package:shelf_router/shelf_router.dart';
Future<void> main() async {
var app = Router();
app.addPassKitServer(CustomPassKitBackend());
app.get('/hello', (Request request) {
return Response.ok('hello-world');
});
var server = await io.serve(app.call, 'localhost', 8888);
}
class CustomPassKitBackend extends PassKitBackend {
@override
Future<PkPass?> getLatestPassFor(String identifier, String serial) async {
print('getLatestPassFor($identifier, $serial)');
return null;
}
@override
FutureOr<bool> isKnownDeviceId(String deviceId) {
print('isKnownDeviceId($deviceId)');
return true;
}
@override
FutureOr<bool> isValidAuthToken(String serial, String authToken) {
print('isValidAuthToken($serial, $authToken)');
return true;
}
@override
Future<void> logMessage(Map<String, dynamic> message) async {
print('logMessage($message)');
}
@override
Future<UpdatablePassResponse?> returnUpdatablePasses(
String deviceId,
String typeId,
String? lastTag,
) async {
print('returnUpdatablePasses($deviceId, $typeId, $lastTag)');
return null;
}
@override
Future<NotificationRegistrationReponse> startSendingPushNotificationsFor(
String deviceId,
String passTypeId,
String serialNumber,
String pushToken,
) async {
print(
'startSendingPushNotificationsFor($deviceId, $passTypeId, $serialNumber, $pushToken)',
);
return NotificationRegistrationReponse.created;
}
@override
Future<bool> stopSendingPushNotificationsFor(
String deviceId,
String passTypeId,
String serialNumber,
) async {
print(
'stopSendingPushNotificationsFor($deviceId, $passTypeId, $serialNumber)',
);
return true;
}
}
```
78 changes: 78 additions & 0 deletions passkit_server/example/passkit_server_example.dart
Original file line number Diff line number Diff line change
@@ -1 +1,79 @@
import 'dart:async';

import 'package:passkit/src/pkpass/pkpass.dart';
import 'package:passkit_server/passkit_server.dart';
import 'package:shelf/shelf.dart';
import 'package:shelf/shelf_io.dart' as io;
import 'package:shelf_router/shelf_router.dart';

Future<void> main() async {
var app = Router();

app.addPassKitServer(CustomPassKitBackend());

app.get('/hello', (Request request) {
return Response.ok('hello-world');
});

var server = await io.serve(app.call, 'localhost', 8888);
}

class CustomPassKitBackend extends PassKitBackend {
@override
Future<PkPass?> getLatestPassFor(String identifier, String serial) async {
print('getLatestPassFor($identifier, $serial)');
return null;
}

@override
FutureOr<bool> isKnownDeviceId(String deviceId) {
print('isKnownDeviceId($deviceId)');
return true;
}

@override
FutureOr<bool> isValidAuthToken(String serial, String authToken) {
print('isValidAuthToken($serial, $authToken)');
return true;
}

@override
Future<void> logMessage(Map<String, dynamic> message) async {
print('logMessage($message)');
}

@override
Future<UpdatablePassResponse?> returnUpdatablePasses(
String deviceId,
String typeId,
String? lastTag,
) async {
print('returnUpdatablePasses($deviceId, $typeId, $lastTag)');
return null;
}

@override
Future<NotificationRegistrationReponse> startSendingPushNotificationsFor(
String deviceId,
String passTypeId,
String serialNumber,
String pushToken,
) async {
print(
'startSendingPushNotificationsFor($deviceId, $passTypeId, $serialNumber, $pushToken)',
);
return NotificationRegistrationReponse.created;
}

@override
Future<bool> stopSendingPushNotificationsFor(
String deviceId,
String passTypeId,
String serialNumber,
) async {
print(
'stopSendingPushNotificationsFor($deviceId, $passTypeId, $serialNumber)',
);
return true;
}
}
2 changes: 2 additions & 0 deletions passkit_server/lib/src/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@ final _utf8JsonEncoder = JsonUtf8Encoder();

Uint8List utf8JsonEncode(Object data) =>
Uint8List.fromList(_utf8JsonEncoder.convert(data));

const kdebugMode = bool.fromEnvironment('dart.vm.product');

0 comments on commit 477eb6d

Please sign in to comment.