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 serviceprivate static string serviceType = "APNS"; //APNS or GSM//message typeprivate static string title = "Message from .Net";private static string desc = "Hello world";private static string notiType = "newEmployee"; //is custom value to send to appprivate static string employeeId = "001";private static string targetId = "12";//androidprivate static string destinationNumber = "The phone token";private static string rawMsg = "{\"notification\":{\"title\":\"" + title + "\",\"body\":\"" + desc + "\"},\"data\":{\"notiType\":\"" + notiType + "\",\"employeeID\":\"" + employeeId + "\",\"targetID\":\"" + targetId + "\"}}"; //json string//iosprivate 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 onlyPNMessage().Wait();}private static void WriteProfile(string profileName, string keyId, string secret){var chain = new CredentialProfileStoreChain();CredentialProfile basicProfile;if (chain.TryGetProfile("default", out basicProfile)){// Use defaultConsole.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
9. Execute the program.
No comments:
Post a Comment