1

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

DChhapgar
  • 2,280
  • 12
  • 18

1 Answers1

2

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 # ";
    }
}
DChhapgar
  • 2,280
  • 12
  • 18
  • For what purpose you encourage use of interface that is marked with [PXInternalUseOnlyAttribute]? This attribute means that Acumatica can change or remove it in any version (major or minor) and will not notice it in Release notes as it will not be breaking change. – Kirill Bestemyanov Apr 07 '20 at 10:17