OneSignal / OneSignal-DotNet-SDK

OneSignal is a free push notification service for mobile apps. This plugin makes it easy to integrate your .NET app with OneSignal. https://onesignal.com
MIT License
14 stars 4 forks source link

WIP notification service extension service in dotnet #111

Open tmijieux opened 1 week ago

tmijieux commented 1 week ago

Description

One Line Summary

add possibilities to use the notification service extension from the dotnet world.

Details

this is based on the work i already done in the "old" xamarin framework https://github.com/OneSignal/OneSignal-Xamarin-SDK/pull/381

the final usage would be very similar to the one i described in this message: https://github.com/OneSignal/OneSignal-Xamarin-SDK/pull/381#issuecomment-1841065887 YourApp/Platforms/Android/MyOneSignalServiceExtension.cs :

using AndroidX.Core.App;
using Com.OneSignal.Android;
using Com.OneSignal.Android.Notifications;
using Com.OneSignal.Notifications.Android;
using Org.Json;

namespace YourApp.Platforms.Android
{
    public class MyExtender : NotificationExtenderBase
    {
        private INotification Notification { get; set; }
        public MyExtender(INotification notif)
        {
            Notification = notif;
        }

        public override NotificationCompat.Builder Extend(NotificationCompat.Builder builder)
        {
            //do some logic with Notification to customize the notif,
            return builder
                //.SetContentTitle("coucou1")
                //.SetContentText("hello2")
                /*.AddAction(new NotificationCompat.Action(null, "coucou", null))*/
                ;
        }
    }

    /* this attribute declare a name in java world for the native onesignal lib 
    to be able to call our c# code. Change it with a name fitting for your app!
    Also, in your AndroidManifest.xml add the following tag 
    with name matching the one you choose under the application tag:
        <meta-data android:name="com.onesignal.NotificationServiceExtension" 
                            android:value="com.your.app.MyOneSignalServiceExtension" />
     */
    [NotificationExtension(Name = "com.your.app.MyOneSignalServiceExtension")]
    public class MyOneSignalServiceExtension : NotificationServiceExtenderBase
    {
        public bool SomeLogicOn(IDisplayableMutableNotification notif, JSONObject additionalData)
        {
            /* implement your logic here if appropriate for your app */
            return false;
        }

        public override void OnNotificationReceived(INotificationReceivedEvent ev)
        {
            var notif = ev.Notification;
            notif.SetExtender(new MyExtender(notif));

            /* example of logic usage to choose to hide the notif or not: */
            bool hideNotif = SomeLogicOn(notif, notif.AdditionalData);

            if (hideNotif)
            {
                ev.PreventDefault();
            }
            else
            {
                notif.Display();
            }
        }
    }
}

I did not yet test this usage because i still have dependencies issues, but it used to work in a similar way on the xamarin version.

the main point of this pull-request are:

This dependency is the main reason this MR is still WIP. After adding this dependency, there are lots of conflicts in the final app, stuff about some classes being defined multiple times , some others classes are missing at runtime etc ... If someone is willing to bring help on this point this would be very welcome. In the end, I had to add a lot of dependency just for it to build and i think this is probably too much there must be a dependency/version combination that is optimal and should work in most case but i did not find it yet.

Motivation

fixes #92 this feature is already available in the android sdk but not working yet in dotnet sdk

Scope

notification service extension

Manual testing

RECOMMEND - OPTIONAL - Explain what scenarios were tested and the environment. Example: Tested opening a notification while the app was foregrounded, app build with Android Studio 2020.3 with a fresh install of the OneSignal example app on a Pixel 6 with Android 12.

Affected code checklist

Checklist

Overview

Testing

Final pass