Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/github/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 尝试使用GCM将推送通知从windows服务发送到IOS设备_C#_Ios_Notifications_Google Cloud Messaging - Fatal编程技术网

C# 尝试使用GCM将推送通知从windows服务发送到IOS设备

C# 尝试使用GCM将推送通知从windows服务发送到IOS设备,c#,ios,notifications,google-cloud-messaging,C#,Ios,Notifications,Google Cloud Messaging,你好 我想和大家分享一下我现在面临的推送通知问题,我有两个移动应用程序,一个是Android,另一个是IOS。我需要发送推送通知,这取决于数据库信息。对于Android应用程序,我使用了以下代码: public string SendNotification(string deviceId, string message) { string GoogleAppID = "AIzaSyBvDvWXzvY38VyZN8M67YASQWK27ew7pY8"; v

你好

我想和大家分享一下我现在面临的推送通知问题,我有两个移动应用程序,一个是Android,另一个是IOS。我需要发送推送通知,这取决于数据库信息。对于Android应用程序,我使用了以下代码:

public string SendNotification(string deviceId, string message)
    {
        string GoogleAppID = "AIzaSyBvDvWXzvY38VyZN8M67YASQWK27ew7pY8";
        var SENDER_ID = "305958970287";
        var value = message;
        WebRequest tRequest;
        tRequest = WebRequest.Create("https://android.googleapis.com/gcm/send");
        tRequest.Method = "post";
        tRequest.ContentType = " application/x-www-form-urlencoded;charset=UTF-8";
        tRequest.Headers.Add(string.Format("Authorization: key={0}", GoogleAppID));

        tRequest.Headers.Add(string.Format("Sender: id={0}", SENDER_ID));

        string postData = "collapse_key=score_update&time_to_live=108&delay_while_idle=1&data.message=" + value + "&data.time=" + System.DateTime.Now.ToString() + "&registration_id=" + deviceId + "";
        Console.WriteLine(postData);
        Byte[] byteArray = Encoding.UTF8.GetBytes(postData);
        tRequest.ContentLength = byteArray.Length;

        Stream dataStream = tRequest.GetRequestStream();
        dataStream.Write(byteArray, 0, byteArray.Length);
        dataStream.Close();

        WebResponse tResponse = tRequest.GetResponse();

        dataStream = tResponse.GetResponseStream();

        StreamReader tReader = new StreamReader(dataStream);

        String sResponseFromServer = tReader.ReadToEnd();

        tReader.Close();
        dataStream.Close();
        tResponse.Close();
        return sResponseFromServer;
    }
这段代码运行得非常好,我收到了设备中的所有推送通知

要将GCM用于IOS应用程序,请遵循中描述的所有步骤:

发送通知的代码与android应用程序完全相同,唯一的更改当然是API键和sender参数

问题是,对于IOS应用程序,我没有收到通知,当我运行windows服务时,我从StreamReader tReader获得了此通知:

“id=0:1453902236602583%46569b4366d6cf16”
。我没有在代码中得到任何异常,一切似乎都正常,但它不工作

注意:我没有在代码中得到任何错误和任何异常

在IOS应用程序中,我得到了注册令牌和我需要的一切,因此我认为所有配置过程都是正确的

IOS代码:

 // [START_EXCLUDE]
    // Configure the Google context: parses the GoogleService-Info.plist, and initializes
    // the services that have entries in the file
    var configureError:NSError?
    GGLContext.sharedInstance().configureWithError(&configureError)
    assert(configureError == nil, "Error configuring Google services: \(configureError)")
    gcmSenderID = GGLContext.sharedInstance().configuration.gcmSenderID
    print(gcmSenderID!)
    // [END_EXCLUDE]
    // Register for remote notifications
    let settings: UIUserNotificationSettings =
    UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil)
    application.registerUserNotificationSettings(settings)
    application.registerForRemoteNotifications()

    // [END register_for_remote_notifications]
    // [START start_gcm_service]
    let gcmConfig = GCMConfig.defaultConfig()
    gcmConfig.receiverDelegate = self
    GCMService.sharedInstance().startWithConfig(gcmConfig)
    // [END start_gcm_service]
正在获取注册令牌:

func registrationHandler(registrationToken: String!, error: NSError!) {
    if (registrationToken != nil) {
        self.registrationToken = registrationToken
        let preferences = NSUserDefaults.standardUserDefaults()
        let forKeyPath = SESSION_DATA + "_NOTIFICATIONTOKEN"
        _ = preferences.setObject(registrationToken, forKey: forKeyPath)
        _ = preferences.synchronize()
        print("Registration Token: \(registrationToken)")
        self.subscribeToTopic()
        let userInfo = ["registrationToken": registrationToken]
        NSNotificationCenter.defaultCenter().postNotificationName(
            self.registrationKey, object: nil, userInfo: userInfo)
    } else {
        print("Registration to GCM failed with error: \(error.localizedDescription)")
        let userInfo = ["error": error.localizedDescription]
        NSNotificationCenter.defaultCenter().postNotificationName(
            self.registrationKey, object: nil, userInfo: userInfo)
    }
}
APN测试结果:

