Line data Source code
1 : import 'package:flutter/foundation.dart';
2 : import 'worker.dart';
3 :
4 : /// Sources for remote triggers.
5 : enum RemoteTriggerSource {
6 : /// Firebase Cloud Messaging (Android/iOS)
7 : fcm,
8 :
9 : /// Apple Push Notification service (iOS only)
10 : apns,
11 : }
12 :
13 : /// A rule for matching and executing a remote trigger.
14 : ///
15 : /// When a remote message arrives, the plugin looks for a field
16 : /// in the payload named [payloadKey]. If the value of that field matches
17 : /// a key in [workerMappings], the corresponding worker is enqueued.
18 : ///
19 : /// Use `{{key}}` syntax in worker parameters to substitute values from the
20 : /// remote payload.
21 : @immutable
22 : class RemoteTriggerRule {
23 : /// Create a rule for matching remote triggers.
24 1 : const RemoteTriggerRule({
25 : required this.payloadKey,
26 : required this.workerMappings,
27 : this.secretKey,
28 : });
29 :
30 : /// The key in the remote message payload to look for.
31 : ///
32 : /// For example, if your FCM data is `{"type": "download_update"}`,
33 : /// set [payloadKey] to `"type"`.
34 : final String payloadKey;
35 :
36 : /// Map of payload values to their corresponding worker templates.
37 : ///
38 : /// For example:
39 : /// ```dart
40 : /// workerMappings: {
41 : /// 'download_update': NativeWorker.httpDownload(
42 : /// url: '{{url}}', // Substitutes from payload['url']
43 : /// savePath: '{{path}}',
44 : /// ),
45 : /// }
46 : /// ```
47 : final Map<String, Worker> workerMappings;
48 :
49 : /// Optional secret key for HMAC SHA-256 signature verification.
50 : ///
51 : /// If provided, the plugin will verify that the remote payload contains
52 : /// a valid `x-native-wm-signature` header/key computed using this secret.
53 : /// This prevents malicious actors from spoofing pushes to drain battery.
54 : final String? secretKey;
55 :
56 : /// Convert to a map for the platform channel.
57 1 : Map<String, dynamic> toMap() {
58 1 : return {
59 2 : 'payloadKey': payloadKey,
60 6 : 'workerMappings': workerMappings.map((key, worker) => MapEntry(key, {
61 1 : 'workerClassName': worker.workerClassName,
62 1 : 'workerConfig': worker.toMap(),
63 : })),
64 1 : if (secretKey != null) 'secretKey': secretKey,
65 : };
66 : }
67 : }
|