-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathPNMessagingService.android.cs
More file actions
94 lines (76 loc) · 3.75 KB
/
PNMessagingService.android.cs
File metadata and controls
94 lines (76 loc) · 3.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
using System.Collections.Generic;
using System.Linq;
using Android.App;
using Firebase.Messaging;
namespace Plugin.AzurePushNotification
{
[Service(Exported = false)]
[IntentFilter(new[] { "com.google.firebase.MESSAGING_EVENT" })]
// ReSharper disable once InconsistentNaming
public class PNMessagingService : FirebaseMessagingService
{
public override void OnMessageReceived(RemoteMessage message)
{
var parameters = new Dictionary<string, object>();
var notification = message.GetNotification();
if (notification != null)
{
if (!string.IsNullOrEmpty(notification.Body))
parameters.Add("body", notification.Body);
if (!string.IsNullOrEmpty(notification.BodyLocalizationKey))
parameters.Add("body_loc_key", notification.BodyLocalizationKey);
var bodyLocArgs = notification.GetBodyLocalizationArgs();
if (bodyLocArgs != null && bodyLocArgs.Any())
parameters.Add("body_loc_args", bodyLocArgs);
if (!string.IsNullOrEmpty(notification.Title))
parameters.Add("title", notification.Title);
if (!string.IsNullOrEmpty(notification.TitleLocalizationKey))
parameters.Add("title_loc_key", notification.TitleLocalizationKey);
var titleLocArgs = notification.GetTitleLocalizationArgs();
if (titleLocArgs != null && titleLocArgs.Any())
parameters.Add("title_loc_args", titleLocArgs);
if (!string.IsNullOrEmpty(notification.Tag))
parameters.Add("tag", notification.Tag);
if (!string.IsNullOrEmpty(notification.Sound))
parameters.Add("sound", notification.Sound);
if (!string.IsNullOrEmpty(notification.Icon))
parameters.Add("icon", notification.Icon);
if (notification.Link != null)
parameters.Add("link_path", notification.Link.Path);
if (!string.IsNullOrEmpty(notification.ClickAction))
parameters.Add("click_action", notification.ClickAction);
if (!string.IsNullOrEmpty(notification.Color))
parameters.Add("color", notification.Color);
}
foreach (var d in message.Data)
{
if (!parameters.ContainsKey(d.Key))
parameters.Add(d.Key, d.Value);
}
//Fix localization arguments parsing
string[] localizationKeys=new string[]{ "title_loc_args", "body_loc_args"};
foreach(var locKey in localizationKeys)
{
if (parameters.ContainsKey(locKey) && parameters[locKey] is string parameterValue)
{
if (parameterValue.StartsWith("[") && parameterValue.EndsWith("]") && parameterValue.Length > 2)
{
var arrayValues = parameterValue.Substring(1, parameterValue.Length - 2);
parameters[locKey] = arrayValues.Split(',').Select(t => t.Trim()).ToArray();
}
else
{
parameters[locKey] = new string[] { };
}
}
}
AzurePushNotificationManager.RegisterData(parameters);
CrossAzurePushNotification.Current.NotificationHandler?.OnReceived(parameters);
}
public override void OnNewToken(string p0)
{
AzurePushNotificationManager.RegisterToken(p0);
System.Diagnostics.Debug.WriteLine($"REFRESHED TOKEN: {p0}");
}
}
}