morinel / gcmpush

Titanium Module for Android Push
Apache License 2.0
57 stars 35 forks source link

Push when app is closed (show specific view) #6

Open patriciapg93 opened 9 years ago

patriciapg93 commented 9 years ago

Hi,

I want to show specific view when the user touches the notification. It works when the app is in foreground and background, but it doesn't when the app is closed (it only opens the app and). In the logcat I can't see it never entering in the lastData if, I don't know why.

Is there a way to achieve this?

Here is my code, anyway:


exports.pushNotification = function() {
    var gcm = require("nl.vanvianen.android.gcm");

    var registered = Titanium.App.Properties.getString('pushRegistered2') != null;
    Ti.API.info('REGISTERED: ' + registered);

    /* If the app is started or resumed act on pending data saved when the notification was received */
    var lastData = gcm.getLastData();
    if (lastData) {
        Ti.API.info("Last notification received " + JSON.stringify(lastData));

        // Here I try to show the view - it doesn't work
        Ti.App.fireEvent('view:FavoriteView', {
            push : lastData
        });

        gcm.clearLastData();
    }

    gcm.registerPush({
        /* The Sender ID from Google Developers Console, see https://console.developers.google.com/project/XXXXXXXX/apiui/credential */
        /* It's the same as your project id */
        senderId : '299548822026',
        notificationSettings : {
            // sound : 'mysound.mp3', /* Place soudn file in platform/android/res/raw/mysound.mp3 */
            // smallIcon : 'notification_icon.png', /* Place icon in platform/android/res/drawable/notification_icon.png */
            // largeIcon : 'appicon.png', /* Same */
            vibrate : true, /* Whether the phone should vibrate */
            insistent : true, /* Whether the notification should be insistent */
            group : 'MyNotificationGroup', /* Name of group to group similar notifications together */
            localOnly : false, /* Whether this notification should be bridged to other devices */
            priority : +2 /* Notification priority, from -2 to +2 */
        },
        success : function(event) {
            Ti.API.info("Push registration success: " + JSON.stringify(event));
            /* Add code to send event.registrationId to your server */

            if (!registered) {
                var myID = event.registrationId;
                Titanium.App.Properties.setString("deviceToken", myID);
                Alloy.Globals.utils.sendDeviceToken(Alloy.Globals.getUserId(), myID/*, Alloy.Globals.getPushRegisterURL(), _callbackSendServer*/);
                Titanium.App.Properties.setString("pushRegistered2", "1");
            }
        },
        error : function(event) {
            Ti.API.info("Push registration error: " + JSON.stringify(event));
            alert(event.error);
        },
        callback : function(event) {
            Ti.API.info("Push callback = " + JSON.stringify(event));
            /* Called when a notification is received and the app is in the foreground */

            Alloy.Globals.lastPushMessage = event.data.message;

            // Here I try to show the view - It works
            Ti.App.fireEvent('view:FavoriteView', {
                push : event.data
            });

            var dialog = Ti.UI.createAlertDialog({
                title : 'Push received',
                message : JSON.stringify(event.data),
                buttonNames : ['View'],
                cancel : 1
            });
            dialog.addEventListener("click", function(event) {
                dialog.hide();
                if (event.index == 0) {
                    /* Do stuff to view the notification */
                }
            });
            //dialog.show();
        }
    });
};
morinel commented 9 years ago

Put this in your app.js:

/* When the app is started */
var lastData = gcm.getLastData();
if (lastData) {
    ... view the notification ...
    gcm.clearLastData();
}

/* And when the app is resumed */
Ti.Android.currentActivity.addEventListener("resume", function() {
    var lastData = gcm.getLastData();
    if (lastData) {
       ... view the notification ...
       gcm.clearLastData();
    }
});
patriciapg93 commented 9 years ago

Okey, now it works. I was calling the function twice, my bad.

