serenity-is / Serenity

Business Apps Made Simple with Asp.Net Core MVC / TypeScript
https://serenity.is
MIT License
2.57k stars 796 forks source link

Notification System #2221

Closed shakazulu89 closed 7 years ago

shakazulu89 commented 7 years ago

Can anyone point me in the right direction to implement a real-time notification system? There is one wiki article on using signalR but i was wondering if anyone had luck with it or found better alternatives. https://github.com/volkanceylan/Serenity/wiki/Group-notifications-with-SignalR

abelal83 commented 7 years ago

Signalr from wiki works great. We implemented with some changes for our purposes following the wiki.

shakazulu89 commented 7 years ago

thanks for the reply abelal! did you create a facebook type notification system? i mean with icons on the top right showing the notifications/messages. if so can you point me in the right direction to accomplish that? :) I saw this link: https://github.com/volkanceylan/Serenity/wiki/Layout-Improvements-%5BNotification-Icons-&-User-Personalization%5D but im not sure how to begin coupling that with signalr and have them work together :/ any help appreciated!

abelal83 commented 7 years ago

I created toastr notification, it was designed to send real time mesages out to users. What you're trying to accomplish should be possible with signalr, you'll need to dynamically display icons and this can be achieved using jquery. I'm afraid I don't have examples to give without actually writing something which will be time consuming.

wh1337 commented 7 years ago

@shakazulu89 I have implemented OneSignal and it's really easy to implement. I can send you the source that I have for it

VR-Architect commented 7 years ago

When you guys say notification system, do you mean a push notification? For when the user is not on our website? If yes, I would be interested in that too.

wh1337 commented 7 years ago

Onesignal does push notifications. When I get back from active duty in a week or so I'll post my source code up. On Sat, Apr 29, 2017 at 13:18 Architect notifications@github.com wrote:

When you guys say notification system, do you mean a push notification? For when the user is not on our website? If yes, I would be interested in that too.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/volkanceylan/Serenity/issues/2221#issuecomment-298181961, or mute the thread https://github.com/notifications/unsubscribe-auth/AK6u3ZH07jnWS7027WLiKnZKqv5dCbjDks5r03DQgaJpZM4NLl-4 .

VR-Architect commented 7 years ago

Thanks for serving.

shakazulu89 commented 7 years ago

abela thanks .. of course i dont mean for you to write code especially :) @williamhodges that would be really great!!

wh1337 commented 7 years ago

@shakzulu89 I got your slack message and I'll get it to you as soon as I can! It won't be until next Monday though unless I can get on a computer here. But I'll try On Sat, Apr 29, 2017 at 14:47 shakazulu89 notifications@github.com wrote:

abela thanks .. of course i dont mean for you to write code especially :) @williamhodges https://github.com/williamhodges that would be really great!!

— You are receiving this because you were mentioned.

Reply to this email directly, view it on GitHub https://github.com/volkanceylan/Serenity/issues/2221#issuecomment-298187220, or mute the thread https://github.com/notifications/unsubscribe-auth/AK6u3Vh6c4iVRDvNh89D2elRi8mEH5nWks5r04XPgaJpZM4NLl-4 .

shakazulu89 commented 7 years ago

whenever you can man, appreciate it! if i want emails sent out as well, does onesignal support that? that is, via serenity...

wh1337 commented 7 years ago

The one signal doesn't do that but you can build a custom class and what not to do it as well. Onesignal basically just reaches out and uses the onesignal.com API to create the push notification. On Sat, Apr 29, 2017 at 16:00 shakazulu89 notifications@github.com wrote:

whenever you can man, appreciate it! if i want emails sent out as well, does onesignal support that? that is, via serenity...

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/volkanceylan/Serenity/issues/2221#issuecomment-298191160, or mute the thread https://github.com/notifications/unsubscribe-auth/AK6u3UT-9vf14AUs2fDeBNo3sc9Bfkvkks5r05b1gaJpZM4NLl-4 .

shakazulu89 commented 7 years ago

right. yea i was just going to start on my email class but just wanted to make sure onesignal doesnt provide something like that ;) thanks for the clarification.

wh1337 commented 7 years ago

No worries man On Sat, Apr 29, 2017 at 16:06 shakazulu89 notifications@github.com wrote:

right. yea i was just going to start on my email class but just wanted to make sure onesignal doesnt provide something like that ;) thanks for the clarification.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/volkanceylan/Serenity/issues/2221#issuecomment-298191421, or mute the thread https://github.com/notifications/unsubscribe-auth/AK6u3SQzQHoLoihIJoUQdkfLjaEWZi-hks5r05gogaJpZM4NLl-4 .

wh1337 commented 7 years ago

OneSignal:

First you need to signup for OneSignal at www.onesignal.com. They have amazing documentation on how to use their API.

First off I modified the web.config files to add the following:

<add key="OneSignalSubDomain" value="https://asdf1234.onesignal.com"/>
<add key="OneSignalAppID" value="ASDF1234"/>
<add key="OneSignalREST" value="ASDF1234"/>
<add key="ExternalSite" value="localhost:51975"/>

AppID, Rest and Subdomain will be supplied to you once you create an app on onesignal.

Under Modules > Common I added a new folder named OneSignal and with OneSignal.cs residing in it. Here are it's contents:

using System.Configuration;
using System.IO;
using System.Net;
using System.Text;
using System.Web.Script.Serialization;

