Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
C
cordova-plugin-local-notifications
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Nabil Sadki
cordova-plugin-local-notifications
Commits
b01a1375
Commit
b01a1375
authored
Aug 11, 2017
by
Sebastián Katzer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Code refacturing
parent
15bb160f
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
326 additions
and
251 deletions
+326
-251
Builder.cs
...Proxy/LocalNotificationProxy/LocalNotification/Builder.cs
+54
-17
Content.cs
...Proxy/LocalNotificationProxy/LocalNotification/Content.cs
+243
-0
Options.cs
...Proxy/LocalNotificationProxy/LocalNotification/Options.cs
+28
-234
LocalNotificationProxy.csproj
...roxy/LocalNotificationProxy/LocalNotificationProxy.csproj
+1
-0
No files found.
src/windows/LocalNotificationProxy/LocalNotificationProxy/LocalNotification/Builder.cs
View file @
b01a1375
...
...
@@ -27,23 +27,23 @@ namespace LocalNotificationProxy.LocalNotification
internal
class
Builder
{
/// <summary>
/// Notification properties
/// </summary>
private
Options
options
;
/// <summary>
/// Initializes a new instance of the <see cref="Builder"/> class.
/// </summary>
/// <param name="options">Notification properties to set.</param>
internal
Builder
(
Options
options
)
{
this
.
options
=
options
;
this
.
Content
=
new
Content
(
options
)
;
}
/// <summary>
/// Gets content
/// </summary>
public
Content
Content
{
get
;
private
set
;
}
/// <summary>
/// Gets options
/// </summary>
p
ublic
Options
Options
{
get
=>
this
.
o
ptions
;
}
p
rivate
Options
Options
{
get
=>
this
.
Content
.
O
ptions
;
}
/// <summary>
/// Build a toast notification specified by the options.
...
...
@@ -51,10 +51,24 @@ namespace LocalNotificationProxy.LocalNotification
/// <returns>A fully configured toast notification instance.</returns>
public
ScheduledToastNotification
Build
()
{
var
toast
=
new
ToastContent
()
var
toast
=
this
.
InitToast
();
this
.
AddAttachments
(
toast
);
this
.
AddActions
(
toast
);
return
this
.
GetNotification
(
toast
);
}
/// <summary>
/// Gets the initialize skeleton for a toast notification.
/// </summary>
/// <returns>Basic skeleton with sound, image and text.</returns>
private
ToastContent
InitToast
()
{
return
new
ToastContent
()
{
Launch
=
this
.
Options
.
Identifier
,
Audio
=
this
.
Options
.
ToastAudio
,
Launch
=
this
.
Content
.
GetXml
()
,
Audio
=
this
.
Content
.
Sound
,
Visual
=
new
ToastVisual
()
{
...
...
@@ -73,33 +87,56 @@ namespace LocalNotificationProxy.LocalNotification
}
},
AppLogoOverride
=
this
.
Options
.
ToastLogo
AppLogoOverride
=
this
.
Content
.
Image
}
},
Actions
=
new
ToastActionsCustom
()
{
Buttons
=
{
}
Buttons
=
{
},
Inputs
=
{
}
}
};
}
foreach
(
var
image
in
this
.
Options
.
ImageAttachments
)
/// <summary>
/// Adds attachments to the toast.
/// </summary>
/// <param name="toast">Tho toast to extend for.</param>
private
void
AddAttachments
(
ToastContent
toast
)
{
foreach
(
var
image
in
this
.
Content
.
Attachments
)
{
toast
.
Visual
.
BindingGeneric
.
Children
.
Add
(
image
);
}
}
foreach
(
var
btn
in
this
.
Options
.
ToastButtons
)
/// <summary>
/// Adds buttons and input fields to the toast.
/// </summary>
/// <param name="toast">Tho toast to extend for.</param>
private
void
AddActions
(
ToastContent
toast
)
{
foreach
(
var
btn
in
this
.
Content
.
Buttons
)
{
(
toast
.
Actions
as
ToastActionsCustom
).
Buttons
.
Add
(
btn
);
}
}
/// <summary>
/// Converts the toast into a notification.
/// </summary>
/// <param name="toast">The toast to convert.</param>
/// <returns>A notification ready to schedule.</returns>
private
ScheduledToastNotification
GetNotification
(
ToastContent
toast
)
{
var
xml
=
toast
.
GetXml
();
var
at
=
this
.
Options
.
Trigger
Date
;
var
at
=
this
.
Content
.
Date
;
ScheduledToastNotification
notification
;
if
(
this
.
Options
.
IsRepeating
())
if
(
this
.
Content
.
IsRepeating
())
{
var
interval
=
this
.
Options
.
Repeat
Interval
;
var
interval
=
this
.
Content
.
Interval
;
notification
=
new
ScheduledToastNotification
(
xml
,
at
,
interval
,
5
);
}
else
...
...
src/windows/LocalNotificationProxy/LocalNotificationProxy/LocalNotification/Content.cs
0 → 100644
View file @
b01a1375
namespace
LocalNotificationProxy.LocalNotification
{
using
System
;
using
System.Collections.Generic
;
using
Microsoft.Toolkit.Uwp.Notifications
;
using
Windows.Data.Xml.Dom
;
internal
class
Content
{
/// <summary>
/// Initializes a new instance of the <see cref="Content"/> class.
/// </summary>
/// <param name="options">The options hash map from JS side.</param>
public
Content
(
Options
options
)
{
this
.
Options
=
options
;
}
/// <summary>
/// Initializes a new instance of the <see cref="Content"/> class.
/// </summary>
/// <param name="xml">The options as a xml string.</param>
public
Content
(
string
xml
)
{
this
.
Options
=
Options
.
Parse
(
xml
);
}
public
Options
Options
{
get
;
private
set
;
}
/// <summary>
/// Gets a ToastAudio object based on the specified sound uri.
/// </summary>
public
ToastAudio
Sound
{
get
{
var
sound
=
new
ToastAudio
();
var
path
=
this
.
Options
.
Sound
;
if
(
path
==
null
||
path
.
Length
==
0
||
path
.
Equals
(
"false"
))
{
sound
.
Silent
=
true
;
}
else
if
(
path
.
StartsWith
(
"file:///"
)
||
path
.
StartsWith
(
"http"
))
{
sound
.
Src
=
new
Uri
(
path
,
UriKind
.
Absolute
);
}
else
if
(
path
.
StartsWith
(
"file://"
))
{
sound
.
Src
=
new
Uri
(
path
.
Replace
(
"file:/"
,
"ms-appx:///www"
));
}
else
if
(
path
.
StartsWith
(
"res://"
))
{
sound
.
Src
=
new
Uri
(
path
.
Replace
(
"res://"
,
"ms-winsoundevent:notification."
));
}
else
if
(
path
.
StartsWith
(
"app://"
))
{
sound
.
Src
=
new
Uri
(
path
.
Replace
(
"app:/"
,
"ms-appdata://"
));
}
return
sound
;
}
}
/// <summary>
/// Gets a GenericAppLogo object based on the specified icon uri.
/// </summary>
public
ToastGenericAppLogo
Image
{
get
{
var
image
=
new
ToastGenericAppLogo
();
var
path
=
this
.
Options
.
Image
;
if
(
path
==
null
||
path
.
StartsWith
(
"res://logo"
))
{
image
.
Source
=
string
.
Empty
;
}
else
if
(
path
.
StartsWith
(
"file:///"
)
||
path
.
StartsWith
(
"http"
))
{
image
.
Source
=
path
;
}
else
if
(
path
.
StartsWith
(
"file://"
))
{
image
.
Source
=
path
.
Replace
(
"file:/"
,
"ms-appx:///www"
);
}
else
if
(
path
.
StartsWith
(
"res://"
))
{
image
.
Source
=
path
.
Replace
(
"res://"
,
"ms-appx:///images"
);
}
else
if
(
path
.
StartsWith
(
"app://"
))
{
image
.
Source
=
path
.
Replace
(
"app:/"
,
"ms-appdata://local"
);
}
else
{
image
.
Source
=
string
.
Empty
;
}
if
(
image
.
Source
.
EndsWith
(
"?crop=none"
))
{
image
.
HintCrop
=
ToastGenericAppLogoCrop
.
None
;
}
else
if
(
image
.
Source
.
EndsWith
(
"?crop=cirlce"
))
{
image
.
HintCrop
=
ToastGenericAppLogoCrop
.
Circle
;
}
return
image
;
}
}
/// <summary>
/// Gets the parsed image attachments.
/// </summary>
public
List
<
AdaptiveImage
>
Attachments
{
get
{
var
images
=
new
List
<
AdaptiveImage
>();
if
(
this
.
Options
.
Attachments
==
null
)
{
return
images
;
}
foreach
(
string
path
in
this
.
Options
.
Attachments
)
{
var
image
=
new
AdaptiveImage
();
if
(
path
.
StartsWith
(
"file:///"
)
||
path
.
StartsWith
(
"http"
))
{
image
.
Source
=
path
;
}
else
if
(
path
.
StartsWith
(
"file://"
))
{
image
.
Source
=
path
.
Replace
(
"file:/"
,
"ms-appx:///www"
);
}
else
if
(
path
.
StartsWith
(
"res://"
))
{
image
.
Source
=
path
.
Replace
(
"res://"
,
"ms-appx:///images"
);
}
else
if
(
path
.
StartsWith
(
"app://"
))
{
image
.
Source
=
path
.
Replace
(
"app:/"
,
"ms-appdata://local"
);
}
if
(
image
.
Source
!=
null
)
{
images
.
Add
(
image
);
}
}
return
images
;
}
}
/// <summary>
/// Gets all toast buttons.
/// </summary>
public
List
<
ToastButton
>
Buttons
{
get
{
var
buttons
=
new
List
<
ToastButton
>();
foreach
(
var
action
in
this
.
Options
.
Buttons
)
{
buttons
.
Add
(
action
.
ToastButton
);
}
return
buttons
;
}
}
/// <summary>
/// Gets the date when to trigger the notification.
/// </summary>
public
DateTime
Date
{
get
{
var
date
=
DateTimeOffset
.
FromUnixTimeMilliseconds
(
this
.
Options
.
At
*
1000
).
LocalDateTime
;
var
minDate
=
DateTime
.
Now
.
AddSeconds
(
0.1
);
return
(
date
<
minDate
)
?
minDate
:
date
;
}
}
/// <summary>
/// Gets the parsed repeat interval.
/// </summary>
public
TimeSpan
Interval
{
get
{
switch
(
this
.
Options
.
Every
)
{
case
"minute"
:
return
new
TimeSpan
(
TimeSpan
.
TicksPerMinute
);
case
"hour"
:
return
new
TimeSpan
(
TimeSpan
.
TicksPerHour
);
case
"day"
:
return
new
TimeSpan
(
TimeSpan
.
TicksPerDay
);
default
:
return
TimeSpan
.
Zero
;
}
}
}
/// <summary>
/// Gets the instance as an serialized xml element.
/// </summary>
/// <returns>Element with all property values set as attributes.</returns>
public
string
GetXml
()
{
return
this
.
Options
.
GetXml
();
}
/// <summary>
/// If the notification shall be repeating.
/// </summary>
/// <returns>True if the Every property has some value.</returns>
public
bool
IsRepeating
()
{
var
every
=
this
.
Options
.
Every
;
return
every
!=
null
&&
every
.
Length
>
0
&&
!
every
.
Equals
(
"0"
);
}
}
}
src/windows/LocalNotificationProxy/LocalNotificationProxy/LocalNotification/Options.cs
View file @
b01a1375
...
...
@@ -84,139 +84,60 @@ namespace LocalNotificationProxy.LocalNotification
public
Button
[]
Buttons
{
get
;
set
;
}
/// <summary>
///
Gets the date when to trigger the notification
.
///
Deserializes the XML string into an instance of Options
.
/// </summary>
internal
DateTime
TriggerDate
{
get
/// <param name="identifier">The serialized instance of Options as an xml string.</param>
/// <returns>An instance where all properties have been assigned.</returns>
public
static
Options
Parse
(
string
identifier
)
{
var
date
=
DateTimeOffset
.
FromUnixTimeMilliseconds
(
this
.
At
*
1000
).
LocalDateTime
;
var
minDate
=
DateTime
.
Now
.
AddSeconds
(
0.1
);
return
(
date
<
minDate
)
?
minDate
:
date
;
}
}
var
doc
=
new
XmlDocument
();
doc
.
LoadXml
(
identifier
);
/// <summary>
/// Gets the parsed repeat interval.
/// </summary>
internal
TimeSpan
RepeatInterval
{
get
{
switch
(
this
.
Every
)
{
case
"minute"
:
return
new
TimeSpan
(
TimeSpan
.
TicksPerMinute
);
case
"hour"
:
return
new
TimeSpan
(
TimeSpan
.
TicksPerHour
);
case
"day"
:
return
new
TimeSpan
(
TimeSpan
.
TicksPerDay
);
default
:
return
TimeSpan
.
Zero
;
}
}
}
var
options
=
new
Options
();
var
node
=
doc
.
DocumentElement
;
/// <summary>
/// Gets a ToastAudio object based on the specified sound uri.
/// </summary>
internal
ToastAudio
ToastAudio
{
get
{
var
sound
=
new
ToastAudio
();
string
path
=
this
.
Sound
;
options
.
ID
=
int
.
Parse
(
node
.
GetAttribute
(
"id"
));
options
.
Badge
=
int
.
Parse
(
node
.
GetAttribute
(
"badge"
));
options
.
At
=
int
.
Parse
(
node
.
GetAttribute
(
"at"
));
if
(
path
==
null
||
path
.
Length
==
0
||
path
.
Equals
(
"false"
))
{
sound
.
Silent
=
true
;
}
else
if
(
path
.
StartsWith
(
"file:///"
)
||
path
.
StartsWith
(
"http"
))
{
sound
.
Src
=
new
System
.
Uri
(
path
,
System
.
UriKind
.
Absolute
);
}
else
if
(
path
.
StartsWith
(
"file://"
))
{
sound
.
Src
=
new
System
.
Uri
(
path
.
Replace
(
"file:/"
,
"ms-appx:///www"
));
}
else
if
(
path
.
StartsWith
(
"res://"
))
{
sound
.
Src
=
new
System
.
Uri
(
path
.
Replace
(
"res://"
,
"ms-winsoundevent:notification."
));
}
else
if
(
path
.
StartsWith
(
"app://"
))
if
(
node
.
GetAttributeNode
(
"text"
)
!=
null
)
{
sound
.
Src
=
new
System
.
Uri
(
path
.
Replace
(
"app:/"
,
"ms-appdata://"
));
}
return
sound
;
}
options
.
Text
=
node
.
GetAttribute
(
"text"
);
}
/// <summary>
/// Gets a GenericAppLogo object based on the specified icon uri.
/// </summary>
internal
ToastGenericAppLogo
ToastLogo
{
get
{
var
image
=
new
ToastGenericAppLogo
();
string
path
=
this
.
Image
;
if
(
path
==
null
||
path
.
StartsWith
(
"res://logo"
))
{
image
.
Source
=
string
.
Empty
;
}
else
if
(
path
.
StartsWith
(
"file:///"
)
||
path
.
StartsWith
(
"http"
))
{
image
.
Source
=
path
;
}
else
if
(
path
.
StartsWith
(
"file://"
))
{
image
.
Source
=
path
.
Replace
(
"file:/"
,
"ms-appx:///www"
);
}
else
if
(
path
.
StartsWith
(
"res://"
))
if
(
node
.
GetAttributeNode
(
"title"
)
!=
null
)
{
image
.
Source
=
path
.
Replace
(
"res://"
,
"ms-appx:///images
"
);
options
.
Title
=
node
.
GetAttribute
(
"title
"
);
}
else
if
(
path
.
StartsWith
(
"app://"
)
)
if
(
node
.
GetAttributeNode
(
"sound"
)
!=
null
)
{
image
.
Source
=
path
.
Replace
(
"app:/"
,
"ms-appdata://local
"
);
options
.
Sound
=
node
.
GetAttribute
(
"sound
"
);
}
else
if
(
node
.
GetAttributeNode
(
"image"
)
!=
null
)
{
image
.
Source
=
string
.
Empty
;
options
.
Image
=
node
.
GetAttribute
(
"image"
)
;
}
if
(
image
.
Source
.
EndsWith
(
"?crop=none"
)
)
if
(
node
.
GetAttributeNode
(
"every"
)
!=
null
)
{
image
.
HintCrop
=
ToastGenericAppLogoCrop
.
None
;
options
.
Every
=
node
.
GetAttribute
(
"every"
)
;
}
else
if
(
image
.
Source
.
EndsWith
(
"?crop=cirlce"
)
)
if
(
node
.
GetAttributeNode
(
"data"
)
!=
null
)
{
image
.
HintCrop
=
ToastGenericAppLogoCrop
.
Circle
;
options
.
Data
=
node
.
GetAttribute
(
"data"
)
;
}
return
image
;
}
return
options
;
}
/// <summary>
/// Gets the instance as an serialized xml element.
/// </summary>
/// <returns>Element with all property values set as attributes.</returns>
internal
string
Identifier
{
get
public
string
GetXml
()
{
var
node
=
new
XmlDocument
().
CreateElement
(
"options"
);
...
...
@@ -257,130 +178,4 @@ namespace LocalNotificationProxy.LocalNotification
return
node
.
GetXml
();
}
}
/// <summary>
/// Gets the parsed image attachments.
/// </summary>
internal
List
<
AdaptiveImage
>
ImageAttachments
{
get
{
var
images
=
new
List
<
AdaptiveImage
>();
if
(
this
.
Attachments
==
null
)
{
return
images
;
}
foreach
(
string
path
in
this
.
Attachments
)
{
var
image
=
new
AdaptiveImage
();
if
(
path
.
StartsWith
(
"file:///"
)
||
path
.
StartsWith
(
"http"
))
{
image
.
Source
=
path
;
}
else
if
(
path
.
StartsWith
(
"file://"
))
{
image
.
Source
=
path
.
Replace
(
"file:/"
,
"ms-appx:///www"
);
}
else
if
(
path
.
StartsWith
(
"res://"
))
{
image
.
Source
=
path
.
Replace
(
"res://"
,
"ms-appx:///images"
);
}
else
if
(
path
.
StartsWith
(
"app://"
))
{
image
.
Source
=
path
.
Replace
(
"app:/"
,
"ms-appdata://local"
);
}
if
(
image
.
Source
!=
null
)
{
images
.
Add
(
image
);
}
}
return
images
;
}
}
/// <summary>
/// Gets all toast buttons.
/// </summary>
internal
List
<
ToastButton
>
ToastButtons
{
get
{
var
buttons
=
new
List
<
ToastButton
>();
foreach
(
var
action
in
this
.
Buttons
)
{
buttons
.
Add
(
action
.
ToastButton
);
}
return
buttons
;
}
}
/// <summary>
/// Deserializes the XML string into an instance of Options.
/// </summary>
/// <param name="identifier">The serialized instance of Options as an xml string.</param>
/// <returns>An instance where all properties have been assigned.</returns>
public
static
Options
Parse
(
string
identifier
)
{
var
doc
=
new
XmlDocument
();
doc
.
LoadXml
(
identifier
);
var
options
=
new
Options
();
var
node
=
doc
.
DocumentElement
;
options
.
ID
=
int
.
Parse
(
node
.
GetAttribute
(
"id"
));
options
.
Badge
=
int
.
Parse
(
node
.
GetAttribute
(
"badge"
));
options
.
At
=
int
.
Parse
(
node
.
GetAttribute
(
"at"
));
if
(
node
.
GetAttributeNode
(
"text"
)
!=
null
)
{
options
.
Text
=
node
.
GetAttribute
(
"text"
);
}
if
(
node
.
GetAttributeNode
(
"title"
)
!=
null
)
{
options
.
Title
=
node
.
GetAttribute
(
"title"
);
}
if
(
node
.
GetAttributeNode
(
"sound"
)
!=
null
)
{
options
.
Sound
=
node
.
GetAttribute
(
"sound"
);
}
if
(
node
.
GetAttributeNode
(
"image"
)
!=
null
)
{
options
.
Image
=
node
.
GetAttribute
(
"image"
);
}
if
(
node
.
GetAttributeNode
(
"every"
)
!=
null
)
{
options
.
Every
=
node
.
GetAttribute
(
"every"
);
}
if
(
node
.
GetAttributeNode
(
"data"
)
!=
null
)
{
options
.
Data
=
node
.
GetAttribute
(
"data"
);
}
return
options
;
}
/// <summary>
/// If the notification shall be repeating.
/// </summary>
/// <returns>True if the Every property has some value.</returns>
internal
bool
IsRepeating
()
{
return
this
.
Every
!=
null
&&
this
.
Every
.
Length
>
0
&&
!
this
.
Every
.
Equals
(
"0"
);
}
}
}
\ No newline at end of file
src/windows/LocalNotificationProxy/LocalNotificationProxy/LocalNotificationProxy.csproj
View file @
b01a1375
...
...
@@ -113,6 +113,7 @@
<Compile Include="LocalNotificationProxy.cs" />
<Compile Include="LocalNotification\Builder.cs" />
<Compile Include="LocalNotification\Button.cs" />
<Compile Include="LocalNotification\Content.cs" />
<Compile Include="LocalNotification\Options.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment