示例#1
0
        private static dynamic CreateMessage(PushNotificationRequest pushNotification)
        {
            dynamic message = new ExpandoObject();

            message.registration_ids = new[] { pushNotification.RegistrationId };
            message.priority         = "high";
            const string soundName = "default";

            // REF Firebase Cloud Messaging (FCM)
            // https://firebase.google.com/docs/cloud-messaging/concept-options

            // Use notification messages when you want FCM to handle displaying a notification on your client app's behalf.
            // Use data messages when you want to process the messages on your client app.

            // Common fields for iOS and Android
            // message.notification.title
            // message.notification.body
            // message.data

            switch (pushNotification.Platform)
            {
            case PushNotificationPlatform.IOS:
                // Use notification message
                message.notification = new
                {
                    title = pushNotification.Title,
                    body  = pushNotification.Body,
                    sound = soundName,
                };
                break;

            case PushNotificationPlatform.Android:     // Work for start up code block and get push in background
                // Use data message
                message.data = new Dictionary <string, object>()
                {
                    { "title", pushNotification.Title },
                    { "body", pushNotification.Body },
                    { "soundname", soundName },
                    // Just dump property to show that we need to use snake case naming.
                    // If we use camel case, the data won't sent.
                    { "transaction_id", 1 },
                    { "additionalData", "other" }     // This field won't sent
                };
                break;

            default:
                throw new InvalidOperationException("Invalid registration platform");
            }

            return(message);
        }
示例#2
0
        public void SendPushNotifcation_ValidRequest_PushNotficationSentSuccessfully()
        {
            var pushNotification = new PushNotificationRequest()
            {
                Title          = "Push message",
                Body           = "FCM push message, credit admin Pong Codesanook",
                RegistrationId = registrationId,
                Platform       = PushNotificationPlatform.Android
            };

            var response = SendPushNotification(pushNotification);

            Assert.True(response.Success);
        }
示例#3
0
        private PushNotificationResponse SendPushNotification(PushNotificationRequest pushNotification)
        {
            var message = CreateMessage(pushNotification);
            var request = CreateRequest();

            var payloadData = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(message));

            request.ContentLength = payloadData.Length;

            WebResponse response;

            using (var requestStream = request.GetRequestStream())
            {
                requestStream.Write(payloadData, 0, payloadData.Length);
                response = request.GetResponse();
            }

            var responseCode = ((HttpWebResponse)response).StatusCode;

            switch (responseCode)
            {
            case HttpStatusCode.OK:
                using (var reader = new StreamReader(response.GetResponseStream()))
                {
                    var responseContent = reader.ReadToEnd();
                    return(JsonConvert.DeserializeObject <PushNotificationResponse>(responseContent));
                }

            case HttpStatusCode.Unauthorized:
            case HttpStatusCode.Forbidden:
                throw new InvalidOperationException("Unauthorized - need a new token");

            default:
                // TODO need to log more exception information
                throw new InvalidOperationException($"Response from web service with status code {responseCode}");
            }
        }