如何在不使用BusinessEvent的情况下发送移动推送通知?
当用户点击通知时,可以在不使用Business Events
打开移动应用程序屏幕的情况下发送mobile push notification
。
以下示例描述了方法:
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 # ";
}
}