morinel/gcmpush

Success called twice

Opened this issue · 3 comments

m1ga commented

Setup and start register:

var push = require("/push").create({
    senderID: 'SENDER_ID'
});

push.registerPush({
    success: onRegisterPush,
    error: onRegisterError,
    callback: onPushCallback
});

function onRegisterPush(e) {
    if (OS_ANDROID) {

        console.log("SUCCESS PUSH " + JSON.stringify(e));

        api.register({
            pushid: e.registrationId
        }, function(video) {
            // do stuff with the video
        });

        push.subscribe({
            topic: "global",
            success: onSubSuccess
        });
    }

Log:

[INFO]  REGISTER PUSH SENDER_ID
[DEBUG] GCMModule: (KrollRuntimeThread) [2,250] registerPush called
[DEBUG] GCMRegistrar: Resetting backoff for com.name.app
[TRACE] GCMRegistrar: Registering app com.name.app of senders SENDER_ID
[TRACE] GCMRegistrar: Creating pending intent to get package name
[DEBUG] GCMModule: (KrollRuntimeThread) [8,258] get registrationId property
[INFO]  SUCCESS PUSH {"registrationId":"PUSH_ID"}
[DEBUG] GCMModule: (main) [32,290] onPause com.name.app.appActivity@207cb33e (true)
[INFO]  ::RESTE:: CALL_SERVER
[INFO]  SUBSCRIBE PUSH SENDER_ID

[DEBUG] GCMModule: (main) [591,881] onStop com.name.app.appActivity@207cb33e (true)
[TRACE] GCMBroadcastReceiver: onReceive: com.google.android.c2dm.intent.REGISTRATION
[TRACE] GCMBroadcastReceiver: GCM IntentService class: nl.vanvianen.android.gcm.GCMIntentService
[TRACE] GCMBaseIntentService: Intent service name: GCMIntentService--2
[DEBUG] GCMBaseIntentService: handleRegistration: registrationId = PUSH_ID, error = null, unregistered = null
[DEBUG] GCMRegistrar: Resetting backoff for com.name.app
[TRACE] GCMRegistrar: Saving regId on app version 1
[DEBUG] GCMIntentService: (IntentService[GCMIntentService--2]) [745,1626] Registered: PUSH_ID
[INFO]  SUCCESS PUSH {"registrationId":"PUSH_ID"}
[INFO]  ::RESTE:: CALL_SERVER
[INFO]  SUBSCRIBE PUSH SENDER_ID

::RESTE:: CALL_SERVER is called twice because the success event is called twice. Is that normal?
The Push.sender_id is the same so its just an identical server call

Are you maybe calling onRegisterPush from both app start up and app resume?

m1ga commented

no, I don't have start or resume registered. I just call the script once in the app.js for now.
The log "REGISTER PUSH" is only called once, thats what is called in my commonjs module:

exports.create = function(opt) {
    return new Push(opt);
};

function Push(opt) {
    var senderID = opt.senderID;

    if (OS_ANDROID) {
        var gcm = require("nl.vanvianen.android.gcm");
        var lastData = gcm.getLastData();
        if (lastData) {
            Ti.API.info("Last notification received " + JSON.stringify(lastData));
            gcm.clearLastData();
        }
    }

    this.registerPush = function(opt) {
        if (OS_ANDROID) {
            console.log("REGISTER PUSH " + senderID);

            gcm.registerPush({
                senderId: senderID,
                notificationSettings: {
                    smallIcon: 'notification_icon.png',
                    /* Place icon in platform/android/res/drawable/notification_icon.png */
                    largeIcon: 'appicon.png',
                    vibrate: true,
                    insistent: false,
                    group: 'MyNotificationGroup',
                    localOnly: false,
                    priority: +2
                },
                success: opt.success,
                error: opt.error,
                callback: opt.callback
            });

        } else {
            Ti.App.iOS.addEventListener('usernotificationsettings', function registerForPush() {
                Ti.App.iOS.removeEventListener('usernotificationsettings', registerForPush);

                Ti.Network.registerForPushNotifications({
                    success: opt.success,
                    error: opt.error,
                    callback: opt.callback
                });
            });
            Ti.App.iOS.registerUserNotificationSettings({
                types: [
                    Ti.App.iOS.USER_NOTIFICATION_TYPE_ALERT,
                    Ti.App.iOS.USER_NOTIFICATION_TYPE_SOUND,
                    Ti.App.iOS.USER_NOTIFICATION_TYPE_BADGE
                ]
            });
        }
    };

    this.subscribe = function(opt) {
        console.log("SUBSCRIBE PUSH " + senderID);
        gcm.subscribe({
            senderId: senderID,
            topic: "/topics/" + opt.topic,
            success: opt.success,
            error: opt.error
        });
    };

}

it looks like the BroadcastReceiver (service?) is calling success again

I'm seeing this as well. First time called on device the success is only called once, but subsequently it's called twice.