Monday, November 9, 2020

Amazon Pinpoint push notification .Net code sample



I found out that AWS only provides push notification integration code example for Python and Node.js only but they providing .net SDK for it.

Below is the step I had done for develop the push notification program with c#.net.

1. Add the AWS package to the project.


2. Create a class name program.cs.



3. The program divided by 2 parts : -
        - Create the CredentialProfile in the machine.
        - Send Push notification.

4. The credentialProfile works like the pass to let Pinpoint allow you send the notification from the machine.
 
5. Copy the code below: 

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Amazon;
using Amazon.Pinpoint;
using Amazon.Pinpoint.Model;
using Amazon.Runtime.CredentialManagement;


namespace pinpoint_pushnotification
{
    class Program
    {
        // The AWS Region that you want to use to send the message. For a list of
        // AWS Regions where the Amazon Pinpoint API is available, see
        // https://docs.aws.amazon.com/pinpoint/latest/apireference/
        private static readonly string region = "ap-south-1";
        private static readonly string appId = "XXXXXX"; //the appId provided by AWS


        //token and service
        private static string serviceType = "APNS"; //APNS or GSM

        //message type
        private static string title = "Message from .Net";
        private static string desc = "Hello world"; 
        private static string notiType = "newEmployee"; //is custom value to send to app
        private static string employeeId = "001";
        private static string targetId = "12";

        //android
        private static string destinationNumber = "The phone token";
        private static string rawMsg = "{\"notification\":{\"title\":\"" + title + "\",\"body\":\"" + desc + "\"},\"data\":{\"notiType\":\"" + notiType + "\",\"employeeID\":\"" + employeeId + "\",\"targetID\":\"" + targetId + "\"}}"; //json string
       
        //ios
        private static string iosDestinationNumber = "iOS phone token";
        private static string iOSRawMsg = "{\"aps\":{\"alert\":{\"title\":\"" + title + "\",\"body\":\"" + desc + "\"},\"sound\":\"default\",\"notiType\":\"" + notiType + "\",\"employeeID\":" + employeeId + ",\"targetID\":" + targetId + "}}";

        static void Main(string[] args)
        {
            WriteProfile("default", "ACCESSKEYID", "AWSSECRETKEY"); //for 1st time only
            PNMessage().Wait();
        }


        private static void WriteProfile(string profileName, string keyId, string secret)
        {
            var chain = new CredentialProfileStoreChain();
            CredentialProfile basicProfile;
            if (chain.TryGetProfile("default", out basicProfile))
            {
                // Use default
                Console.WriteLine($"Using the [default] profile...");
            }
            else
            {
                Console.WriteLine($"Create the [{profileName}] profile...");
                var options = new CredentialProfileOptions
                {
                    AccessKey = keyId,
                    SecretKey = secret
                };
                var profile = new CredentialProfile(profileName, options);
                var sharedFile = new SharedCredentialsFile();
                sharedFile.RegisterProfile(profile);
            }
            
        }

        private static async Task PNMessage()
        {
            using (AmazonPinpointClient client = new AmazonPinpointClient(RegionEndpoint.GetBySystemName(region)))
            {
                var sendRequest = new SendMessagesRequest();
                if (serviceType == "GSM")
                {
                    sendRequest = new SendMessagesRequest()
                    {
                        ApplicationId = appId,
                        MessageRequest = new MessageRequest
                        {
                            Addresses = new Dictionary<string, AddressConfiguration>
                        {
                            {
                                destinationNumber,
                                new AddressConfiguration
                                {
                                    ChannelType = "GCM"
                                }
                            }
                        },
                            Context = new Dictionary<string, string>
                        {
                            {
                                "context1",
                                "context2"
                            }

                        },
                            MessageConfiguration = new DirectMessageConfiguration
                            {
                                GCMMessage = new GCMMessage
                                {
                                    //Title = title,
                                    // Body = message,
                                    Priority = "high",
                                    Action = Amazon.Pinpoint.Action.DEEP_LINK,
                                    RawContent = rawMsg

                                }
                            }
                        }
                    };
                }
                else if (serviceType == "APNS")
                {
                    sendRequest = new SendMessagesRequest()
                    {
                        ApplicationId = appId,
                        MessageRequest = new MessageRequest
                        {
                            Addresses = new Dictionary<string, AddressConfiguration>
                        {
                            {
                                iosDestinationNumber,
                                new AddressConfiguration
                                {
                                    ChannelType = "APNS" //APNS_SANDBOX
                                }
                            }
                        },
                            Context = new Dictionary<string, string>
                        {
                            {
                                "context1",
                                "context2"
                            }

                        },
                            MessageConfiguration = new DirectMessageConfiguration
                            {
                                APNSMessage = new APNSMessage
                                {
                                    //Title = title,
                                    // Body = message,
                                    Priority = "10",
                                    Action = Amazon.Pinpoint.Action.DEEP_LINK,
                                    RawContent = iOSRawMsg

                                }
                            }
                        }
                    };

                }
               
                try
                {
                    Console.WriteLine("Sending message...");
                    SendMessagesResponse response = await client.SendMessagesAsync(sendRequest).ConfigureAwait(false);                   
                    Console.WriteLine("Message sent!");
                }
                catch (Exception ex)
                {
                    Console.WriteLine("The message wasn't sent. Error message: " + ex.Message);
                }
            }

        }
    }
}

