从服务发送通知 [英] Send notification from service

查看:28
本文介绍了从服务发送通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 signalR 服务已经开始运行,现在我需要在收到消息时向用户发送通知.我编写了执行但未显示通知的通知代码.我的服务代码是:

I've got my signalR service working and now I need to send a notification to the user when a message is received. I wrote the notification code which is executed but no notification is displayed. My service code is:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Acr.UserDialogs;
using Android;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Support.V4.App;
using Android.Views;
using Android.Widget;
using Java.Lang;
using Java.Util.Concurrent;
using Microsoft.AspNet.SignalR.Client;
using Microsoft.AspNet.SignalR.Client.Transports;


namespace MyAndroid
{
    [Service]
    public class SignalRSrv : Service
    {
        private bool InstanceFieldsInitialized = false;
        private string username = "";
        private string firstname = "";
        private string lastname = "";
        private string company = "";
        private string department = "";
        private string section = "";

        private void InitializeInstanceFields()
        {
            mBinder = new LocalBinder(this);
        }

        private Handler mHandler; // to display any received messages
        private IBinder mBinder; // Binder given to clients
        private SignalRSingleton mInstance;
        private Notification notification = null;

        public SignalRSrv()
        {
            if (!InstanceFieldsInitialized)
            {
                InitializeInstanceFields();
                InstanceFieldsInitialized = true;
            }

        }

        public override void OnCreate()
        {
            base.OnCreate();
            mInstance = SignalRSingleton.getInstance();
            mHandler = new Handler(Looper.MainLooper);
            notification = RegisterForegroundService(); // here we set up the notification and start in foreground service
        }

        public override void OnDestroy()
        {
            base.OnDestroy();
        }


        public override IBinder OnBind(Intent intent)
        {

            //binder = new LocalBinder(this);
            User MyUser = new User("", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "");
            Bundle bundlee = intent.GetBundleExtra("TheBundle");
            MyUser = bundlee.GetParcelable("MyUser") as User;

            username = MyUser.Username;
            firstname = MyUser.Firstname;
            lastname = MyUser.Lastname;
            company = intent.GetStringExtra("theSelectedCompany");
            department = intent.GetStringExtra("theSelectedDepartment");
            section = intent.GetStringExtra("theSelectedSection");

            startSignalR(bundlee);
            return mBinder;
        }

        private void startSignalR(Bundle bundle)
        {
            mInstance.setmHubConnection(username, firstname,lastname,company,department,section);
            mInstance.setHubProxy();

            try
            {
                // Connect the client to the hup
                mInstance.mHubConnection.Start();

                mInstance.mHubProxy.On("broadcastMessage", (string platform, string message) =>
                {
                    try
                    {
                        showNotification(message, bundle, notification);

                    }
                    catch (System.Exception e)
                    {
                       var error = e.Message;
                    }

            }
            catch (System.Exception e) when (e is InterruptedException || e is ExecutionException)
            {
                //opps

                var x = 1;
                return;
            }
        }

        public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId)
        {

            User MyUser = new User("", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "");
            Bundle bundlee = intent.GetBundleExtra("TheBundle");
            MyUser = bundlee.GetParcelable("MyUser") as User;

            username = MyUser.Username;
            firstname = MyUser.Firstname;
            lastname = MyUser.Lastname;
            company = intent.GetStringExtra("theSelectedCompany");
            department = intent.GetStringExtra("theSelectedDepartment");
            section = intent.GetStringExtra("theSelectedSection");

            startSignalR(bundlee);
            return StartCommandResult.Sticky;
        }
        Notification RegisterForegroundService()
        {
            var notification = new NotificationCompat.Builder(this)
                .SetContentTitle("League Alert")
                .SetContentText(Resources.GetString(Resource.String.notification_text))
                .SetSmallIcon(Resource.Drawable.alert_box)
                // Enlist this instance of the service as a foreground service
            StartForeground(Constants.SERVICE_RUNNING_NOTIFICATION_ID, notification);
            return notification;
        }



        public void showNotification(string message, Bundle bundle, Notification notification)
        {
            int count = 1;
            try
            {
                Intent resultIntent = new Intent(this, typeof(Drawer));
                // Pass some values to SecondActivity:
                resultIntent.PutExtra("TheBundle", bundle);

                // Construct a back stack for cross-task navigation:
                Android.App.TaskStackBuilder stackBuilder = Android.App.TaskStackBuilder.Create(this);
                stackBuilder.AddParentStack(Java.Lang.Class.FromType(typeof(Drawer)));
                stackBuilder.AddNextIntent(resultIntent);

                // Create the PendingIntent with the back stack:            
                PendingIntent resultPendingIntent =
                    stackBuilder.GetPendingIntent(0, PendingIntentFlags.UpdateCurrent);

                NotificationManager notificationManager =
                    (NotificationManager)GetSystemService(Context.NotificationService);
                notificationManager.Notify(Constants.SERVICE_RUNNING_NOTIFICATION_ID, notification);

            }
            catch (System.Exception e)
            {
                var error = e.Message;
            }
        }
        private string CreateNotificationChannel()
        {
            if (Build.VERSION.SdkInt < BuildVersionCodes.O)
            {
                // Notification channels are new in API 26 (and not a part of the
                // support library). There is no need to create a notification
                // channel on older versions of Android.
                return "";
            }

            var channelName = "My Messenger";
            var channelDescription = "My Messenger Channel"; // GetString(Resource.String.channel_description);
            var channel = new NotificationChannel("0", channelName, NotificationImportance.Default)
            {
                Description = channelDescription
            };

            var notificationManager = (NotificationManager)GetSystemService(NotificationService);
            notificationManager.CreateNotificationChannel(channel);

            return channelName;
        }
    }

