Skip to content

Commit

Permalink
Rewrite Messaging Swig logic to prefer handwritten public files (#1068)
Browse files Browse the repository at this point in the history
* Refactor the Messaging swig logic

* Update readme, and random cleanup

* Update readme.md
  • Loading branch information
a-maurice authored Jul 17, 2024
1 parent a6199ee commit 9cde916
Show file tree
Hide file tree
Showing 8 changed files with 544 additions and 512 deletions.
5 changes: 5 additions & 0 deletions docs/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ Support

Release Notes
-------------
### Upcoming release
- Changes
- Messaging: Deprecated the Dispose functions, as they are no longer
necessary for cleaning up memory.

### 12.1.0
- Changes
- General: Update to Firebase C++ SDK version 12.1.0.
Expand Down
4 changes: 4 additions & 0 deletions messaging/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ set(firebase_messaging_swig

# Firebase Messaging CSharp files
set(firebase_messaging_src
src/FirebaseMessage.cs
src/FirebaseMessaging.cs
src/FirebaseNotification.cs
src/MessagingEventArgs.cs
src/MessagingOptions.cs
)

firebase_swig_add_library(firebase_messaging_swig
Expand Down
117 changes: 117 additions & 0 deletions messaging/src/FirebaseMessage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/*
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

using System.Collections.Generic;

namespace Firebase.Messaging {

/// @brief Data structure used to send messages to, and receive messages from,
/// cloud messaging.
public sealed class FirebaseMessage {

internal static FirebaseMessage FromInternal(FirebaseMessageInternal other) {
if (other == null) return null;

FirebaseMessage message = new FirebaseMessage();
message.CollapseKey = other.collapse_key;
// Make a copy of the dictionary, to not rely on the C++ memory lasting around
message.Data = new Dictionary<string, string>(other.data);
message.Error = other.error;
message.ErrorDescription = other.error_description;
message.From = other.from;
message.Link = Firebase.FirebaseApp.UrlStringToUri(other.link);
message.MessageId = other.message_id;
message.MessageType = other.message_type;
message.Notification = FirebaseNotification.FromInternal(other.notification);
message.NotificationOpened = other.notification_opened;
message.Priority = other.priority;
// Make a copy of the array, to not rely on the C++ memory lasting around
message.RawData = new byte[other.raw_data.Count];
other.raw_data.CopyTo(message.RawData);
message.TimeToLive = System.TimeSpan.FromSeconds(other.time_to_live);
message.To = other.to;
return message;
}

/// Gets the collapse key used for collapsible messages.
public string CollapseKey { get; private set; }

/// Gets or sets the metadata, including all original key/value pairs.
/// Includes some of the HTTP headers used when sending the message. `gcm`,
/// `google` and `goog` prefixes are reserved for internal use.
public System.Collections.Generic.IDictionary<string, string> Data { get; private set; }

/// Gets the error code. Used in "nack" messages for CCS, and in responses
/// from the server.
/// See the CCS specification for the externally-supported list.
public string Error { get; private set; }

/// Gets the human readable details about the error.
public string ErrorDescription { get; private set; }

/// Gets the authenticated ID of the sender. This is a project number in most cases.
public string From { get; private set; }

/// The link into the app from the message.
public System.Uri Link { get; private set; }

/// Gets or sets the message ID. This can be specified by sender. Internally a
/// hash of the message ID and other elements will be used for storage. The ID
/// must be unique for each topic subscription - using the same ID may result
/// in overriding the original message or duplicate delivery.
public string MessageId { get; private set; }

/// Gets the message type, equivalent with a content-type.
/// CCS uses "ack", "nack" for flow control and error handling.
/// "control" is used by CCS for connection control.
public string MessageType { get; private set; }

/// Optional notification to show. This only set if a notification was
/// received with this message, otherwise it is null.
public FirebaseNotification Notification { get; private set; }

/// Gets a flag indicating whether this message was opened by tapping a
/// notification in the OS system tray. If the message was received this way
/// this flag is set to true.
public bool NotificationOpened { get; private set; }

/// Gets the priority level. Defined values are "normal" and "high".
/// By default messages are sent with normal priority.
public string Priority { get; private set; }

/// Gets the binary payload. For webpush and non-json messages, this is the
/// body of the request entity.
public byte[] RawData { get; private set; }

/// The Time To Live (TTL) for the message.
public System.TimeSpan TimeToLive { get; private set; }

/// Gets or sets recipient of a message.
///
/// For example it can be a registration token, a topic name, a IID or project
/// ID.
public string To { get; private set; }

/// @deprecated No longer needed, will be removed in the future.
[System.Obsolete("No longer needed, will be removed in the future.")]
public void Dispose() { }

/// @deprecated No longer needed, will be removed in the future.
[System.Obsolete("No longer needed, will be removed in the future.")]
public void Dispose(bool disposing) { }
}

} // namespace Firebase.Messaging
186 changes: 186 additions & 0 deletions messaging/src/FirebaseMessaging.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
/*
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace Firebase.Messaging {

/// @brief Firebase Cloud Messaging API.
///
/// Firebase Cloud Messaging allows you to send data from your server to your
/// users' devices, and receive messages from devices on the same connection
/// if you're using a XMPP server.
///
/// The FCM service handles all aspects of queueing of messages and delivery
/// to client applications running on target devices.
public static class FirebaseMessaging {

/// Enable or disable token registration during initialization of Firebase
/// Cloud Messaging.
///
/// This token is what identifies the user to Firebase, so disabling this
/// avoids creating any new identity and automatically sending it to Firebase,
/// unless consent has been granted.
///
/// If this setting is enabled, it triggers the token registration refresh
/// immediately. This setting is persisted across app restarts and overrides
/// the setting "firebase_messaging_auto_init_enabled" specified in your
/// Android manifest (on Android) or Info.plist (on iOS and tvOS).
///
/// <p>By default, token registration during initialization is enabled.
///
/// The registration happens before you can programmatically disable it, so
/// if you need to change the default, (for example, because you want to
/// prompt the user before FCM generates/refreshes a registration token on app
/// startup), add to your application’s manifest:
///
/// @if NOT_DOXYGEN
/// <meta-data android:name="firebase_messaging_auto_init_enabled"
/// android:value="false" />
/// @else
/// @code
/// &lt;meta-data android:name="firebase_messaging_auto_init_enabled"
/// android:value="false" /&gt;
/// @endcode
/// @endif
///
/// or on iOS or tvOS to your Info.plist:
///
/// @if NOT_DOXYGEN
/// <key>FirebaseMessagingAutoInitEnabled</key>
/// <false/>
/// @else
/// @code
/// &lt;key&gt;FirebaseMessagingAutoInitEnabled&lt;/key&gt;
/// &lt;false/&gt;
/// @endcode
/// @endif
public static bool TokenRegistrationOnInitEnabled {
get {
return FirebaseMessagingInternal.IsTokenRegistrationOnInitEnabled();
}
set {
FirebaseMessagingInternal.SetTokenRegistrationOnInitEnabled(value);
}
}

/// Enables or disables Firebase Cloud Messaging message delivery metrics
/// export to BigQuery.
///
/// By default, message delivery metrics are not exported to BigQuery. Use
/// this method to enable or disable the export at runtime. In addition, you
/// can enable the export by adding to your manifest. Note that the run-time
/// method call will override the manifest value.
///
/// @code
/// <meta-data android:name= "delivery_metrics_exported_to_big_query_enabled"
/// android:value="true"/>
/// @endcode
///
/// @note This function is currently only implemented on Android, and has no
/// behavior on other platforms.
public static bool DeliveryMetricsExportedToBigQueryEnabled {
get {
return FirebaseMessagingInternal.DeliveryMetricsExportToBigQueryEnabled();
}
set {
FirebaseMessagingInternal.SetDeliveryMetricsExportToBigQuery(value);
}
}

/// @brief This creates a Firebase Installations ID, if one does not exist, and
/// sends information about the application and the device where it's running to
/// the Firebase backend.
///
/// @return A task with the token.
public static System.Threading.Tasks.Task<string> GetTokenAsync() {
return FirebaseMessagingInternal.GetTokenAsync();
}

/// @brief Deletes the default token for this Firebase project.
///
/// Note that this does not delete the Firebase Installations ID that may have
/// been created when generating the token. See Installations.Delete() for
/// deleting that.
///
/// @return A task that completes when the token is deleted.
public static System.Threading.Tasks.Task DeleteTokenAsync() {
return FirebaseMessagingInternal.DeleteTokenAsync();
}

#if DOXYGEN
/// Called on the client when a message arrives.
public static event System.EventHandler<MessageReceivedEventArgs> MessageReceived;
#else
public static event System.EventHandler<MessageReceivedEventArgs> MessageReceived {
add {
FirebaseMessagingInternal.MessageReceived += value;
}
remove {
FirebaseMessagingInternal.MessageReceived -= value;
}
}
#endif // DOXYGEN

#if DOXYGEN
/// Called on the client when a registration token message arrives.
public static event System.EventHandler<TokenReceivedEventArgs> TokenReceived;
#else
public static event System.EventHandler<TokenReceivedEventArgs> TokenReceived {
add {
FirebaseMessagingInternal.TokenReceived += value;
}
remove {
FirebaseMessagingInternal.TokenReceived -= value;
}
}
#endif

/// @brief Displays a prompt to the user requesting permission to display
/// notifications.
///
/// The permission prompt only appears on iOS and tvOS. If the user has
/// already agreed to allow notifications, no prompt is displayed and the
/// returned future is completed immediately.
///
/// @return A Task that completes when the notification prompt has been
/// dismissed.
public static System.Threading.Tasks.Task RequestPermissionAsync() {
return FirebaseMessagingInternal.RequestPermissionAsync();
}

/// @brief Subscribe to receive all messages to the specified topic.
///
/// Subscribes an app instance to a topic, enabling it to receive messages
/// sent to that topic.
///
/// @param[in] topic The name of the topic to subscribe. Must match the
/// following regular expression: `[a-zA-Z0-9-_.~%]{1,900}`.
public static System.Threading.Tasks.Task SubscribeAsync(string topic) {
return FirebaseMessagingInternal.SubscribeAsync(topic);
}

/// @brief Unsubscribe from a topic.
///
/// Unsubscribes an app instance from a topic, stopping it from receiving
/// any further messages sent to that topic.
///
/// @param[in] topic The name of the topic to unsubscribe from. Must match the
/// following regular expression: `[a-zA-Z0-9-_.~%]{1,900}`.
public static System.Threading.Tasks.Task UnsubscribeAsync(string topic) {
return FirebaseMessagingInternal.UnsubscribeAsync(topic);
}
}

} // namespace Firebase.Messaging
Loading

0 comments on commit 9cde916

Please sign in to comment.