6. The access key ( "ACCESSKEYID" and "AWSSECRETKEY") at the CredentialProfile you can get from IAM > User > Create access key


7. The appId you can Pinpoint > Setting > General settings and get the project Id 


8. Put ur test device fcm token or APNS token to the code
9. Execute the program.



Sunday, September 18, 2016

Words to use instead of 'Very'

Very Cute = Adorable

Very Accurate = Exact

Very Loud = Thunderous

Very Slow = Sluggish

Very big = Massive

Very colorful = Vibrant

Very annoying = exasperating

Very dirty = Filthy

Very Fat = Obese

Very Excited = Thrilled

Very Competitive = Cutthroat

Very Confused = Perplexed

Very Creative = Innovative

Very Crowded = Bustling

Very Dangerous = Perilous




Sunday, July 17, 2016

New Born Baby Checklist 新出生的婴儿清单:-


Below is the checklist for new born baby : -

Packing for Hospital 生产前准备用品
For Mom
For Baby
Daily Necessities 日用品
New Born Outfit (4 Sets) 新生儿服装 (4)
Disposable Maternity Pad 孕妇产后垫
New Born Disposable Diaper 新生儿纸尿裤
Sarong 纱笼
Blanket & Towel 毛毯和毛巾
Disposable Panties 孕妇纸内裤
Baby Wipes 婴儿湿纸巾
Face Towel 小手巾
Mitten & Bootees 婴儿手脚套

Nursing / Breast Feed 哺乳
Breast Pump吸乳器
Nipple/ Breast Shells 护乳奶嘴
Nursing Bra哺乳胸罩
Nipple Cream 护乳霜
Breast Pad防溢乳垫
Nursing Pillow 哺乳枕
Breast Milk Storage Kits 母乳保存用品

Cooler Bag 母乳冷冻袋
Milk Storage Bag 母乳保存袋
Ice Gel Pack 冰冻包
Milk Storage Bottle 母乳保存瓶

Feeding 哺喂
Feeding Bottle 奶瓶
Milk Powder Container 奶粉盒
Pacifier / Teat 安抚奶嘴 / 奶嘴
Bottle Warmer 温奶壶
Baby Food Feeder (4 months above)婴儿食物哺喂器 (4个月以上)

Cleaning Equipment 清洗用具

Bottle Sterilizer 消毒器
Bottle Cleanser Liquid奶瓶清洗液
Bottle Drying Rack奶瓶干燥架
Bottle / Teats Brush 奶瓶/奶嘴清刷

Outfit 服饰
Baby Outfit 婴儿服饰
Binder 护脐带
Mitten & Bootees手套和脚套
Baby Bibs 口水巾
Baby Swaddle 婴儿襁褓
Face Towel 手帕/面巾
Bonnet & Hat 绒毛帽子
Socks & Shoes 鞋袜
Laundry 洗濯

Baby Detergent Powser/Liquid婴儿洗衣粉/
Baby Clothes Softeners 婴儿衣物柔软剂

Diapering 卫生用品
Napkin可洗尿布
Napkin Liner 尿布护疹垫
Cloth Diaper 布尿布
Safety Pin 尿布扣针
Diaper Pants尿布裤
Snappi Fasteners 尿布钩
Disposable Diaper 一次性尿布
Changing Mat 尿布垫
Diaper Insert布尿布隔尿垫
Nappy Cream护臀霜

Bathing & Health Care 沐浴及护理
Baby Toiletries 婴儿沐浴用品
Bath Tub Net 婴儿沐浴网
Bath Towel 浴巾
Bath Thermometer 沐浴温度计
Bath Tub & Non-Slip Mat 沐浴盆&防滑垫
Powder Puffs/Container粉拍/粉盒
Health Care护理用品

Cotton Buds/Wool 棉棒/棉花
Nail Clipper/Scissors 指甲钳/剪刀
Nasal Aspirator吸鼻器
Baby Oral Care Kit婴儿口腔护理


Bedding 寝具
Mattress Set 床套
Baby Cot Sheet/ Rubber Sheet 隔尿垫
Pillow & Bolster枕头&抱枕
Bumper Pad 婴儿床护围
Blankets/ Comforter被单
Bedrail (Optional) 婴儿床栏
Bedroom Furniture卧室家具
Baby Cot/ Playpen 婴儿木床/游戏床
Spring Cot & Net 摇篮架 &
Bouncer/ Rocking Chair婴儿摇椅
Mosquito Net & Stand蚊帐 & 蚊帐架


Travelling Necessities宝宝出门用品
Mama Bag 妈妈包
Baby Car Seat/ Booster Seat汽车安全座椅
Sling or Baby Carrier 婴儿背袋
Bottle Warmer奶瓶保温袋
Stroller/ Buggy婴儿推车
Water Flask热水壶
Baby Moses Basket 婴儿提篮


Miscellaneous其他
Baby Safety婴儿安全必备
Baby Training 婴儿训练必备
Baby Listening Monitor婴儿监视器
Training Mug/ Cup训练杯
Door Stopper/ Cabinet Lock/ Corner Cushion
门吸/柜锁/角垫
Baby Teether婴儿咬牙器
Safety Stair Gate
安全护栏
Baby Mealtime Set 婴儿餐套
Baby Health Care Kit 婴儿护理工具包
Baby Training Potty/ Potty Seat婴儿便壶/马桶座

Baby Walker婴儿学步车