arleyandrada / PushClient

FREE and Open Source - Titanium PushClient Module
Other
44 stars 17 forks source link

Android notifications doesn't work when the application is fully closed. #123

Closed infoa3r closed 8 years ago

infoa3r commented 8 years ago

Hi Arley,

First of all thank you for your work with the module.

We have your module working on our app in iOS and Android platform. On iOS everything is working fine (Background and Foreground), but on Android the local notification is shown only when the application is closed pressing the home button. If we closed the app through the task manager the Local notification is not shown.

The payload we send from the server is this:

 {
  data: {
      data:{"title":"AppName",
            "alert":"New Notifications!",
            "notificationId":0,
            "vibrate":true,
            "sound":"default",
            "smallIcon":"iconnotification",
            "largeIcon":"appicon"
      }
  }
}

Here you can see the fragment of code which we use your module. This code is placed on "app.js" and is called when the app starts:

PushClient = require('br.com.arlsoft.pushclient');

    if (Titanium.Platform.name != 'android') {//OS_IOS
        var issue17030 = Ti.Network.registerForPushNotifications;
        var issue17030iOS8 = Ti.App.iOS.registerUserNotificationSettings;
    }

    var registerOptions = {
        GCMSenderId : '333366674012',
        APNTypes : [PushClient.NOTIFICATION_TYPE_BADGE, PushClient.NOTIFICATION_TYPE_ALERT, PushClient.NOTIFICATION_TYPE_SOUND]
    };

    if (Titanium.Platform.name != 'android') {
        var acceptAction = PushClient.createAction({
            identifier : 'ACCEPT_IDENTIFIER',
            title : 'Accept',
            activationMode : PushClient.NOTIFICATION_ACTIVATION_MODE_FOREGROUND,
            destructive : false,
            authenticationRequired : true
        });
        var rejectAction = PushClient.createAction({
            identifier : 'REJECT_IDENTIFIER',
            title : 'Reject',
            activationMode : PushClient.NOTIFICATION_ACTIVATION_MODE_BACKGROUND,
            destructive : true,
            authenticationRequired : false
        });
        var downloadContent = PushClient.createCategory({
            identifier : 'APPROVE_CONTENT',
            actionsForMinimalContext : [acceptAction, rejectAction],
            actionsForDefaultContext : [acceptAction, rejectAction]
        });
        registerOptions.Categories = [downloadContent];
    }

    var eventSuccess = function(event) {
        //TODO 
    };

    var eventError = function(event) {
        if (!event) {
            //alertDialogManagerInstance.showAlert('Error:\n\nInvalid error');
            return;
        }
        switch (event.code) {
        case PushClient.ERROR_SENDER_ID:
            //TODO 
            break;
        case PushClient.ERROR_PLAY_SERVICES:
            //TODO 
            break;
        case PushClient.ERROR_NOT_SUPPORTED:
            //TODO 
            break;
        case PushClient.ERROR_REGISTER:
            //TODO 
            break;
        case PushClient.ERROR_UNREGISTER:
            //TODO 
            break;
        default:
            //TODO 
        }
    };

    var eventCallback = function(event) {   
        if (!event) {
            // Should never happen...
        } else if (event.mode == PushClient.MODE_FOREGROUND) {
            // Push data received with app in foreground
            //TODO 
        } else if (event.mode == PushClient.MODE_CLICK) {
            //TODO 
            // Push data received when user clicks in notification message
        } else if (event.mode == PushClient.MODE_BACKGROUND) {
            // Requires set remote-notification UIBackgroundModes in tiapp.xml
            PushClient.endBackgroundHandler(event.data.handlerId);
            // Put the application back to sleep before any UI interations
            // Push data received with app in background
        } else if (event.mode == PushClient.MODE_ACTION) {
            // Push data received when userchoose an action from notification message
        } else {
            // Should never happen...
        }
    };

    PushClient.addEventListener(PushClient.EVENT_SUCCESS, eventSuccess);
    PushClient.addEventListener(PushClient.EVENT_ERROR, eventError);
    PushClient.addEventListener(PushClient.EVENT_CALLBACK, eventCallback);

If we analyze the logcat when send the notification we can see the next lines:

I/GCM     ( 1357): GCM message com.company.apppackage 0:1450357838356749%9ffef7a5f9fd7ecd
W/GCM-DMM ( 1357): broadcast intent callback: result=CANCELLED forIntent { act=com.google.android.c2dm.intent.RECEIVE pkg=com.company.apppackage (has extras) }

We have test in different version of android (4.4, 5.1 and 6.0) with the same result.

Titanium SDK version is 5.2.0.GA and the lastest version of your module.

¿Do you have an idea of what is going on?

Best regards.

arleyandrada commented 8 years ago

Related to : https://github.com/arleyandrada/PushClient/issues/111