2017-08-03 137 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