This repository has been archived by the owner on Jul 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 168
/
AndroidPushService.cs
executable file
·208 lines (174 loc) · 6.93 KB
/
AndroidPushService.cs
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using System.Threading.Tasks;
using Android.Support.V4.App;
using Android.Support.V4.Content;
using WindowsAzure.Messaging;
using Shopping.DemoApp.Droid.Activities;
using Gcm;
namespace Shopping.DemoApp.Droid.Services
{
public static class AndroidPushService
{
private const int VibrateTimeInMillis = 300;
private const string PushTitle = "Shopping Demo App";
private static TaskCompletionSource<string> registrationTaskCompletionSource;
private static TaskCompletionSource<string> unRegistrationTaskCompletionSource;
private static Context initContext;
private static bool gmsAvailabilable;
private static NotificationHub hub;
public static void Initialize(Context context)
{
initContext = context;
gmsAvailabilable = CheckAvailability();
//hub = new NotificationHub(AppSettings.NotificationHubPath, AppSettings.NotificationHubConnectionString, context);
}
public static async Task Register(string[] tags)
{
if (gmsAvailabilable)
{
var registrationId = await RegisterInternal();
try
{
await Task.Run(() => hub.Register(registrationId, tags));
Console.WriteLine($"Registered to channel with registrationId {registrationId}");
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}
}
public static async Task UnRegister(string[] tags)
{
if (gmsAvailabilable)
{
var registrationId = await UnRegisterInternal();
try
{
await Task.Run(() => hub.UnregisterAll(registrationId));
Console.WriteLine($"UnRegistered to channel with registrationId {registrationId}");
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}
}
public static void SetRegisterRegistrationId(string rRegistrationId)
{
registrationTaskCompletionSource?.TrySetResult(rRegistrationId);
Console.WriteLine($"Current rRegistrationId {rRegistrationId}");
}
public static void SetUnRegisterRegistrationId(string unrRegistrationId)
{
unRegistrationTaskCompletionSource?.TrySetResult(unrRegistrationId);
Console.WriteLine($"Current unrRegistrationId {unrRegistrationId}");
}
public static void ShowInAppToast(Context context, string msg)
{
Console.WriteLine($"Toast received: {msg}");
var toastMsg = msg;
if (!string.IsNullOrEmpty(toastMsg))
{
Toast.MakeText(context, toastMsg, ToastLength.Long).Show();
var vibrator = (Vibrator)context.GetSystemService(Context.VibratorService);
vibrator.Vibrate(VibrateTimeInMillis);
}
}
public static void ShowLocalPush(Context context, string msg)
{
Console.WriteLine($"Toast received: {msg}");
var pushMsg = msg;
if (!string.IsNullOrEmpty(pushMsg))
{
var pendingIntent = GeneratePendingIntent(context, msg);
var currentMillis = Java.Lang.JavaSystem.CurrentTimeMillis();
var builder = new NotificationCompat.Builder(context)
.SetContentIntent(pendingIntent)
.SetContentTitle(PushTitle)
.SetContentText(pushMsg)
.SetAutoCancel(true)
.SetStyle(new NotificationCompat.BigTextStyle().BigText(pushMsg))
.SetDefaults((int)(NotificationDefaults.Sound | NotificationDefaults.Vibrate))
.SetWhen(currentMillis)
.SetSmallIcon(GetNotificationIcon());
builder = SetNotificationColor(context, builder);
var notification = builder.Build();
var notificationManager = (NotificationManager)context.GetSystemService(Context.NotificationService);
notificationManager.Notify((int)currentMillis, notification);
}
}
private static PendingIntent GeneratePendingIntent(Context context, string msg)
{
var notificationIntent = new Intent(context, typeof(MainActivity));
notificationIntent.SetFlags(ActivityFlags.SingleTop);
var pendingIntent = PendingIntent.GetActivity(context, 0, notificationIntent, PendingIntentFlags.UpdateCurrent);
return pendingIntent;
}
private static Task<string> RegisterInternal()
{
registrationTaskCompletionSource = new TaskCompletionSource<string>();
try
{
GcmClient.Register(initContext, GcmBroadcastReceiver.SENDER_IDS);
}
catch (Exception ex)
{
Console.WriteLine(ex);
registrationTaskCompletionSource.TrySetResult(string.Empty);
}
return registrationTaskCompletionSource.Task;
}
private static Task<string> UnRegisterInternal()
{
unRegistrationTaskCompletionSource = new TaskCompletionSource<string>();
try
{
GcmClient.UnRegister(initContext);
}
catch (Exception ex)
{
Console.WriteLine(ex);
unRegistrationTaskCompletionSource.TrySetResult(string.Empty);
}
return unRegistrationTaskCompletionSource.Task;
}
private static bool CheckAvailability()
{
try
{
GcmClient.CheckDevice(initContext);
GcmClient.CheckManifest(initContext);
return true;
}
catch (Exception ex)
{
Console.WriteLine(ex);
return false;
}
}
private static NotificationCompat.Builder SetNotificationColor(Context context, NotificationCompat.Builder builder)
{
if (global::Android.OS.Build.VERSION.SdkInt >= global::Android.OS.BuildVersionCodes.Lollipop)
{
var blueDocks = ContextCompat.GetColor(context, Resource.Color.blue);
builder = builder.SetColor(blueDocks);
}
return builder;
}
private static int GetNotificationIcon()
{
var useWhiteIcon = global::Android.OS.Build.VERSION.SdkInt >= global::Android.OS.BuildVersionCodes.Lollipop;
return useWhiteIcon ? Resource.Drawable.Icon : Resource.Drawable.Icon;
}
}
}