.NET quickly connects with Jiguang message push

What is push message?

Many mobile APPs will push messages to users from time to time. For example, some news APPs will push news that the user may be interested in, or if the APP is updated, they will push messages to the user whether to choose to update, etc. This is the so-called ” Message push”.

Some common examples of APP message push

Strong marketing category:

Directly display the marketing efforts and marketing model in a hawking style, with the purpose of arousing users’ greed and curiosity through discounts and timeliness, as shown below:

Picture

Strong correlation:

In the era of information explosion, the brain will automatically filter out the information that is valuable to itself and the information that is worthless. If there are words such as @you, you, etc. in a message, the brain will automatically recognize it. The skill of using direct correlation lies in clever use. Words related to “you”.

Picture

Strong topicality:

There is a saying in the marketing industry that communication cannot be created without a sense of violation, and topics cannot be created without being relevant. Copywriting with such a strong topicality has its own communication attributes and will generally hit a certain feeling in the user’s heart, such as their concern for society. Cynical, rebellious against high housing prices, artistic attachment to travel, etc.

Picture

Introduction to Aurora Push

JPush is a professional app message push service platform with an average daily message volume exceeding tens of billions. JPush supports Android, iOS, QuickApp, Web and other platforms. The SDK access is convenient and fast. The push channel is high-speed and stable and supports overseas dedicated lines. , the API open interface is powerful, flexible and easy to use, and the WEB side supports operational functions such as notification creation, after-effect analysis, tag alias management and troubleshooting. JPush not only provides developers with basic push services, but also provides service capabilities such as precise user labeling, user grouping, geo-fencing, in-app messaging, smart sending strategies, and smart activation, which can effectively improve the delivery of messages. reach rate, display rate and click-through rate, and help APP improve daily activity and retention through refined operation and reach.

Platform type support

Picture

Message type support

Picture

Notification style support

Picture

Why choose Jiguang as the APP’s message push platform?

  • First of all, Aurora Push supports multi-platform push.

  • Support large-scale message push.

  • Jiguang Push is easy to connect, and corresponding SDKs are provided for different back-end languages.

  • The support for free accounts is also very friendly (however, there are resource bottlenecks during the peak period of free accounts. If you need strong timeliness, you can purchase the premium version of the paid service).

Picture

Quickly connect to Jpush Aurora Push

  • Register a developer account on the Jiguang Push official website;

  • Log in to the management console, create an application, and get the Appkey (the SDK and the server recognize each other through the Appkey);

  • In the push settings, set the package name for Android, upload the certificate for iOS, enable WinPhone, and select according to your needs;

.NET FX 4.5 project quick access

This project is based on C#/.NET (example of .NET Framework 4.5.1) Jiguang push docking example, mainly docking the SKD provided by Jiguang integration for our .Neter. Here I mainly encapsulate three push methods: single device registration ID push, device registration ID batch push and broadcast push. For other push methods, you can refer to the documentation for encapsulation.

  • JPuhs-Sample (encapsulated sample source code): https://github.com/YSGStudyHards/JPuhs-Sample

1. Introduce Jiguang.JPush nuget package into the project

Picture

2. Aurora push call

namespace Jpush.Controllers
{
    /// <summary>
    /// Aurora push management
    /// </summary>
    public class JPushManageController : Controller
    {

        private readonly JPushClientUtil _jPushClientUtil;

        public JPushManageController(JPushClientUtil jPushClientUtil)
        {
          this._jPushClientUtil=jPushClientUtil;
        }


        /// <summary>
        /// Single device registration ID push
        /// </summary>
        /// <returns></returns>
        public ActionResult SendPushByRegistrationId()
        {
            var isOk = _jPushClientUtil.SendPushByRegistrationId("Time chasers welcome you!", "Happy New Year 2022", "1507bfd3f715abecfa4", new Dictionary<string, object>(), true);

            return Json(new { result = isOk });
        }


        /// <summary>
        /// Batch push of device registration IDs (up to 1000 at a time)
        /// </summary>
        /// <returns></returns>
        public ActionResult SendPushByRegistrationIdList()
        {
            var registrationIds = new List<string>() { "1507bfd3f715abecfa455", "1507bfd3f715abecfa433", "1507bfd3f715abecfa422" };
            var isOk = _jPushClientUtil.SendPushByRegistrationIdList("Time chasers welcome you!", "Happy New Year 2022", registrationIds, new Dictionary<string, object>(), true);

            return Json(new { result = isOk });
        }


        /// <summary>
        /// Broadcast push
        /// </summary>
        /// <returns></returns>
        public ActionResult BroadcastPush()
        {
            var isOk = _jPushClientUtil.BroadcastPush("Time chasers welcome you!", "Happy New Year 2022", new Dictionary<string, object>(), true);

            return Json(new { result = isOk });
        }

    }
}

3. Jiguang push tool class (JPushClientUtil)

namespace Jpush.Common
{
    /// <summary>
    /// Aurora push tool class
    /// </summary>
    public class JPushClientUtil
    {
        private const string appKey = "youAppKey";
        private const string masterSecret = "youMasterSecret";
        private static JPushClient client = new JPushClient(appKey, masterSecret);

