从C#应用程序向移动终端发送推送通知

时间:2015-12-29 07:38:28

标签: c# .net amazon-web-services google-cloud-messaging amazon-sns

我正在使用Amazon SNS发送通知。目前我已经意识到我可以将消息发布到某个主题。但我需要使用GCM直接向Android设备发送推送通知。有人可以帮我用C#代码实现吗? 这是我将消息发布到主题的代码:

private IAmazonSimpleNotificationService FAmazonSNS = null;


    public void Connect(string AAccessKey, string ASecretKey, bool AUseHTTP, string ARegion, int ATimeoutInMSec, int AReadWriteTimeoutInMSec, bool AConnectToSQS, bool AConnectToS3, bool AConnectToSNS)
    {
        AWSCredentials vAWSCredentials = new BasicAWSCredentials(AAccessKey, ASecretKey);

        if ((AConnectToSNS) && (FAmazonSNS == null))
        {
            AmazonSimpleNotificationServiceConfig vAmazonSNSConfig = new AmazonSimpleNotificationServiceConfig();
            vAmazonSNSConfig.UseHttp = AUseHTTP;
            vAmazonSNSConfig.RegionEndpoint = RegionEndpoint.GetBySystemName(ARegion);

            if (ATimeoutInMSec != 0)
            { vAmazonSNSConfig.Timeout = TimeSpan.FromMilliseconds(ATimeoutInMSec); }

            if (AReadWriteTimeoutInMSec != 0)
            { vAmazonSNSConfig.ReadWriteTimeout = TimeSpan.FromMilliseconds(AReadWriteTimeoutInMSec); }

            FAmazonSNS = AWSClientFactory.CreateAmazonSimpleNotificationServiceClient(vAWSCredentials, vAmazonSNSConfig);
        }
    }

public bool SNS_Publish(string ATopicARN, string ASubject, string AMessage, out int AStatusCode, out string AErrorCode, out string AErrorMessage)
    {
        try
        {
            PublishRequest vPublishRequest = new PublishRequest();
            vPublishRequest.TopicArn = ATopicARN;
            vPublishRequest.Subject = ASubject;
            vPublishRequest.Message = AMessage;

            //vPublishRequest.TargetArn

            PublishResponse vPublishResponse = FAmazonSNS.Publish(vPublishRequest);

            AStatusCode = (int)vPublishResponse.HttpStatusCode;
            AErrorCode = "";
            AErrorMessage = "";
            return (vPublishResponse.HttpStatusCode == System.Net.HttpStatusCode.OK);
        }
        catch (AmazonSimpleNotificationServiceException ex)
        {
            AStatusCode = (int)ex.StatusCode;
            AErrorCode = ex.ErrorCode;
            AErrorMessage = ex.Message;
            return false;
        }
        catch (Exception e)
        {
            AStatusCode = 0;
            AErrorCode = ""; //e.InnerException.Message;
            AErrorMessage = e.Message; // A WebException with status Timeout was thrown
            return false;
        }

    } 

所以对我来说绝对不清楚如何设置“服务器API密钥”,“设备令牌”,这是向移动终端发送通知所需的。

对于java我发现的例子如何做但是对于C#不是(

0 个答案:

没有答案