0

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.

Community
  • 1
  • 1
Yahya Uddin
  • 26,997
  • 35
  • 140
  • 231

3 Answers3

1

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.

Community
  • 1
  • 1
Yahya Uddin
  • 26,997
  • 35
  • 140
  • 231
0

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

  • That's not the issue. The device token is a global variable is fine here. In fact it even subscribed fine as well as it's on the device list – Yahya Uddin May 03 '17 at 09:35
  • I think the issue is where I got my credentials as the documentation seems out of date. Where did you get yours? – Yahya Uddin May 03 '17 at 09:56
  • @YahyaUddin This is a snip from the notes I took when I worked this out last year - Create a Google API Project The first step is creating a Google API project and enabling its GCM service. You also need to obtain a GCM sender ID and create a Google API server key. create a new project from here : https://console.developers.google.com/project GO TO API Manager Credentials - create API Key - copy it and paste it into the Arrow dashboard click hamburger menu - IAM & Admin Settings - copy the project number - paste that into Arrow dashboard Is that what you're referring to? – Jonathan Wheat May 03 '17 at 16:20
  • Please look at my related question here to see what I did wrong: http://stackoverflow.com/questions/43757524/appcelerator-titanium-getting-android-credentials-to-push-notifications . Thanks so much. – Yahya Uddin May 03 '17 at 16:22
  • It seems that the Google changed some things around! – Yahya Uddin May 03 '17 at 16:23
  • I posted the notes over there as well, looks like you were close, just create an API Key instead of an OAuth Client ID. – Jonathan Wheat May 03 '17 at 16:42
0

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

  • The type 'gcm' is not mentioned anywhere in the docs. None the less I tried it and it didn't change anything. The error is still there. I really think the issue is where I got my credentials. How did you get yours as the docs are really out of date, – Yahya Uddin May 03 '17 at 15:45
  • You can find it on https://console.cloud.google.com/home/dashboard. You have to create a Project (and you'll have the Sender ID), and then have to create a new Credential for App Android (this will be your API key) – AstrovicApps May 03 '17 at 16:51
  • Please see my related question here: http://stackoverflow.com/questions/43757524/appcelerator-titanium-getting-android-credentials-to-push-notifications . Things have changed on both Appcelerator and google ends – Yahya Uddin May 03 '17 at 17:09
  • It's really strange. I'm using it in different android app, and I have not changed this configuration for more than two years, and it still works correctly. – AstrovicApps May 03 '17 at 22:38
  • Perhaps google wants to keep backwards compatibility. Can you try with a new demo app? – Yahya Uddin May 03 '17 at 22:42