2016-02-02 15:07:43+0000:加载的文档aps_development.cer 2016-02-02 15:07:50+0000:已连接到服务器 gateway.sandbox.push.apple.com 2016-02-02 15:07:50+0000:设置SSL 连接2016-02-02 15:07:50+0000:设置对等域名 gateway.sandbox.push.apple.com 2016-02-02 15:07:50+0000:Keychain 打开2016-02-02 15:07:50+0000:苹果的证书数据 开发IOS推送服务:com.jperera.RapidSentryMaster 已成功初始化2016-02-02 15:07:50+0000:Sec标识 已创建2016-02-02 15:07:50+0000:已创建客户端证书 2016-02-02 15:07:58+0000:已连接2016-02-02 15:07:58+0000: 令牌:2016-02-02 15:07:58 +0000:写入92字节,将数据发送到gateway.sandbox.push.apple.com:2195 2016-02-02 15:07:58+0000: 已断开与服务器网关的连接。sandbox.push.apple.com:2195


我将非常感谢您在这方面的任何帮助

啊哈!你得到的答复对我来说很熟悉

当我用HTTP
POST
调用GCM发送一些JSON时,我会得到这个JSON响应<代码>{“多播id”:5557NNN163626712336,“成功”:1,“失败”:0,“规范id”:0,“结果”:[{“消息id”:“0:1451239821391315%e0bf69ABC9fd7ecd”}
…最后一部分看起来与您得到的响应相同

我相信这是GCM告诉您一切正常,这是您可以在GCM诊断工具中使用的
消息ID
(Google Play开发者控制台的一部分)。您是否从您的web请求中得到任何标题,告诉您
多播\u id
或成功/失败的次数

此工具可能会显示您的邮件发生了什么。这并不完美,但可以帮上忙。也许GCM认为您的信息发送正常,但后来在苹果手中,在到达您的iOS设备之前失败了


您可能需要注意的一件事是,GCM有两种配置有效负载的方法(我使用JSON做什么,您使用
&data.message=“+value
)您可能想尝试使用
通知来代替。请参阅“有效负载”部分,我承认我还没有完全理解!

啊哈!您得到的响应对我来说很熟悉

当我用一个HTTP
POST
调用GCM来发送一些JSON时,我得到了这个JSON响应…
{“multicast_id”:5557NNN163626712336,“success”:1,“failure”:0,“canonical_id”:0,“results”:[{“message_id”:“0:1451239821391315%e0bf69ABC9fd7ecd”}

…最后一部分看起来与您得到的响应相同

我相信这是GCM告诉您一切正常,这是您可以在GCM诊断工具中使用的
消息ID
(Google Play开发者控制台的一部分)。您是否从web请求中得到任何标题,告诉您
多播ID
或成功/失败的次数

此工具可能会揭示您的消息发生了什么。它并不完美,但可以提供帮助。也许GCM认为您的消息发送正常,但后来在苹果手中,在到达您的iOS设备之前失败了


您可能需要注意的一件事是,GCM有两种配置有效负载的方法(我使用JSON所做的,您使用
&data.message=“+value
)所做的),您可能需要尝试使用
通知
。请参阅“有效载荷”部分,我承认我还没有完全理解

那是messageId吗?是否有其他对社区有帮助的信息?你能通过Google Play服务开发者控制台查看该条目是否存在吗?如果它的GCM相关,它可以在那里有messageId和为什么iOS应用程序似乎没有收到通知的状态。希望这有帮助@adjuremods非常感谢您的回答,我将检查GCM诊断,并会让您知道。@adjuremods,我无法通过google play控制台检查GCM诊断,因为该应用程序是一个IOS应用程序,无法上载到google play。在安卓系统中,一切正常,问题在于IOS应用程序。我认为GCM diagnostics应该仍然能够提供对您的问题有用的信息,因为IOS应用程序只是一个客户端。如果您已经注册了客户端应用程序,据我所知,它应该在诊断上显示MessageID和状态。如果不是这样,可能是APNS问题。@JoseRaulPerera您的C#代码如何找到证书(您从苹果获得的)?或者你是用另一种方式把它上传到谷歌上的?那是messageId吗?是否有其他对社区有帮助的信息?你能通过Google Play服务开发者控制台查看该条目是否存在吗?如果其GCM相关