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

标签 php cordova azure push-notification azure-notificationhub

对于用户要求,后端必须是 PHP 并且应用程序客户端采用 Ionic 2。基于:

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的xml格式

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>';

 }

我在 Ionic 2 应用程序中使用此函数获取了 GcmRegistrationId。

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 表示“请求正文无效。由于请求格式错误,无法创建注册。”。我不明白为什么会发生这种情况。

最佳答案

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

关于php - 使用 PHP 后端在通知中心注册,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45473589/

相关文章:

ios - 在 IOS 上重新安装 ionic 应用程序后显示 Angular 标(Badge)通知

azure - 如何在标准 Azure 网站实例上托管多个网站,这是 Web 托管计划吗?

javascript - PhoneGap Ajax 请求在使用 AT&T 数据时返回 502(错误的网关),但不使用 WiFi

将证书添加到 Key Vault 时出现 Azure 错误

azure - 如何在特定资源组上添加标签?

php - 既然所有错误都是异常,PHP7 上的 set_error_handler() 会发生什么?

php - Paypal IPN [仅信用卡交易失败]

php - PHP本地服务器-两次错误-错误还是没有?

php - 如何编写我自己的 Smarty If-case

android - 无法使用 Cordova FileTransfer 上传文件