Skip to content

Commit b614222

Browse files
committed
Merged with master
2 parents 7b9b21c + b54e7ea commit b614222

File tree

4 files changed

+154
-0
lines changed

4 files changed

+154
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Unreleased
22

33
- [added] Implemented the `CreateUserAsync()` and `UserRecordArgs` APIs.
4+
- [added] `WebpushFcmOptions` added to the `WebpushConfig` class.
45
- [added] Implemented the `GetUserByEmailAsync()` and `GetUserByPhoneNumberAsync()`
56
APIs in the `FirebaseAuth` class.
67

FirebaseAdmin/FirebaseAdmin.Tests/Messaging/MessageTest.cs

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1558,6 +1558,98 @@ public void ApnsInvalidCriticalSoundVolumeTooHigh()
15581558
Assert.Throws<ArgumentException>(() => message.CopyAndValidate());
15591559
}
15601560

1561+
[Fact]
1562+
public void WebpushNotificationWithLinkUrl()
1563+
{
1564+
var message = new Message()
1565+
{
1566+
Topic = "test-topic",
1567+
Webpush = new WebpushConfig()
1568+
{
1569+
Notification = new WebpushNotification()
1570+
{
1571+
Title = "title",
1572+
Body = "body",
1573+
Icon = "icon",
1574+
},
1575+
FcmOptions = new WebpushFcmOptions()
1576+
{
1577+
Link = "https://www.firebase.io/",
1578+
},
1579+
},
1580+
};
1581+
var expected = new JObject()
1582+
{
1583+
{ "topic", "test-topic" },
1584+
{
1585+
"webpush", new JObject()
1586+
{
1587+
{
1588+
"notification", new JObject()
1589+
{
1590+
{ "title", "title" },
1591+
{ "body", "body" },
1592+
{ "icon", "icon" },
1593+
}
1594+
},
1595+
{
1596+
"fcm_options", new JObject()
1597+
{
1598+
{ "link", "https://www.firebase.io/" },
1599+
}
1600+
},
1601+
}
1602+
},
1603+
};
1604+
this.AssertJsonEquals(expected, message);
1605+
}
1606+
1607+
[Fact]
1608+
public void WebpushNotificationWithInvalidHttpLinkUrl()
1609+
{
1610+
var message = new Message()
1611+
{
1612+
Topic = "test-topic",
1613+
Webpush = new WebpushConfig()
1614+
{
1615+
Notification = new WebpushNotification()
1616+
{
1617+
Title = "title",
1618+
Body = "body",
1619+
Icon = "icon",
1620+
},
1621+
FcmOptions = new WebpushFcmOptions()
1622+
{
1623+
Link = "http://www.firebase.io/",
1624+
},
1625+
},
1626+
};
1627+
Assert.Throws<ArgumentException>(() => message.CopyAndValidate());
1628+
}
1629+
1630+
[Fact]
1631+
public void WebpushNotificationWithInvalidHttpsLinkUrl()
1632+
{
1633+
var message = new Message()
1634+
{
1635+
Topic = "test-topic",
1636+
Webpush = new WebpushConfig()
1637+
{
1638+
Notification = new WebpushNotification()
1639+
{
1640+
Title = "title",
1641+
Body = "body",
1642+
Icon = "icon",
1643+
},
1644+
FcmOptions = new WebpushFcmOptions()
1645+
{
1646+
Link = "https whatever",
1647+
},
1648+
},
1649+
};
1650+
Assert.Throws<ArgumentException>(() => message.CopyAndValidate());
1651+
}
1652+
15611653
private void AssertJsonEquals(JObject expected, Message actual)
15621654
{
15631655
var json = NewtonsoftJsonSerializer.Instance.Serialize(actual.CopyAndValidate());

FirebaseAdmin/FirebaseAdmin/Messaging/WebpushConfig.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ public sealed class WebpushConfig
4444
[JsonProperty("notification")]
4545
public WebpushNotification Notification { get; set; }
4646

47+
/// <summary>
48+
/// Gets or sets the Webpush options that will be included in the message.
49+
/// </summary>
50+
[JsonProperty("fcm_options")]
51+
public WebpushFcmOptions FcmOptions { get; set; }
52+
4753
/// <summary>
4854
/// Copies this Webpush config, and validates the content of it to ensure that it can be
4955
/// serialized into the JSON format expected by the FCM service.
@@ -55,6 +61,7 @@ internal WebpushConfig CopyAndValidate()
5561
Headers = this.Headers?.Copy(),
5662
Data = this.Data?.Copy(),
5763
Notification = this.Notification?.CopyAndValidate(),
64+
FcmOptions = this.FcmOptions?.CopyAndValidate(),
5865
};
5966
}
6067
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Copyright 2019, Google Inc. All rights reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
using System;
16+
using Newtonsoft.Json;
17+
18+
namespace FirebaseAdmin.Messaging
19+
{
20+
/// <summary>
21+
/// Represents the Webpush-specific notification options that can be included in a <see cref="Message"/>.
22+
/// See <see href="https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages#WebpushFcmOptions">this link</see>.
23+
/// </summary>
24+
public sealed class WebpushFcmOptions
25+
{
26+
/// <summary>
27+
/// Gets or sets the link to open when the user clicks on the notification. For all URL values, HTTPS is required.
28+
/// </summary>
29+
[JsonProperty("link")]
30+
public string Link { get; set; }
31+
32+
/// <summary>
33+
/// Copies this Webpush FCM options, and validates the content of it to ensure that it can
34+
/// be serialized into the JSON format expected by the FCM service.
35+
/// </summary>
36+
internal WebpushFcmOptions CopyAndValidate()
37+
{
38+
var copy = new WebpushFcmOptions()
39+
{
40+
Link = this.Link,
41+
};
42+
43+
if (copy.Link != null)
44+
{
45+
if (!Uri.IsWellFormedUriString(copy.Link, UriKind.Absolute) || !copy.Link.StartsWith("https"))
46+
{
47+
throw new ArgumentException("The link options should be a valid https url.");
48+
}
49+
}
50+
51+
return copy;
52+
}
53+
}
54+
}

0 commit comments

Comments
 (0)