Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
aritchie committed Apr 8, 2024
2 parents 659b846 + bf02b88 commit 854b47b
Show file tree
Hide file tree
Showing 7 changed files with 136 additions and 73 deletions.
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
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
95 changes: 95 additions & 0 deletions src/Shiny.Push/Platforms/Android/AndroidPushNotification.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
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)
{
// TODO: config OR click_action
var intent = new Intent(not.ClickAction ?? ShinyPushIntents.NotificationClickAction);
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);
}
}
77 changes: 15 additions & 62 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 @@ -250,10 +251,12 @@ void DoInit()
if (this.NativeRegistrationToken == token || this.registrationRequest)
return;
var regToken = await this.provider.Register(token);
this.NativeRegistrationToken = token;
this.RegistrationToken = await this.provider.Register(token);
await this.services
.RunDelegates<IPushDelegate>(
x => x.OnNewToken(regToken),
x => x.OnNewToken(this.RegistrationToken),
this.logger
)
.ConfigureAwait(false);
Expand All @@ -268,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 @@ -309,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.1-beta.{height}",
"version": "3.3.3-beta.{height}",
"assemblyVersion": {
"precision": "revision"
},
Expand Down

0 comments on commit 854b47b

Please sign in to comment.