namespace Phoenix.Common.OneSignal
{
    public class OneSignal
    {
        public static void Notify(string heading, string content, string endpointURL, string icon)
        {
            var config = ConfigurationManager.AppSettings;
            string AppId = config["OneSignalAppID"];
            string RESTId = config["OneSignalREST"];
            string externalSite = config["ExternalSite"];
            string url = externalSite + "/" + endpointURL;
            var request = WebRequest.Create("https://onesignal.com/api/v1/notifications") as HttpWebRequest;
            request.KeepAlive = true;
            request.Method = "POST";
            request.ContentType = "application/json; charset=utf-8";
            request.Headers.Add("authorization", "Basic " + RESTId);
            var serializer = new JavaScriptSerializer();
            var obj = new
            {
                app_id = AppId.ToString(),
                contents = new { en = content },
                headings = new { en = heading },
                url = url,
                chrome_web_icon = icon,
                included_segments = new string[] { "All" }
            };
            var param = serializer.Serialize(obj);
            byte[] byteArray = Encoding.UTF8.GetBytes(param);
            string responseContent = null;
            try
            {
                using (var writer = request.GetRequestStream())
                {
                    writer.Write(byteArray, 0, byteArray.Length);
                }
                using (var response = request.GetResponse() as HttpWebResponse)
                {
                    using (var reader = new StreamReader(response.GetResponseStream()))
                    {
                        responseContent = reader.ReadToEnd();
                    }
                }
            }
            catch (WebException ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.Message);
                System.Diagnostics.Debug.WriteLine(new StreamReader(ex.Response.GetResponseStream()).ReadToEnd());
            }
            System.Diagnostics.Debug.WriteLine(responseContent);
        }
    }
}

This is the class that you will call from your endpoint / repository. Now, in order for someone to get the actual desktop notification they have to subscribe to desktop notifications. I added the following to DashboardIndex.cshtml

string appId = System.Configuration.ConfigurationManager.AppSettings["OneSignalAppID"];
string subdomainName = System.Configuration.ConfigurationManager.AppSettings["OneSignalSubDomain"];

...

<script src="https://cdn.onesignal.com/sdks/OneSignalSDK.js" async='async'></script>
<script>
    var OneSignal = window.OneSignal || [];
    OneSignal.push(["init", {
        appId: "@appId",
        autoRegister: true,
        subdomainName: '@subdomainName',
        httpPermissionRequest: {
            enable: true
        },
        notifyButton: {
            enable: true,
            size: 'medium',
            position: 'bottom-left',
            offset: {
                bottom: '30px',
                left: '75px',
                right: '0px'
            },
            preNotify: true,
            showCredit: true,
            text: {
                'tip.state.unsubscribed': 'Awe. That hurts my feelings.',
                'tip.state.subscribed': 'You are subscribed. Dont mind me chilling here.',
                'tip.state.blocked': 'That hurts. Truly.',
                'message.prenotify': 'Click me to stay in the loop!',
                'message.action.subscribed': "Thank you for subbing. We will keep you up to date.",
                'message.action.resubscribed': "Glad to see you're back.",
                'message.action.unsubscribed': "You won't receive notifications again. Promise. :(",
                'dialog.main.title': 'Manage Site Notifications',
                'dialog.main.button.subscribe': 'NOTIFY ME',
                'dialog.main.button.unsubscribe': 'LEAVE ME ALONE',
                'dialog.blocked.title': 'Unblock Notifications',
                'dialog.blocked.message': "Follow these instructions to allow notifications:"
            }
        },
        welcomeNotification: {
            title: "Welcome to Notifications",
            message: "Thank you for signing up for desktop notifications."
        }
    }])
</script>

Now you have it all set up. Keep in mind: if your app url doesn't match what you put into your onesignal.com app, the notification will not appear. This is why I have one onesignal app for my development server, test server and the production.

To create a desktop notification on insert, delete, or update add it do the following:

In MyEntityRepository.cs:

public SaveResponse Create(IUnitOfWork uow, SaveRequest<MyRow> request)
        {
            SaveResponse response = new MySaveHandler().Process(uow, request, SaveRequestType.Create);
            Common.OneSignal.OneSignal.Notify("New Drone Created", "A new Drone as been created. Drone ID: " + response.EntityId, "ccm/panel/drones/" + response.EntityId, "https://cdn1.iconfinder.com/data/icons/ui-navigation-1/152/alert-256.png");
            //return new MySaveHandler().Process(uow, request, SaveRequestType.Create);
            return response;
        }
wh1337 commented 7 years ago

if you need any more just let me know

shakazulu89 commented 7 years ago

@williamhodges thank you so much! i was away that is why i didnt look at it yet. willdo that the next few days :)

stixoffire commented 4 years ago

@abelal83 I am having trouble implementing the Signal R from WIKI - I am mainly looking at the UserSupportGroupRow nothing is documented as to what that row looks like and I am trying to follow the process of notification ..

Can you supply some information ?

Basically I have certain events that occur and a notification Group . I have an EventType (new quote, new order) , NotifyMethod (email, sms, signalR), Group.

So I can get all members of a group and notify by method according to event

The Groups contain the members UserName, Id etc..

Can you assist ?

The two code areas I am most in need of information and guidance is NotificationHub OnCOnnected Method - UserSupportGroupRow .. BroadcastNotificationToClients What is this method doing and how to implement for my scenario ?

Basically I would like to be able to do a simple notification - so I can add a Call in my Repository Methods - Call Notifier - and it notifies everyone according to their assignment.

muhammedyaman commented 2 years ago

Hi @cryptic-ai Thanks for your work. Do you have a sample for user-based notifications? Sending notifications to specific users?

S-Muthuselvakumar commented 6 months ago

if you need any more just let me know

Hi i am new to serenity. i need a notification in my project for my process due date. kindly help me with this

@volkanceylan