        /// <summary>
        /// Single device registration ID push
        /// </summary>
        /// <param name="title">Push title (only exists on Android)</param>
        /// <param name="noticeContent">Notice content</param>
        /// <param name="registrationid">Device registration ID (registration_id)</param>
        /// <param name="extrasParam">Extended parameters (pass in some parameter identifiers received by the App)</param>
        /// <param name="isApnsProduction">Note: Whether iOS pushes the production environment (true yes, false not the development environment)</param>
        /// <returns></returns>
        public bool SendPushByRegistrationId(string title, string noticeContent, string registrationid, Dictionary<string, object> extrasParam = null, bool isApnsProduction = true)
        {
            //Device identification parameter splicing
            var pushRegistrationId = new RegistrationIdList();
            pushRegistrationId.registration_id.Add(registrationid);

            return JPushBaseSendMessage(title, noticeContent, isApnsProduction, pushRegistrationId, extrasParam);
        }

        /// <summary>
        /// Batch push of device registration IDs (up to 1000 at a time)
        /// </summary>
        /// <param name="title">Push title (only exists on Android)</param>
        /// <param name="noticeContent">Notice content</param>
        /// <param name="registrationIds">Registration ID (registration_id) list, up to 1000 can be pushed at one time</param>
        /// <param name="extrasParam">Extended parameters (pass in some parameter identifiers received by the App)</param>
        /// <param name="isApnsProduction">Note: Whether iOS pushes the production environment (true yes, false not the development environment)</param>
        /// <returns></returns>
        public bool SendPushByRegistrationIdList(string title, string noticeContent, List<string> registrationIds, Dictionary<string, object> extrasParam = null, bool isApnsProduction = true)
        {
            //Device identification parameter splicing
            var pushRegistrationId = new RegistrationIdList();
            pushRegistrationId.registration_id.AddRange(registrationIds);

            return JPushBaseSendMessage(title, noticeContent, isApnsProduction, pushRegistrationId, extrasParam);
        }

        /// <summary>
        /// Broadcast push
        /// </summary>
        /// <param name="title">Push title (only exists on Android)</param>
        /// <param name="noticeContent">Notice content</param>
        /// <param name="extrasParam">Extended parameters (pass in some parameter identifiers received by the App)</param>
        /// <param name="isApnsProduction">Note: Whether iOS pushes the production environment (true yes, false not the development environment)</param>
        /// <returns></returns>
        public bool BroadcastPush(string title, string noticeContent, Dictionary<string, object> extrasParam = null, bool isApnsProduction = true)
        {
            return JPushBaseSendMessage(title, noticeContent, isApnsProduction, null, extrasParam, true);
        }

        /// <summary>
        /// Jiguang message push public method
        /// </summary>
        /// <param name="title">Push title (only exists on Android)</param>
        /// <param name="noticeContent">Notice content</param>
        /// <param name="pushRegistrationId">Device Registration ID (registration_id)</param>
        /// <param name="isApnsProduction">Whether iOS pushes to the production environment (true does, false does not push the development environment)</param>
        /// <param name="extrasParam">Extended parameters</param>
        /// <param name="isRadioBroadcast">Whether to broadcast</param>
        /// <returns></returns>
        private bool JPushBaseSendMessage(string title, string noticeContent, bool isApnsProduction, RegistrationIdList pushRegistrationId, Dictionary<string, object> extrasParam, bool isRadioBroadcast = false)
        {
            try
            {
                object audience = pushRegistrationId;

                if (isRadioBroadcast)
                {
                    audience = "all";
                }

                var pushPayload = new PushPayload()
                {
                    Platform = new List<string> { "android", "ios" },//Push platform settings
                    Audience = audience,//Push target
                    //notifacation: notification content body. is the content that is pushed to the client. Along with message, one of the two must exist, and both can coexist.
                    Notification = new Notification
                    {
                        Alert = noticeContent,//Notification content
                        Android = new Android
                        {
                            Alert = noticeContent,//Notification content
                            Title = title,//Notification title
                            URIActivity = "com.king.sysclearning.platform.app.JPushOpenClickActivity",//This field is used to specify the activity that the developer wants to open. The value is the "android:name" attribute value of the activity node; suitable for Huawei, Xiaomi, vivo manufacturer channel jump
                            URIAction = "com.king.sysclearning.platform.app.JPushOpenClickActivity",//This field is used to specify the activity that the developer wants to open, the value is "activity"-"intent-filter"-"action" node "android" :name" attribute value; adapted to oppo and fcm jumps
                            Extras = extrasParam //Customize the Key/Value information in JSON format here for business use.
                        },
                        IOS = new IOS
                        {
                            Alert = noticeContent,
                            Badge = " + 1", //This item specifies that the pushed badge will automatically increase by 1
                            Extras = extrasParam //Customize the Key/Value information in JSON format here for business use.
                        }
                    },
                    Options = new Options//optional parameters
                    {
                        //IOS environment inconsistency problem: API pushes messages to iOS, you need to set apns_production to specify the push environment, false for development, true for production.
                        IsApnsProduction = isApnsProduction// Set up iOS push production environment. Not set to default to development environment.
                    }
                };

                var response = client.SendPush(pushPayload);
                //200 must be correct. All exceptions do not use the 200 return code
                if (response.StatusCode == HttpStatusCode.OK)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception ex)
            {
                return false;
            }
        }
    }

    public class RegistrationIdList
    {
        /// <summary>
        /// Device registration ID
        /// </summary>
        public List<string> registration_id { get; set; } = new List<string>();
    }
}

Reference article

  • Take you to understand APP message push (Push) in ten minutes: https://www.woshipm.com/operate/526224.html

  • Jiguang detailed docking documentation: https://docs.jiguang.cn/jpush/quickstart/3m_dem