DChhapgar
DChhapgar

Reputation: 2340

Acumatica mobile push notification without using Business Event

How do I send mobile push notification without using Business Event ?

Upvotes: 1

Views: 229

Answers (1)

DChhapgar
DChhapgar

Reputation: 2340

It is possible to send mobile push notification without using Business Events to open screen in mobile application when user taps the notification.

Below example describes approach :

using System;
using System.Collections;
using System.Linq;
using System.Threading;
using CommonServiceLocator;
using PX.Api.Mobile.PushNotifications;
using PX.Common;
using PX.Data;
using PX.Objects.SO;

namespace PX.PushNotificatioinSample.Ext
{
    public class SOOrderEntryPXExt : PXGraphExtension<SOOrderEntry>
    {
        public PXAction<SOOrder> ViewOnMobileApp;
        [PXUIField(DisplayName = Messages.ViewActionDisplayName, 
                   MapEnableRights = PXCacheRights.Select, MapViewRights = PXCacheRights.Select)]
        [PXButton(SpecialType = PXSpecialButtonType.Default)]
        public virtual IEnumerable viewOnMobileApp(PXAdapter adapter)
        {
            if (Base.Document.Cache.GetStatus(Base.Document.Current) == PXEntryStatus.Inserted ||
                Base.Document.Cache.GetStatus(Base.Document.Current) == PXEntryStatus.InsertedDeleted) { return adapter.Get(); }

            //Get instance of PushNotification service
            var pushNotificationSender = ServiceLocator.Current.GetInstance<IPushNotificationSender>();

            //Users to whom message will be sent
            var userIds = new[] { PXAccess.GetUserID() };

            //Check if User is using Acumatica Mobile App
            var activeTokens = pushNotificationSender.CountActiveTokens(userIds);
            if (activeTokens == 0)
            {
                throw new PXException(Messages.NoDeviceError);
            }

            string sOrderNbr = Base.Document.Current.OrderNbr;
            string sScreenID = Base.Accessinfo.ScreenID.Replace(".", "");
            Guid noteID = Base.Document.Current.NoteID.Value;

            PXLongOperation.StartOperation(Base, () =>
            {
                try
                {
                    pushNotificationSender.SendNotificationAsync(
                                        userIds: userIds,
                                        // Push Notification Title
                                        title: Messages.PushNotificationTitle,
                                        // Push Notification Message Body
                                        text: $"{ Messages.PushNotificationMessageBody } { sOrderNbr }.",
                                        // Link to Screen to open upon tap with Sales Order data associated to NoteID
                                        link: (sScreenID, noteID),
                                        cancellation: CancellationToken.None);
                }
                catch (AggregateException ex)
                {
                    var message = string.Join(";", ex.InnerExceptions.Select(c => c.Message));
                    throw new InvalidOperationException(message);
                }
            });

            return adapter.Get();
        }
    }

    [PXLocalizable]
    public static class Messages
    {
        public const string ViewActionDisplayName = "View On Mobile App";
        public const string NoDeviceError = "You need to set up the Acumatica mobile app.";
        public const string PushNotificationTitle = "View Sales Order";
        public const string PushNotificationMessageBody = "Tap to view Sales Order # ";
    }
}

Upvotes: 2

Related Questions