Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Unreleased

- [added] `WebpushFcmOptions` added to the `WebpushConfig` class.
- [added] Implemented the `GetUserByEmailAsync()` and `GetUserByPhoneNumberAsync()`
APIs in the `FirebaseAuth` class.

Expand Down
92 changes: 92 additions & 0 deletions FirebaseAdmin/FirebaseAdmin.Tests/Messaging/MessageTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1558,6 +1558,98 @@ public void ApnsInvalidCriticalSoundVolumeTooHigh()
Assert.Throws<ArgumentException>(() => message.CopyAndValidate());
}

[Fact]
public void WebpushNotificationWithLinkUrl()
{
var message = new Message()
{
Topic = "test-topic",
Webpush = new WebpushConfig()
{
Notification = new WebpushNotification()
{
Title = "title",
Body = "body",
Icon = "icon",
},
FcmOptions = new WebpushFcmOptions()
{
Link = "https://www.firebase.io/",
},
},
};
var expected = new JObject()
{
{ "topic", "test-topic" },
{
"webpush", new JObject()
{
{
"notification", new JObject()
{
{ "title", "title" },
{ "body", "body" },
{ "icon", "icon" },
}
},
{
"fcm_options", new JObject()
{
{ "link", "https://www.firebase.io/" },
}
},
}
},
};
this.AssertJsonEquals(expected, message);
}

[Fact]
public void WebpushNotificationWithInvalidHttpLinkUrl()
{
var message = new Message()
{
Topic = "test-topic",
Webpush = new WebpushConfig()
{
Notification = new WebpushNotification()
{
Title = "title",
Body = "body",
Icon = "icon",
},
FcmOptions = new WebpushFcmOptions()
{
Link = "http://www.firebase.io/",
},
},
};
Assert.Throws<ArgumentException>(() => message.CopyAndValidate());
}

[Fact]
public void WebpushNotificationWithInvalidHttpsLinkUrl()
{
var message = new Message()
{
Topic = "test-topic",
Webpush = new WebpushConfig()
{
Notification = new WebpushNotification()
{
Title = "title",
Body = "body",
Icon = "icon",
},
FcmOptions = new WebpushFcmOptions()
{
Link = "https whatever",
},
},
};
Assert.Throws<ArgumentException>(() => message.CopyAndValidate());
}

private void AssertJsonEquals(JObject expected, Message actual)
{
var json = NewtonsoftJsonSerializer.Instance.Serialize(actual.CopyAndValidate());
Expand Down
7 changes: 7 additions & 0 deletions FirebaseAdmin/FirebaseAdmin/Messaging/WebpushConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ public sealed class WebpushConfig
[JsonProperty("notification")]
public WebpushNotification Notification { get; set; }

/// <summary>
/// Gets or sets the Webpush options that will be included in the message.
/// </summary>
[JsonProperty("fcm_options")]
public WebpushFcmOptions FcmOptions { get; set; }

/// <summary>
/// Copies this Webpush config, and validates the content of it to ensure that it can be
/// serialized into the JSON format expected by the FCM service.
Expand All @@ -55,6 +61,7 @@ internal WebpushConfig CopyAndValidate()
Headers = this.Headers?.Copy(),
Data = this.Data?.Copy(),
Notification = this.Notification?.CopyAndValidate(),
FcmOptions = this.FcmOptions?.CopyAndValidate(),
};
}
}
Expand Down
54 changes: 54 additions & 0 deletions FirebaseAdmin/FirebaseAdmin/Messaging/WebpushFcmOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Copyright 2019, Google Inc. All rights reserved.
//
// 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;
using Newtonsoft.Json;

namespace FirebaseAdmin.Messaging
{
/// <summary>
/// Represents the Webpush-specific notification options that can be included in a <see cref="Message"/>.
/// See <see href="https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages#WebpushFcmOptions">this link</see>.
/// </summary>
public sealed class WebpushFcmOptions
{
/// <summary>
/// Gets or sets the link to open when the user clicks on the notification. For all URL values, HTTPS is required.
/// </summary>
[JsonProperty("link")]
public string Link { get; set; }

/// <summary>
/// Copies this Webpush FCM options, and validates the content of it to ensure that it can
/// be serialized into the JSON format expected by the FCM service.
/// </summary>
internal WebpushFcmOptions CopyAndValidate()
{
var copy = new WebpushFcmOptions()
{
Link = this.Link,
};

if (copy.Link != null)
{
if (!Uri.IsWellFormedUriString(copy.Link, UriKind.Absolute) || !copy.Link.StartsWith("https"))
{
throw new ArgumentException("The link options should be a valid https url.");
}
}

return copy;
}
}
}