【问题标题】:Windows Azure Service Bus and GCM Push NotificationsWindows Azure 服务总线和 GCM 推送通知
【发布时间】:2015-02-07 20:16:45
【问题描述】:

这个问题很长而且很复杂,但我希望有人能帮帮我。我正在编写一个需要发送和接收推送通知的 Phonegap 应用程序。我想为此使用 Azure 服务总线,以便可以部署到多个移动平台。现在我只是在测试Android。我已经设置了我的 Google 和 Azure 帐户,并且下面的代码可以正常工作。当我在我的设备上部署我的应用程序时,onGcmNotification 被触发并且我从谷歌获得了一个 regid。然后,我使用提供的 regid 和模板进行注册,之后我收到一个警报(“Registered With Hub!”),所以我认为一切正常。但是,通过 Azure 调试控制台进行测试时,我无法获得任何推送通知。见下图。它只是一直告诉我“未找到所选平台的注册”。我部署的应用程序上没有弹出任何内容,并且永远不会触发 onGcmNotification。我错过了什么?还有其他方法可以完成我想要做的事情吗?

var GCM_SENDER_ID = 'XXXXXXXXXXXXX';
    var MOBILE_SERVICE_URL = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
    var MOBILE_SERVICE_APP_KEY = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
    var mobileServiceClient;
    var pushNotification;

    var app = {
        initialize: function () {
            this.bindEvents();
        },
        bindEvents: function () {
            document.addEventListener('deviceready', this.onDeviceReady, true);
        },

        onDeviceReady: function () {
            angular.element(document).ready(function () {

                mobileServiceClient = new WindowsAzure.MobileServiceClient(MOBILE_SERVICE_URL, MOBILE_SERVICE_APP_KEY);

                // Create a pushNotification (from the PushPlugin).
                pushNotification = window.plugins.pushNotification;

                // Platform-specific registrations.
                if (device.platform == 'android' || device.platform == 'Android') {
                    // Register with GCM for Android apps.
                    pushNotification.register(successHandler, errorHandler,
                        {
                            "senderID": GCM_SENDER_ID,
                            "ecb": "onGcmNotification"
                        });
                } else if (device.platform === 'iOS') {
                    // Register with APNS for iOS apps.
                    pushNotification.register(tokenHandler, errorHandler,
                        {
                            "badge": "false",
                            "sound": "false",
                            "alert": "true",
                            "ecb": "onApnsNotification"
                        });
                }

                angular.bootstrap(document, ['app']);
            });
        },
    };


    // Handle a GCM notification.
    function onGcmNotification(e) {
        switch (e.event) {
            case 'registered':
                // Handle the registration.
                if (e.regid.length > 0) {
                    console.log("gcm id " + e.regid);

                    if (mobileServiceClient) {

                        // Create the integrated Notification Hub client.
                        var hub = new NotificationHub(mobileServiceClient);

                        // Template registration.
                        var template = "{ \"data\" : {\"message\":\"$(message)\"}}";

                        // Register for notifications.
                        // (gcmRegId, ["tag1","tag2"], templateName, templateBody)
                        hub.gcm.register(e.regid, null, "myTemplate", template).done(function () {
                            alert("Registered with hub!");
                        }).fail(function (error) {
                            alert("Failed registering with hub: " + error);
                        });
                    }
                }
                break;

            case 'message':
                console.log("received message: " + e.message);
                if (e.foreground) {
                    alert(e.payload.message);
                }
                break;

            case 'error':
                alert('GCM error: ' + e.message);
                break;

            default:
                alert('An unknown GCM event has occurred');
                break;
        }
    }

【问题讨论】:

标签: cordova phonegap-plugins azure-mobile-services


【解决方案1】:

我在 Azure 上遇到了同样的结果。推送通知对我有用,但在 Android 上。通过让我的移动服务在成功插入时发送通知,我第一次收到通知工作。

在 Azure 门户中,在您使用的移动服务中,转到数据->表格->脚本->插入,然后在插入成功时使用此脚本发送推送通知。

function insert(item, user, request) {
// Define a payload for the Google Cloud Messaging toast notification.
var payload = {
    data: {
        message: '{ "message" : "New item added: ' + item.text + '" }'; 
    }
};      
request.execute({
    success: function() {
        // If the insert succeeds, send a notification.
        push.gcm.send(null, payload, {
            success: function(pushResponse) {
                console.log("Sent push:", pushResponse, payload);
                request.respond();
                },              
            error: function (pushResponse) {
                console.log("Error Sending push:", pushResponse);
                request.respond(500, { error: pushResponse });
                }
            });
        },
    error: function(err) {
        console.log("request.execute error", err)
        request.respond();
    }
  });
}

现在所有已注册的设备都应在将某些内容插入表时收到通知,即使这些设备未显示在 Azure 的通知中心。

您还可以使用 Node 库从本地计算机发送通知:

https://github.com/ToothlessGear/node-gcm

我使用DevGirl's tutorial 来完成这项工作。让我知道这些东西是否有效。希望有帮助!

PS。我使用与您的代码类似的代码为 iOS 和 Android 推送通知,但我仍然无法从 Azure 通知中心的调试选项卡发送测试通知。

【讨论】:

    【解决方案2】:

    如果您使用的是 mobileServiceClient,则不需要使用 NotificationHub。

    改为调用它。

    mobileServiceClient.push.gcm.registerTemplate(e.regid,"myTemplate", template, null)

    另见这篇文章https://msdn.microsoft.com/en-us/magazine/dn879353.aspx

    【讨论】:

      【解决方案3】:

      我浪费了 2 天的时间尝试使用 Notification Hub 通过 Azure 向我的移动应用程序发送消息,最终我做到了。 请注意在函数 onGcmNotification 中,当您收到注册时,您注册了一个自定义模板。因此,如果您想使用 Azure 发送消息,请在调试选项卡上选择消息类型 自定义模板

      【讨论】:

        猜你喜欢
        • 2014-12-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-03-08
        • 1970-01-01
        • 2014-01-08
        • 2014-10-02
        相关资源
        最近更新 更多