4

I've set up the push notification plugin and am calling the register method, but it's only returning a string "OK" instead of a device id. How do I get the registered device id?

   $window.plugins.pushNotification.register(
          function (result) {
            q.resolve(result); //this always just returns the string "OK", how do I get the device ID?
          },
          function (error) {
            console.log(error);
            q.reject(error);
          },
          config);

        return q.promise;
      },

e.regid is null, taken from this example

// Android and Amazon Fire OS
function onNotification(e) {
    $("#app-status-ul").append('<li>EVENT -> RECEIVED:' + e.event + '</li>');

    switch( e.event )
    {
    case 'registered':
        if ( e.regid.length > 0 )
        {
            $("#app-status-ul").append('<li>REGISTERED -> REGID:' + e.regid + "</li>");
            // Your GCM push server needs to know the regID before it can push to this device
            // here is where you might want to send it the regID for later use.
            console.log("regID = " + e.regid);
        }
    break;

    case 'message':
        // if this flag is set, this noti
4

3 回答 3

5

所以我只是误解了推送库中的回调。iOS 上的 register 函数回调返回一个字符串令牌,而在 Android 中它只返回一个成功处理程序。Android 版本中注册事件的令牌 id 必须在推送通知 ecb 处理程序上处理,而不是在注册回调中处理。

所以在他们的例子中

if ( device.platform == 'android' || device.platform == 'Android' || device.platform == "amazon-fireos" ){
    pushNotification.register(
    successHandler,
    errorHandler,
    {
        "senderID":"replace_with_sender_id",
        "ecb":"onNotification"
    });
} 

该函数在配置属性“ecb”中设置为“onNotification”,而不是在成功处理程序中。

在这种情况下,您可以使用原始问题中的 onNotification 示例。我的错误是将 onNotification 函数作为 Android 中的成功处理程序传递,事实并非如此。

于 2014-11-30T14:50:05.577 回答
1

在android中,当设备注册事件被触发时,可以从ecb回调中获取设备令牌。这段代码说明了我是如何做到的(它可能更有条理,但对于我们的目的来说它就足够了):

var GOOGLE_SENDER_ID = '/* replace with yours */';
var deferred;

function registerWithGCMServer() {
    deferred = $.Deferred();

    window.plugins.pushNotification.register(
        function() {
            //It will be resolved in 'window.onAndroidNotification'
            //when the device is registered and the token is gotten
            //our rejected if the timeout is reached
        },
        function() {
            deferred.reject('Error registering.');
        }, {
            senderID: GOOGLE_SENDER_ID,
            ecb: 'window.onAndroidNotification'
        });

    setTimeout(function() {
        if(deferred.state() === 'pending') {
            deferred.reject('Error registering (timeout).');
        }
    }, 10000); //10s

    return deferred.promise();
}

window.onAndroidNotification = function(e) {
    if(e.event == 'registered') {
        deferred.resolve(e.regid);
    } else if(e.event == 'message') {
        onMessageRecived.call(null, e.message);
    }
};

然后使用它的功能:

registerWithGCMServer()
    .then(function(deviceToken) {
        console.log('Device registered and its token is ' + deviceToken);
    })
    .fail(function(e) {
        console.error(e);
    });

function onMessageRecived(message) {
    console.log('Push message received: ' + message);
}
于 2015-05-14T07:15:24.377 回答
0

我关于注册的错误是使用了不正确的项目 ID。起初,我使用项目的唯一名称(在开发控制台中也称为项目 ID)而不是项目编号。无论 gcm 的响应如何,回调都会成功。

在我的两台三星测试设备上,我也遇到了类似的问题:注册回调工作正常,但应用程序没有收到推送通知。这似乎与我的测试设备上的某些服务已过时有关。

最后,如果您希望通知在系统托盘中正确显示,请确保在有效负载数据中包含“消息”和“标题”。

于 2015-03-03T17:03:28.217 回答