Skip to content

Commit 9cde916

Browse files
authored
Rewrite Messaging Swig logic to prefer handwritten public files (#1068)
* Refactor the Messaging swig logic * Update readme, and random cleanup * Update readme.md
1 parent a6199ee commit 9cde916

8 files changed

+544
-512
lines changed

docs/readme.md

+5
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,11 @@ Support
7171

7272
Release Notes
7373
-------------
74+
### Upcoming release
75+
- Changes
76+
- Messaging: Deprecated the Dispose functions, as they are no longer
77+
necessary for cleaning up memory.
78+
7479
### 12.1.0
7580
- Changes
7681
- General: Update to Firebase C++ SDK version 12.1.0.

messaging/CMakeLists.txt

+4
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,11 @@ set(firebase_messaging_swig
2323

2424
# Firebase Messaging CSharp files
2525
set(firebase_messaging_src
26+
src/FirebaseMessage.cs
27+
src/FirebaseMessaging.cs
28+
src/FirebaseNotification.cs
2629
src/MessagingEventArgs.cs
30+
src/MessagingOptions.cs
2731
)
2832

2933
firebase_swig_add_library(firebase_messaging_swig

messaging/src/FirebaseMessage.cs

+117
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
/*
2+
* Copyright 2024 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
using System.Collections.Generic;
18+
19+
namespace Firebase.Messaging {
20+
21+
/// @brief Data structure used to send messages to, and receive messages from,
22+
/// cloud messaging.
23+
public sealed class FirebaseMessage {
24+
25+
internal static FirebaseMessage FromInternal(FirebaseMessageInternal other) {
26+
if (other == null) return null;
27+
28+
FirebaseMessage message = new FirebaseMessage();
29+
message.CollapseKey = other.collapse_key;
30+
// Make a copy of the dictionary, to not rely on the C++ memory lasting around
31+
message.Data = new Dictionary<string, string>(other.data);
32+
message.Error = other.error;
33+
message.ErrorDescription = other.error_description;
34+
message.From = other.from;
35+
message.Link = Firebase.FirebaseApp.UrlStringToUri(other.link);
36+
message.MessageId = other.message_id;
37+
message.MessageType = other.message_type;
38+
message.Notification = FirebaseNotification.FromInternal(other.notification);
39+
message.NotificationOpened = other.notification_opened;
40+
message.Priority = other.priority;
41+
// Make a copy of the array, to not rely on the C++ memory lasting around
42+
message.RawData = new byte[other.raw_data.Count];
43+
other.raw_data.CopyTo(message.RawData);
44+
message.TimeToLive = System.TimeSpan.FromSeconds(other.time_to_live);
45+
message.To = other.to;
46+
return message;
47+
}
48+
49+
/// Gets the collapse key used for collapsible messages.
50+
public string CollapseKey { get; private set; }
51+
52+
/// Gets or sets the metadata, including all original key/value pairs.
53+
/// Includes some of the HTTP headers used when sending the message. `gcm`,
54+
/// `google` and `goog` prefixes are reserved for internal use.
55+
public System.Collections.Generic.IDictionary<string, string> Data { get; private set; }
56+
57+
/// Gets the error code. Used in "nack" messages for CCS, and in responses
58+
/// from the server.
59+
/// See the CCS specification for the externally-supported list.
60+
public string Error { get; private set; }
61+
62+
/// Gets the human readable details about the error.
63+
public string ErrorDescription { get; private set; }
64+
65+
/// Gets the authenticated ID of the sender. This is a project number in most cases.
66+
public string From { get; private set; }
67+
68+
/// The link into the app from the message.
69+
public System.Uri Link { get; private set; }
70+
71+
/// Gets or sets the message ID. This can be specified by sender. Internally a
72+
/// hash of the message ID and other elements will be used for storage. The ID
73+
/// must be unique for each topic subscription - using the same ID may result
74+
/// in overriding the original message or duplicate delivery.
75+
public string MessageId { get; private set; }
76+
77+
/// Gets the message type, equivalent with a content-type.
78+
/// CCS uses "ack", "nack" for flow control and error handling.
79+
/// "control" is used by CCS for connection control.
80+
public string MessageType { get; private set; }
81+
82+
/// Optional notification to show. This only set if a notification was
83+
/// received with this message, otherwise it is null.
84+
public FirebaseNotification Notification { get; private set; }
85+
86+
/// Gets a flag indicating whether this message was opened by tapping a
87+
/// notification in the OS system tray. If the message was received this way
88+
/// this flag is set to true.
89+
public bool NotificationOpened { get; private set; }
90+
91+
/// Gets the priority level. Defined values are "normal" and "high".
92+
/// By default messages are sent with normal priority.
93+
public string Priority { get; private set; }
94+
95+
/// Gets the binary payload. For webpush and non-json messages, this is the
96+
/// body of the request entity.
97+
public byte[] RawData { get; private set; }
98+
99+
/// The Time To Live (TTL) for the message.
100+
public System.TimeSpan TimeToLive { get; private set; }
101+
102+
/// Gets or sets recipient of a message.
103+
///
104+
/// For example it can be a registration token, a topic name, a IID or project
105+
/// ID.
106+
public string To { get; private set; }
107+
108+
/// @deprecated No longer needed, will be removed in the future.
109+
[System.Obsolete("No longer needed, will be removed in the future.")]
110+
public void Dispose() { }
111+
112+
/// @deprecated No longer needed, will be removed in the future.
113+
[System.Obsolete("No longer needed, will be removed in the future.")]
114+
public void Dispose(bool disposing) { }
115+
}
116+
117+
} // namespace Firebase.Messaging

messaging/src/FirebaseMessaging.cs

+186
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
/*
2+
* Copyright 2024 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
namespace Firebase.Messaging {
18+
19+
/// @brief Firebase Cloud Messaging API.
20+
///
21+
/// Firebase Cloud Messaging allows you to send data from your server to your
22+
/// users' devices, and receive messages from devices on the same connection
23+
/// if you're using a XMPP server.
24+
///
25+
/// The FCM service handles all aspects of queueing of messages and delivery
26+
/// to client applications running on target devices.
27+
public static class FirebaseMessaging {
28+
29+
/// Enable or disable token registration during initialization of Firebase
30+
/// Cloud Messaging.
31+
///
32+
/// This token is what identifies the user to Firebase, so disabling this
33+
/// avoids creating any new identity and automatically sending it to Firebase,
34+
/// unless consent has been granted.
35+
///
36+
/// If this setting is enabled, it triggers the token registration refresh
37+
/// immediately. This setting is persisted across app restarts and overrides
38+
/// the setting "firebase_messaging_auto_init_enabled" specified in your
39+
/// Android manifest (on Android) or Info.plist (on iOS and tvOS).
40+
///
41+
/// <p>By default, token registration during initialization is enabled.
42+
///
43+
/// The registration happens before you can programmatically disable it, so
44+
/// if you need to change the default, (for example, because you want to
45+
/// prompt the user before FCM generates/refreshes a registration token on app
46+
/// startup), add to your application’s manifest:
47+
///
48+
/// @if NOT_DOXYGEN
49+
/// <meta-data android:name="firebase_messaging_auto_init_enabled"
50+
/// android:value="false" />
51+
/// @else
52+
/// @code
53+
/// &lt;meta-data android:name="firebase_messaging_auto_init_enabled"
54+
/// android:value="false" /&gt;
55+
/// @endcode
56+
/// @endif
57+
///
58+
/// or on iOS or tvOS to your Info.plist:
59+
///
60+
/// @if NOT_DOXYGEN
61+
/// <key>FirebaseMessagingAutoInitEnabled</key>
62+
/// <false/>
63+
/// @else
64+
/// @code
65+
/// &lt;key&gt;FirebaseMessagingAutoInitEnabled&lt;/key&gt;
66+
/// &lt;false/&gt;
67+
/// @endcode
68+
/// @endif
69+
public static bool TokenRegistrationOnInitEnabled {
70+
get {
71+
return FirebaseMessagingInternal.IsTokenRegistrationOnInitEnabled();
72+
}
73+
set {
74+
FirebaseMessagingInternal.SetTokenRegistrationOnInitEnabled(value);
75+
}
76+
}
77+
78+
/// Enables or disables Firebase Cloud Messaging message delivery metrics
79+
/// export to BigQuery.
80+
///
81+
/// By default, message delivery metrics are not exported to BigQuery. Use
82+
/// this method to enable or disable the export at runtime. In addition, you
83+
/// can enable the export by adding to your manifest. Note that the run-time
84+
/// method call will override the manifest value.
85+
///
86+
/// @code
87+
/// <meta-data android:name= "delivery_metrics_exported_to_big_query_enabled"
88+
/// android:value="true"/>
89+
/// @endcode
90+
///
91+
/// @note This function is currently only implemented on Android, and has no
92+
/// behavior on other platforms.
93+
public static bool DeliveryMetricsExportedToBigQueryEnabled {
94+
get {
95+
return FirebaseMessagingInternal.DeliveryMetricsExportToBigQueryEnabled();
96+
}
97+
set {
98+
FirebaseMessagingInternal.SetDeliveryMetricsExportToBigQuery(value);
99+
}
100+
}
101+
102+
/// @brief This creates a Firebase Installations ID, if one does not exist, and
103+
/// sends information about the application and the device where it's running to
104+
/// the Firebase backend.
105+
///
106+
/// @return A task with the token.
107+
public static System.Threading.Tasks.Task<string> GetTokenAsync() {
108+
return FirebaseMessagingInternal.GetTokenAsync();
109+
}
110+
111+
/// @brief Deletes the default token for this Firebase project.
112+
///
113+
/// Note that this does not delete the Firebase Installations ID that may have
114+
/// been created when generating the token. See Installations.Delete() for
115+
/// deleting that.
116+
///
117+
/// @return A task that completes when the token is deleted.
118+
public static System.Threading.Tasks.Task DeleteTokenAsync() {
119+
return FirebaseMessagingInternal.DeleteTokenAsync();
120+
}
121+
122+
#if DOXYGEN
123+
/// Called on the client when a message arrives.
124+
public static event System.EventHandler<MessageReceivedEventArgs> MessageReceived;
125+
#else
126+
public static event System.EventHandler<MessageReceivedEventArgs> MessageReceived {
127+
add {
128+
FirebaseMessagingInternal.MessageReceived += value;
129+
}
130+
remove {
131+
FirebaseMessagingInternal.MessageReceived -= value;
132+
}
133+
}
134+
#endif // DOXYGEN
135+
136+
#if DOXYGEN
137+
/// Called on the client when a registration token message arrives.
138+
public static event System.EventHandler<TokenReceivedEventArgs> TokenReceived;
139+
#else
140+
public static event System.EventHandler<TokenReceivedEventArgs> TokenReceived {
141+
add {
142+
FirebaseMessagingInternal.TokenReceived += value;
143+
}
144+
remove {
145+
FirebaseMessagingInternal.TokenReceived -= value;
146+
}
147+
}
148+
#endif
149+
150+
/// @brief Displays a prompt to the user requesting permission to display
151+
/// notifications.
152+
///
153+
/// The permission prompt only appears on iOS and tvOS. If the user has
154+
/// already agreed to allow notifications, no prompt is displayed and the
155+
/// returned future is completed immediately.
156+
///
157+
/// @return A Task that completes when the notification prompt has been
158+
/// dismissed.
159+
public static System.Threading.Tasks.Task RequestPermissionAsync() {
160+
return FirebaseMessagingInternal.RequestPermissionAsync();
161+
}
162+
163+
/// @brief Subscribe to receive all messages to the specified topic.
164+
///
165+
/// Subscribes an app instance to a topic, enabling it to receive messages
166+
/// sent to that topic.
167+
///
168+
/// @param[in] topic The name of the topic to subscribe. Must match the
169+
/// following regular expression: `[a-zA-Z0-9-_.~%]{1,900}`.
170+
public static System.Threading.Tasks.Task SubscribeAsync(string topic) {
171+
return FirebaseMessagingInternal.SubscribeAsync(topic);
172+
}
173+
174+
/// @brief Unsubscribe from a topic.
175+
///
176+
/// Unsubscribes an app instance from a topic, stopping it from receiving
177+
/// any further messages sent to that topic.
178+
///
179+
/// @param[in] topic The name of the topic to unsubscribe from. Must match the
180+
/// following regular expression: `[a-zA-Z0-9-_.~%]{1,900}`.
181+
public static System.Threading.Tasks.Task UnsubscribeAsync(string topic) {
182+
return FirebaseMessagingInternal.UnsubscribeAsync(topic);
183+
}
184+
}
185+
186+
} // namespace Firebase.Messaging

0 commit comments

Comments
 (0)