caffeinalab / ti.goosh

GCM/FCM module for Titanium.
MIT License
52 stars 34 forks source link

callback not executed if the app is on background or killed #38

Closed Hrfreire closed 7 years ago

Hrfreire commented 7 years ago

The app is calling the callback function only if is on foreground. When the app is closed and I click on the notification, the app open, but not execute callback function.

progress44 commented 7 years ago

@Hrfreire what version of ti.goosh are you using?

Hrfreire commented 7 years ago

I downloaded the master code yesterday and compiled myself because some erros resolved on commit cfc52c0, after last release.

progress44 commented 7 years ago

@Hrfreire does that mean you're using firebase too?

Hrfreire commented 7 years ago

Yes. I'm using firebase. I'm using django-fcm lib: https://django-fcm.readthedocs.io/en/latest/

It works when the app is opened. If the app is in background or closed, the notification arrives, but when user click the notification, the callback function is not fired.

Hrfreire commented 7 years ago

I've tested on my tablet (Samsung galaxy note 8.0) android 4.4.4, and it works when the app is on background but not when the app is closed.

On Motorola moto x 2014 android 6.0 and Xaomi redmi 2 pro android 5.1.1, not working when the app is in background or closed.

kopiro commented 7 years ago

I can't replicate this issue. Can you show me the code that are you using to catch notifications?

Thanks.

frazei commented 7 years ago

I have the same problem using FCM, Android 6.0, Ti 6.0.1.GA and Ti.Goosh 2.0.0 (tested 2.0.1 today with no difference at all). It works only with the app open and in foreground.

var TiGoosh = require('ti.goosh');

TiGoosh.registerForPushNotifications({  
    callback: function(e) {
        alert('Received push: ' + JSON.stringify(e));
    },
    success: function(e) {
            deviceToken = e.deviceToken;
            Ti.App.Properties.setString('deviceToken', deviceToken);
            Ti.API.debug('deviceToken '+deviceToken);
    },
    error: function(err) {
            alert('Failed to register for push notifications! ' + err);
            Ti.API.error(err);
    }
});
kopiro commented 7 years ago

@frazei In which file is written this code? Alloy.js?

frazei commented 7 years ago

I have a file that is called push.js in the root (Resources) that exports a few functions via: exports.registerAndroid = function() { and this file is included inside app.js (that's not an alloy project):

var push = require('push');
if (Ti.Platform.Android) {
        push.registerAndroid();

I've used this structure for years without any problem, it's the same for ios and it works... Do you think that it can be a problem?

kopiro commented 7 years ago

@frazei I don't think so. Could you try to send a minimum part of your project by replicating this structure (only with Push)

frazei commented 7 years ago

Thank you for your help! I've made a simple app only with the push part: https://www.dropbox.com/s/y86l6iu0516bxfa/simplepush.zip?dl=0 While making some more tests I've found out there was an error in the server part (the PHP script was using the "id" property as a varchar, not as a number) and now I can receive notifications even when the app is in background. But the problem still remains if I close (or kill) the app: I do not receive notification at all.

progress44 commented 7 years ago

@frazei if you have a Huawei phone, check this issue https://github.com/caffeinalab/ti.goosh/issues/39

frazei commented 7 years ago

@progress44 Yes! You are right... followed the instructions from http://www.androidheadlines.com/2016/01/how-to-fix-missing-notifications-on-huawei-phones.html and now it's working: I really hate Android and all this complications. Thanks @progress44 and @kopiro for your kind support!

Hrfreire commented 7 years ago

@progress44 I just found that the django-fcm module (and FCM console) that I use, send some information inside 'notification' instead 'data' . The notification arrives normally, but the callback is not executed if the app is in background. I forked the django fcm module and edited to send all on 'data' and now it's working normally.

So, probably everyone that uses FCM console will have this problem.

progress44 commented 7 years ago

Yeah I imagined that was the case. I'm working on an updated to support FCM as it seems that people are starting to prefer it to GCM Thanks for the information

nadjib commented 7 years ago

I have same problem, the app is calling the callback function only if is on foreground. When the app is closed and I click on the notification, the app open, but not execute callback function. Any solution ?

Hrfreire commented 7 years ago

If you are using firebase, send the data notification inside 'data' object. Normally firebase uses 'notification' object to send the data. In my case, change it for data, works.

nadjib commented 7 years ago

Yes it's work when app is on foreground or background, but not if it is closed. When the app is closed and I click on the notification, the app open, but not execute callback function.

nadjib commented 7 years ago

Finally it's work 😄 with 2.0.2 version (in foreground, in background and when the app is closed) 😃 But not work with 2.0.6 version !

progress44 commented 7 years ago

I'm glad you made it work. What was going wrong when it wasn't working before?

nadjib commented 7 years ago

When the app is closed and I click on the notification, the app open, but not execute callback function