The problem I have now is that sometimes, when I execute the app and I close it, I don't receive the push. I must open it again (still not receive push) and close it, then I can receive push again. It seems to enter twice in the success function because I can see the Ti.API.Info duplicated (the second one takes a few seconds to appear), but now I'm calling this function once.

Any clue? What am I doing wrong?

Thank you for your time!

morinel commented 9 years ago

Maybe check the output of adb logcat and verify whether the push is actually received by the device (or simulator).

patriciapg93 commented 9 years ago

I've checked the logcat and when I send a push, I can see this:

09-15 12:09:05.795: I/GCM(1534): GCM message  [app_package] 0:1442311746300784%686fe507f9fd7ecd

09-15 12:09:05.895: V/GCMBroadcastReceiver(20289): GCM IntentService class: nl.vanvianen.android.gcm.GCMIntentService

But nothing else, and I don't receive the notification. This happends when I close the app before the second success info..

When I receive push the ouptut is:

09-15 12:13:28.785: I/GCM(1534): GCM message [app_package] 0:1442312009285689%686fe507f9fd7ecd
09-15 12:13:29.465: V/GCMRegistrar(22499): Setting the name of retry receiver class to nl.vanvianen.android.gcm.GCMBroadcastReceiver
09-15 12:13:29.465: V/GCMBroadcastReceiver(22499): GCM IntentService class: nl.vanvianen.android.gcm.GCMIntentService
09-15 12:13:29.485: V/GCMBaseIntentService(22499): Intent service name: GCMIntentService--1
09-15 12:13:29.485: D/GCMIntentService(22499): (IntentService[GCMIntentService--1]) [29,390] Push notification received
09-15 12:13:29.495: D/GCMIntentService(22499): (IntentService[GCMIntentService--1]) [5,395] Message key: Type value: 1
09-15 12:13:29.495: D/GCMIntentService(22499): (IntentService[GCMIntentService--1]) [1,396] Message key: from value: 299548822026
09-15 12:13:29.495: D/GCMIntentService(22499): (IntentService[GCMIntentService--1]) [1,397] Message key: title value: Ajedrez Viladecans
09-15 12:13:29.495: D/GCMIntentService(22499): (IntentService[GCMIntentService--1]) [1,398] Message key: message value: ¡Tienes un nuevo cuestionario!
09-15 12:13:29.495: D/GCMIntentService(22499): (IntentService[GCMIntentService--1]) [1,399] Message key: channelID value: 6045
09-15 12:13:29.495: D/GCMIntentService(22499): (IntentService[GCMIntentService--1]) [2,401] Message key: collapse_key value: do_not_collapse
09-15 12:13:29.565: E/GCMIntentService(22499): (IntentService[GCMIntentService--1]) [67,468] Invalid setting priority, should be int, between PRIORITY_MIN (-2) and PRIORITY_MAX (2)
09-15 12:13:29.575: D/GCMIntentService(22499): (IntentService[GCMIntentService--1]) [5,473] Creating notification...
09-15 12:13:29.585: I/GCMIntentService(22499): (IntentService[GCMIntentService--1]) [16,489] Group: MyNotificationGroup
09-15 12:13:29.595: I/GCMIntentService(22499): (IntentService[GCMIntentService--1]) [7,496] LocalOnly: false
09-15 12:13:29.595: I/GCMIntentService(22499): (IntentService[GCMIntentService--1]) [1,497] Priority: 0
09-15 12:13:29.595: I/GCMIntentService(22499): (IntentService[GCMIntentService--1]) [2,499] Vibrate: true
09-15 12:13:29.595: I/GCMIntentService(22499): (IntentService[GCMIntentService--1]) [1,500] Insistent: false
morinel commented 9 years ago

Strange, you should see the Push notification received message.

patriciapg93 commented 9 years ago

I think it is because of the registrationId. If it's entering twice in the success function but I quit the app before the second success then it's like the first registration doesn't exists and the second one fails or something, so I'm not able to receive any push.

I'm trying to figure out why is the app trying to register for push twice.