2017-08-03 141 views
14

对于用户通知中心requeriments后端必须是PHP和应用程序客户端在离子2.根据:注册与PHP后端

https://msdn.microsoft.com/en-us/library/azure/dn223265.aspx https://github.com/Azure/azure-notificationhubs-samples/tree/master/notificationhubs-rest-php https://github.com/webwarejp/notificationhubs-rest-php

Create Registration Notification Hub Azure PHP重要

我在php的API中创建了这个方法:

$uri = $this->endpoint . $this->hubPath . "/registrations".NotificationHub::API_VERSION; 
    /* print($uri); */ 
    $ch = curl_init($uri); 
    $token = $this->generateSasToken($uri); 
    $headers = [ 
     'Authorization: '. $token, 
     'Content-Type: '."application/atom+xml;type=entry;charset=utf-8", 
     'x-ms-version: 2015-01', 
     'Content-Length: 0' 
    ]; 
    $body = $this->getXmlAndroid($registrationId, $tag); 
    print_r($body); 
    curl_setopt_array($ch, array(
     CURLOPT_CUSTOMREQUEST => 'POST', 
     CURLOPT_RETURNTRANSFER => TRUE, 
     CURLOPT_SSL_VERIFYPEER => FALSE, 
     CURLOPT_HTTPHEADER => $headers, 
     CURLOPT_POSTFIELDS => $body 
    )); 
    $response = curl_exec($ch); 
     // Check for errors 
    if($response === FALSE){ 
     print_r(curl_error($ch)); 
     throw new Exception(curl_error($ch)); 
    } 

    $info = curl_getinfo($ch); 
    print_r($info); 
    curl_close($ch); 

的getXmlAndroid方法与GCM ID

private function getXmlAndroid($registrationId){ 
    return '<?xml version="1.0" encoding="utf-8"?> 
      <entry xmlns="http://www.w3.org/2005/Atom"> 
       <content type="application/xml"> 
        <GcmRegistrationDescription xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.microsoft.com/netservices/2010/10/servicebus/connect"> 
         <GcmRegistrationId>'.$registrationId.'</GcmRegistrationId> 
        </GcmRegistrationDescription> 
       </content> 
      </entry>'; 

} 

简单的返回的XML格式,我得到了GcmRegistrationId与离子2应用此功能。

import { Push, PushObject, PushOptions } from '@ionic-native/push'; 
.... 
const options: PushOptions = { 
    android: { senderID: 'MyIDFirebaseProject'}, 
    ios: { alert: 'true', badge: true, sound: 'false'}, 
    windows: {} 
}; 
pushObject.on('registration').subscribe((registration: any) => { 
console.log(registration.registrationId); 
}); 

问题总是请求登记方法在通知API返回

[http_code] => 400 

凡400表示“无效的请求的身体。登记无法创建因为请求格式错误”。 。我不明白为什么会发生这种情况。

回答

6

为什么发送Content-Length:0头?这可能会导致问题。

+0

是的,这是愚蠢的问题,这个头是不必要的。 – CampDev