dosrmo0442 2017-08-03 01:57
浏览 66
已采纳

使用PHP后端在通知中心注册

For user requeriments the backend must be PHP and the app client is in Ionic 2. Based on:

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 Important

I created this method in 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);

The getXmlAndroid method is simple return xml format with 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>';

 }

I get the GcmRegistrationId with this function in Ionic 2 app.

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);
});

The problem is always request to Registration method in Notification API return

[http_code] => 400

Where 400 means the "Invalid request body. The registration could not be created because the request was malformed.". I don't understand why this happen.

  • 写回答

1条回答 默认 最新

  • douyou2732 2017-08-07 18:17
    关注

    Why you send Content-Length: 0 header? It may cause a problem.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