Skip to content

Commit

Permalink
Merge branch 'master' into v3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
aritchie committed Apr 8, 2024
2 parents dd4d52d + 4e65f26 commit 8b731eb
Show file tree
Hide file tree
Showing 9 changed files with 137 additions and 76 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
using Android.Bluetooth;

namespace Shiny.BluetoothLE;
namespace Shiny.BluetoothLE;


public record AndroidConnectionConfig(
bool AutoConnect = true,
GattConnectionPriority ConnectionPriority = GattConnectionPriority.Balanced
Android.Bluetooth.GattConnectionPriority ConnectionPriority = Android.Bluetooth.GattConnectionPriority.Balanced
) : ConnectionConfig(
AutoConnect
);
9 changes: 9 additions & 0 deletions src/Shiny.Core/Platforms/Android/AndroidPlatform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public AndroidPlatform()
this.requestedPermissions = this.store.Get<List<string>>(PermissionsKey) ?? new List<string>();
}


public AccessState GetCurrentPermissionStatus(string androidPermission)
{
var self = ContextCompat.CheckSelfPermission(this.AppContext, androidPermission);
Expand Down Expand Up @@ -164,6 +165,14 @@ public void StopService(Type serviceType)
// return result == Permission.Granted ? AccessState.Available : AccessState.Denied;
//}

public int GetDrawableByName(string name) => this
.AppContext
.Resources!
.GetIdentifier(
name,
"drawable",
this.AppContext.PackageName
);

public IObservable<AccessState> RequestAccess(string androidPermissions)
=> this.RequestPermissions(new[] { androidPermissions }).Select(x => x.IsSuccess() ? AccessState.Available : AccessState.Denied);
Expand Down
2 changes: 1 addition & 1 deletion src/Shiny.Logging.Sqlite/Shiny.Logging.Sqlite.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="$(MicrosoftExtensionsVersion)" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="$(MicrosoftExtensionsVersion)" />
<PackageReference Include="sqlite-net-pcl" Version="1.9.141-beta" />
<PackageReference Include="sqlite-net-pcl" Version="1.9.172" />
</ItemGroup>

</Project>
15 changes: 8 additions & 7 deletions src/Shiny.Push.FirebaseMessaging/FirebaseConfiguration.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using System;
#if ANDROID
using Android.App;
#endif

namespace Shiny.Push;

Expand All @@ -13,16 +16,14 @@ public record FirebaseConfiguration(
string? SenderId = null,
string? ProjectId = null,
string? ApiKey = null

#if ANDROID
, NotificationChannel? DefaultChannel = null
, string? IntentAction = null
#endif
)
{
public static FirebaseConfiguration Embedded { get; } = new(true);
public static FirebaseConfiguration FromValues(string apiId, string senderId, string projectId, string apiKey)
{
var cfg = new FirebaseConfiguration(false, apiId, senderId, projectId, apiKey);
cfg.AssertValid();
return cfg;
}


public void AssertValid()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,14 @@ public static IServiceCollection AddPushFirebaseMessaging(this IServiceCollectio
}
else
{
services.AddPush(FirebaseConfig.FromValues(
services.AddPush(new FirebaseConfig(
false,
config.AppId,
config.SenderId,
config.ProjectId,
config.ApiKey
config.ApiKey,
config.DefaultChannel,
config.IntentAction
));
}
#endif
Expand Down
97 changes: 97 additions & 0 deletions src/Shiny.Push/Platforms/Android/AndroidPushNotification.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
using Android.App;
using Android.Content;
using AndroidX.Core.App;
using Firebase.Messaging;

namespace Shiny.Push;


public record AndroidPushNotification(
Notification? Notification,
RemoteMessage NativeMessage,
FirebaseConfig Config,
AndroidPlatform Platform
) : PushNotification(NativeMessage.Data, Notification)
{

public NotificationCompat.Builder CreateBuilder()
{
var not = this.NativeMessage.GetNotification();
var channelId = not.ChannelId ?? this.Config.DefaultChannel?.Id;
var builder = new NotificationCompat.Builder(this.Platform.AppContext, channelId!)!;

if (not != null)
{
var intent = new Intent(not.ClickAction ?? ShinyPushIntents.NotificationClickAction);
foreach (var item in this.NativeMessage.Data)
intent.PutExtra(item.Key, item.Value);

var pendingIntent = PendingIntent.GetActivity(
this.Platform.AppContext,
99,
intent,
PendingIntentFlags.OneShot | PendingIntentFlags.Immutable
);

builder
.SetContentTitle(not.Title)
.SetContentIntent(pendingIntent);

if (!not.Body.IsEmpty())
builder.SetContentText(not.Body);

if (!not.Ticker.IsEmpty())
builder.SetTicker(not.Ticker);

if (not.Icon.IsEmpty())
{
var id = this.Platform.GetDrawableByName("notification");
if (id > 0)
{
builder.SetSmallIcon(id);
}
else if (this.Platform.AppContext.ApplicationInfo!.Icon > 0)
{
builder.SetSmallIcon(this.Platform.AppContext.ApplicationInfo!.Icon);
}
}
else
{
var drawableId = this.Platform.GetDrawableByName(not.Icon);
builder.SetSmallIcon(drawableId);
}

if (!not.Color.IsEmpty())
{
var colorId = this.Platform
.AppContext
.Resources!
.GetIdentifier(
not.Color,
"color",
this.Platform.AppContext.PackageName
);

builder.SetColor(colorId);
}
}
return builder;
}


public void Notify(int notificationId, NotificationCompat.Builder builder)
{
using var service = this.Platform.GetSystemService<NotificationManager>(Context.NotificationService);
service.Notify(notificationId, builder.Build());
}


/// <summary>
/// This will create the default builder and send it
/// </summary>
public void SendDefault(int notificationId)
{
var builder = this.CreateBuilder();
this.Notify(notificationId, builder);
}
}
71 changes: 11 additions & 60 deletions src/Shiny.Push/Platforms/Android/PushManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ void TryCreateConfiguredChannel()
if (this.config.DefaultChannel == null)
return;

var nativeManager = this.platform.GetSystemService<NotificationManager>(Context.NotificationService);
using var nativeManager = this.platform.GetSystemService<NotificationManager>(Context.NotificationService);
var channel = nativeManager.GetNotificationChannel(this.config.DefaultChannel.Id);
if (channel != null)
nativeManager.DeleteNotificationChannel(channel.Id);
Expand Down Expand Up @@ -174,11 +174,12 @@ await this.services
}


public void ActivityOnCreate(Activity activity, Bundle? savedInstanceState) => this.Handle(activity, activity.Intent);
public void ActivityOnCreate(Activity activity, Bundle? savedInstanceState)
=> this.Handle(activity, activity.Intent);

public void Handle(Activity activity, Intent intent)
public void Handle(Activity activity, Intent? intent)
{
var intentAction = this.config.IntentAction; //?? ShinyPushIntents.NotificationClickAction;
var intentAction = this.config.IntentAction ?? ShinyPushIntents.NotificationClickAction;
var clickAction = intent?.Action?.Equals(intentAction, StringComparison.InvariantCultureIgnoreCase) ?? false;
if (!clickAction)
return;
Expand Down Expand Up @@ -270,15 +271,17 @@ await this.services
if (native != null)
{
//native.ChannelId
//native.ImageUrl
notification = new Notification(
native.Title,
native.Body
);
//this.TryTriggerNotification(msg);
}
var push = new PushNotification(msg.Data, notification);
var push = new AndroidPushNotification(
notification,
msg,
this.config,
this.platform
);
await this.services
.RunDelegates<IPushDelegate>(
x => x.OnReceived(push),
Expand Down Expand Up @@ -311,56 +314,4 @@ bool IsFirebaseAappAlreadyInitialized()

return isAppInitialized;
}


// protected virtual void TryTriggerNotification(RemoteMessage message)
// {
// try
// {
// var notification = message.GetNotification();
// var intent = new Intent(notification.ClickAction ?? ShinyIntents.NotificationClickAction);

// if (message.Data != null)
// {
// foreach (var data in message.Data)
// intent.PutExtra(data.Key, data.Value);
// }

// var pendingIntent = PendingIntent.GetActivity(this.platform.AppContext, 99, intent, PendingIntentFlags.Mutable);

// var builder = new NotificationCompat
// .Builder(
// this.platform.AppContext,
// notification.ChannelId ?? Channel.Default.Identifier
// )
// .SetAutoCancel(true)
// .SetSilent(false)
// .SetSmallIcon(this.platform.GetSmallIconResource(notification.Icon))
// .SetContentIntent(pendingIntent)
// .SetContentTitle(notification.Title);

// if (!notification.Ticker.IsEmpty())
// builder.SetTicker(notification.Ticker);

// if (!notification.Body.IsEmpty())
// builder.SetContentText(notification.Body);

// ///this.platform.TrySetImage(notification.ImageUrl, builder);

// if (!notification.Color.IsEmpty())
// {
// var color = this.platform.GetColorResourceId(notification.Color);
// builder.SetColor(color);
// }

// var notificationId = this.settings.IncrementValue("NotificationId");
// this.platform
// .GetSystemService<NotificationManager>(Context.NotificationService)
// .Notify(notificationId, builder.Build());
// }
// catch (Exception ex)
// {
// this.logger.LogError("Error processing foreground remote notification", ex);
// }
// }
}
4 changes: 3 additions & 1 deletion src/Shiny.Push/Platforms/Apple/PushManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,9 @@ public void OnWillPresentNotification(UNNotification notification, Action<UNNoti
this.platform.InvokeOnMainThread(() =>
completionHandler.Invoke(
options ?? UNNotificationPresentationOptions.List | UNNotificationPresentationOptions.Banner
options ??
UNNotificationPresentationOptions.List |
UNNotificationPresentationOptions.Banner
)
);
}
Expand Down
2 changes: 1 addition & 1 deletion version.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"$schema": "https://raw.githubusercontent.com/dotnet/Nerdbank.GitVersioning/master/src/NerdBank.GitVersioning/version.schema.json",
"version": "3.3.2",
"version": "3.3.3",
"assemblyVersion": {
"precision": "revision"
},
Expand Down

0 comments on commit 8b731eb

Please sign in to comment.