简体   繁体   English

Appcelerator / Titanium:无法将推送通知发送到Android

[英]Appcelerator/ Titanium: Cannot send push notification to Android

I want to be able to send push notifications using Titanium and Arrow Push on Android. 我希望能够使用Android上的Titanium和Arrow Push来发送推送通知。

I have followed the instructions here: 我已按照此处的说明进行操作:

Configuring Push Services 配置推送服务

Subscribing to push notifications 订阅推送通知

Modules.CloudPush 模块.CloudPush

My simple code looks as follows: 我的简单代码如下所示:

var CloudPush = require('ti.cloudpush');
var deviceToken = null;

// Works fine
CloudPush.retrieveDeviceToken({
    success: function () {
        deviceToken = e.deviceToken;
        alert('deviceToken: ' + deviceToken);
        subscribeToChannel();
    },
    error: function () {
        alert('Failed to register for push notifications! ' + e.error);
    }
});

// Never runs!!!
CloudPush.addEventListener('callback', function (evt) {
    Ti.API.info('New notification!');
    alert("Notification received: " + evt.payload);
});

// Works fine
function subscribeToChannel () {
    Cloud.PushNotifications.subscribeToken({
        device_token: deviceToken,
        channel: 'general',
        type: Ti.Platform.name
    }, function (e) {
        if (e.success) {
            alert('Subscribed');
        } else {
            alert('Error:\n' + ((e.error && e.message) || JSON.stringify(e)));
        }
    });
}

Most of the above code is similar to the docs. 上面的大多数代码与文档相似。 The subscription aspect of the code seems to works perfectly fine, as the user's device also appears in the devices section of the Appcelerator Dashboard. 该代码的订阅方面似乎工作得很好,因为用户的设备也出现在Appcelerator仪表板的device部分中。

However when it comes to sending a notification, from the Appcelerator Dashboard, the word "Failure" appears next to my Android device. 但是,当要从Appcelerator仪表板发送通知时,Android设备旁边会出现“失败”字样。

推送通知时出错

The full error message when highlighting the "?" 突出显示“?”时的完整错误消息 icon is as follows: 图标如下:

Exception Type: GCM; 异常类型:GCM; Error Code: 3103; 错误代码:3103; Error Message: RegistrationId(s) is null or empty; 错误消息:RegistrationId为空或为空; Catched Exception: argument cannot be null 捕获的异常:参数不能为空

I looked this error up on http://docs.appcelerator.com/arrowdb/latest/#!/guide/troubleshooting and all it says is: 我在http://docs.appcelerator.com/arrowdb/latest/#!/guide/疑难解答中查找了此错误,它的意思是:

The GCM client provided a null or empty registration ID. GCM客户端提供了一个空或空的注册ID。 This error is uncommon if you are using the Modules.CloudPush module. 如果使用的是Modules.CloudPush模块,此错误很少见。

Which isn't helpful. 这没有帮助。

What am I doing wrong? 我究竟做错了什么? Is this a bug on Accelerator side. 这是Accelerator方面的错误吗?

Turns out my code was fine. 原来我的代码很好。 The credentials I was using however was incorrect. 我使用的凭据不正确。 Please see my other related question here: 请在这里查看我的其他相关问题:

Appcelerator/ Titanium: Getting Android credentials to push notifications Appcelerator / Titanium:获取Android凭据以推送通知

The docs are in need of updating. 该文档需要更新。

I'm hardly an expert with push, but I compared what you have to what I have in one of my apps. 我不是推送专家,但我将您拥有的东西与我的一个应用程序中的东西进行了比较。

Pretty sure you need to send the deviceToken into the subscribeToChannel function. 非常确定您需要将deviceToken发送到subscriptionToChannel函数中。

Try changing this - 尝试更改此-

function subscribeToChannel () {

to this - 为此-

function subscribeToChannel (deviceToken) {

then add the token to the call here - 然后将令牌添加到此处的呼叫中-

subscribeToChannel (deviceToken);

Let me know if that works for you. 让我知道这是否适合您。

-Jon -乔恩

On subscribeToChannel() function, you should use type : 'gcm' instead of type: Ti.Platform.name subscribeToChannel()函数上,应使用type : 'gcm'而不是type: Ti.Platform.name

This is a commonJS module that I created for my Android push: 这是我为Android push创建的commonJS模块:

function ACSPush(_callback) {

    var debug_mode = true;
    var Cloud = require('ti.cloud');        
    var CloudPush = require('ti.cloudpush');
    CloudPush.enabled = true; 
    var deviceToken;

    CloudPush.retrieveDeviceToken({
        success : function deviceTokenSuccess(e) {
            if(debug_mode)
                Ti.API.info('Device Token: ' + e.deviceToken);
            deviceToken = e.deviceToken;
            if(Ti.App.Properties.getString("deviceToken") != deviceToken.toString()){
                defaultSubscribe();
            };
        },
        error : function deviceTokenError(e) {
            if(debug_mode)
                Ti.API.info('deviceTokenError.. :( ' + e.error);
        }
    });

    function defaultSubscribe() {
        Cloud.PushNotifications.subscribeToken({
            channel : 'MyChannel',
            device_token : deviceToken,
            type : 'gcm'
        }, function(e) {
            if(e.success) {
                if(debug_mode)
                    Ti.API.info("Success registerForPushNotifications");
                Ti.App.Properties.setString("deviceToken", deviceToken.toString());
            } else {
                if(debug_mode)
                    Ti.API.info('Error:\n' + ((e.error && e.message) || JSON.stringify(e)));
            };
        });
    };

    CloudPush.addEventListener('callback', function(evt) {
        var payload = JSON.parse(evt.payload);
        if(debug_mode){
            Ti.API.info("Received a push notification\nPayload:\n" + JSON.stringify(evt.payload));
            Ti.API.info("payload: " + payload);
        };
        _callback(payload);
    });
    CloudPush.addEventListener('trayClickLaunchedApp', function(evt) {
        if(debug_mode)
            Ti.API.info('Tray Click Launched App (app was not running)');
    });
    CloudPush.addEventListener('trayClickFocusedApp', function(evt) {
        if(debug_mode)
            Ti.API.info('Tray Click Focused App (app was already running)');
    });

};

module.exports = ACSPush;

Obviously, you must first configure Android Push Service http://docs.appcelerator.com/platform/latest/#!/guide/Configuring_push_services-section-src-37551713_Configuringpushservices-ConfiguringpushservicesforAndroiddevices 显然,您必须首先配置Android Push Service http://docs.appcelerator.com/platform/latest/#!/guide/Configuring_push_services-section-src-37551713_Configuringpushservices-ConfiguringpushservicesforAndroiddevices

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM