Appcelerator/Titanium。无法向安卓系统发送推送通知[英] Appcelerator/ Titanium: Cannot send push notification to Android

本文是小编为大家收集整理的关于Appcelerator/Titanium。无法向安卓系统发送推送通知的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我希望能够使用钛和箭头推动在Android上发送推送通知.

我遵循了这里的说明:

配置推送服务

订阅推送通知

modules.cloudpush

我的简单代码看起来如下:

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

上面的大多数代码类似于文档.代码的订阅方面似乎完全正常,因为用户的设备也显示在Appcelerator仪表板的设备部分中.

但是,当它到发送通知时,从Appcelerator仪表板上,我的Android设备旁边出现"失败"字样.

错误

突出显示"?"时完全错误消息图标如下:

异常类型:gcm;错误代码:3103;错误信息: 注册表为空或空;捕获的例外:论证不能 是null

我在 http://docs.appcelerator上看起来有这个错误. com/arrowdb/最新/#!/指南/故障排除所有它所说的是:

GCM客户端提供了空或空注册ID.这个错误是 如果您使用的是Modules.Cloudpush模块,则罕见.

它没有有用.

我做错了什么?这是加速器侧的一个错误.

推荐答案

表示我的代码很好.我使用的凭据不正确.请看我的其他相关问题:

appcelerator/titanium:获得Android凭据推送通知

文档需要更新.

其他推荐答案

我几乎不是推动的专家,但我比较了我在我的一个应用程序中的内容.

非常确定您需要将DeviceToken发送到SubscribetoChannel功能.

尝试更改此 -

function subscribeToChannel () {

到此 -

function subscribeToChannel (deviceToken) {

然后将令牌添加到此处的呼叫 -

subscribeToChannel (deviceToken);

让我知道是否适合您.

-jon

其他推荐答案

在subscribeToChannel()函数上,您应该使用type : 'gcm'而不是type: Ti.Platform.name

这是我为我的Android推送创建的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;

显然,您必须先配置Androef Push Service http://docs.appcelerator.com/platform/latest/#!guide/configuring_push_services-section-src -37551713_configuringpushservices-configuringpushservicesforandroiddevice

本文地址:https://www.itbaoku.cn/post/1938230.html

问题描述

I want to be able to send push notifications using Titanium and Arrow Push on Android.

I have followed the instructions here:

Configuring Push Services

Subscribing to push notifications

Modules.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.

However when it comes to sending a notification, from the Appcelerator Dashboard, the word "Failure" appears next to my Android device.

Error on push notifications

The full error message when highlighting the "?" icon is as follows:

Exception Type: GCM; Error Code: 3103; Error Message: RegistrationId(s) is null or empty; 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:

The GCM client provided a null or empty registration ID. This error is uncommon if you are using the Modules.CloudPush module.

Which isn't helpful.

What am I doing wrong? Is this a bug on Accelerator side.

推荐答案

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

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.

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

This is a commonJS module that I created for my Android push:

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