    public class LocalBinder : Binder
    {
        private readonly SignalRSrv outerInstance;

        public LocalBinder(SignalRSrv outerInstance)
        {
            this.outerInstance = outerInstance;
        }

        public virtual SignalRSrv Service
        {
            get
            {
                // Return this instance of SignalRSrv so clients can call public methods
                return outerInstance;
            }
        }
    }
}

代码编译没有错误,我在代码中设置了断点以遵循流程,一切似乎都在正确执行,但没有出现通知.我错过了什么?

The code compiles with no errors and I set breakpoints in the code to follow the flow and all seems to be executing properly but no notification appears. What am I missing?

推荐答案

经过一些进一步的研究和实验,我终于弄清楚了.我现在工作的服务代码是:

After some further research and experimentation I was able to figure it out. My now working service code is:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Acr.UserDialogs;
using Android;
using Android.App;
using Android.Content;
using Android.Graphics;
using Android.OS;
using Android.Runtime;
using Android.Support.V4.App;
using Android.Views;
using Android.Widget;
using Java.Lang;
using Java.Util.Concurrent;
using Microsoft.AspNet.SignalR.Client;
using Microsoft.AspNet.SignalR.Client.Transports;
using Android.Net;
using Android.Media;

namespace My_Android_service
{
    [Service]
    public class SignalRSrv : Service
    {
        private bool InstanceFieldsInitialized = false;
        private string username = "";
        private string firstname = "";
        private string lastname = "";
        private string companny = "";
        private string department = "";
        private string section = "";
        private int notifyid = 0;

        private void InitializeInstanceFields()
        {
            mBinder = new LocalBinder(this);
        }

        private Handler mHandler; // to display any received messages
        private IBinder mBinder; // Binder given to clients
        private SignalRSingleton mInstance;
        private Notification notification = null;

        public SignalRSrv()
        {
            if (!InstanceFieldsInitialized)
            {
                InitializeInstanceFields();
                InstanceFieldsInitialized = true;
            }

        }

        public override void OnCreate()
        {
            base.OnCreate();
            mInstance = SignalRSingleton.getInstance();
            mHandler = new Handler(Looper.MainLooper);
        }

        public override void OnDestroy()
        {
           try
            {
                base.OnDestroy();

            }
            catch (System.Exception e)
            {
                var m = e.Message;
            }
        }


        public override IBinder OnBind(Intent intent)
        {

            User MyUser = new User("", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "");
            Bundle bundlee = intent.GetBundleExtra("TheBundle");
            MyUser = bundlee.GetParcelable("MyUser") as User;

            username = MyUser.Username;
            firstname = MyUser.Firstname;
            lastname = MyUser.Lastname;
            company = intent.GetStringExtra("theSelectedCompany");
            department = intent.GetStringExtra("theSelectedDepartment");
            Section = intent.GetStringExtra("theSelectedSection");

            startSignalR(bundlee);
            return mBinder;
        }

        private void startSignalR(Bundle bundle)
        {
            mInstance.setmHubConnection(username, firstname,lastname,company,department,section);
            mInstance.setHubProxy();

            try
            {
                // Connect the client to the hub
                mInstance.mHubConnection.Start();

                // Setup the event handler for message received
                mInstance.mHubProxy.On("broadcastMessage", (string platform, string message) =>
                {
                    try
                    {
                        showNotification(message, bundle, notification);

                    }
                    catch (System.Exception e)
                    {
                       var error = e.Message;
                    }
                });

            }
            catch (System.Exception e) when (e is InterruptedException || e is ExecutionException)
            {
                // handle any errors
                var x = 1;
                return;
            }
        }

        public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId)
        {

            User MyUser = new User("", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "");
            Bundle bundlee = intent.GetBundleExtra("TheBundle");
            MyUser = bundlee.GetParcelable("MyUser") as User;

            username = MyUser.Username;
            firstname = MyUser.Firstname;
            lastname = MyUser.Lastname;
            company = intent.GetStringExtra("theSelectedCompany");
            department = intent.GetStringExtra("theSelectedDepartment");
            section = intent.GetStringExtra("theSelectedSection");

            startSignalR(bundlee);

            // Set up Notification
            Notification notify = new Notification();
            notify.Defaults = NotificationDefaults.Sound;
            notify.Defaults = NotificationDefaults.Vibrate;

            // Start Notification system, app will crash without this
            StartForeground(Constants.SERVICE_RUNNING_NOTIFICATION_ID, notify);

            return StartCommandResult.Sticky;
        }

        public void showNotification(string message, Bundle bundle, Notification notification)
        {
            try
            {
                // Activity to open when notification clicked, I'm not doing this yet.
                //Intent intent = new Intent(this, typeof(Drawer)); //Activity you want to open
                //intent.AddFlags(ActivityFlags.ClearTop);
                //intent.PutExtra("TheBundle", bundle);
                //var pendingIntent = PendingIntent.GetActivity(this, RandomGenerator(), intent, PendingIntentFlags.OneShot);

                NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                     .SetSmallIcon(Resource.Drawable.alert_box)
                     .SetContentTitle("Message Received")
                     .SetContentText(message)
                     //.SetSound(Settings.System.DefaultNotificationUri)
                     .SetVibrate(new long[] { 1000, 1000 })
                     .SetLights(Color.AliceBlue, 3000, 3000)
                     .SetAutoCancel(true);
                //.SetContentIntent(pendingIntent);


                // If this is oreo or above, we need a channel
                NotificationChannel channel = null;

                // Set sound to be used for notification
                Android.Net.Uri alarmSound = RingtoneManager.GetDefaultUri(RingtoneType.Notification);

                if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
                {
                    string channelId = "OML_Messenger"; //Context.GetString(Resource.String.default_notification_channel_id);
                    channel = new NotificationChannel(channelId, "Message Received", NotificationImportance.Default);
                    channel.Description = ("Message Received From Administrator");
                    notificationBuilder.SetSound(alarmSound);
                    notificationBuilder.SetChannelId(channelId);
                }

                // Connect to the notification system setup in OnStartCommandResult
                NotificationManager notificationManager = (NotificationManager)Android.App.Application.Context.GetSystemService(Context.NotificationService);

                // Create the channel, if not null
                if (!channel == null)
                {
                    notificationManager.CreateNotificationChannel(channel);
                }
                notifyid = RandomGenerator(); // Get a channel ID

                // Send the noitification
                notificationManager.Notify(notifyid, notificationBuilder.Build());

            }
            catch (System.Exception e)
            {
                var error = e.Message;
            }
        }
        private int RandomGenerator()
        {
            return new Random().Next(int.MinValue, int.MaxValue);
        }
    }

    public class LocalBinder : Binder
    {
        private readonly SignalRSrv outerInstance;

        public LocalBinder(SignalRSrv outerInstance)
        {
            this.outerInstance = outerInstance;
        }

        public virtual SignalRSrv Service
        {
            get
            {
                // Return this instance of SignalRSrv so clients can call public methods
                return outerInstance;
            }
        }
    }
}

这篇关于从服务发送通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