Titanium JIRA Archive
Appcelerator Community (AC)

[AC-4744] cloudpush not calling callback when application is running - forground

GitHub Issuen/a
TypeBug
Priorityn/a
StatusClosed
ResolutionInvalid
Resolution Date2017-02-01T20:32:12.000+0000
Affected Version/sAppcelerator Studio 4.5.0
Fix Version/sn/a
ComponentsAppcelerator Modules, Arrow Push, Titanium SDK & CLI
Labelsn/a
ReporterAnis Vora
AssigneeShak Hossain
Created2017-01-19T11:21:25.000+0000
Updated2017-02-01T20:32:13.000+0000

Description

We implemented Android push in application but having one small issue. When application is running/foreground/active it is not calling callback function. In debug window we can see this when new message came. [INFO] : APSCloudPush: receivePayload: {"android":{"badge":"0","alert":"New message(s) received","sound":"default","icon":"icon","vibrate":true,"title":"CareChatZ"}} [INFO] : APSCloudPush: background: true [INFO] : APSCloudPush: processCallbackIfInstantiated payload: {"android":{"badge":"0","alert":"New message(s) received","sound":"default","icon":"icon","vibrate":true,"title":"CareChatZ"}} Please help

Comments

  1. Sharif AbuDarda 2017-01-19

    Hello, Send the sample code that regenerates the issue. Also, Use the latest SDK 6.0.1.GA for your build.
  2. Anis Vora 2017-01-20

    Hello, Thanks for prompt reply , can't switch to 6.0.1 GA as we are using titutorial.audiorecorder module and it is not compatible with that. Now about code , it is all same as http://docs.appcelerator.com/platform/latest/#!/api/Modules.CloudPush {noformat} if(isAndroid) { CloudPush = require('ti.cloudpush'); CloudPush.showTrayNotificationsWhenFocused = false; CloudPush.showAppOnTrayClick = true; CloudPush.showTrayNotification = true; CloudPush.debug = true; } CloudPush.addEventListener('callback', function (evt) { alert(evt.payload); }); {noformat} I also checked Androidmanifest.xml file from build directory and all seems to be good. Thanks in advance. Regards, Anis
  3. Sharif AbuDarda 2017-01-20

    Hello, I just checked. callback is called successfully as alert, when App is running in foreground.I am running in Android 6.0.1. with SDK 6.0.1.GA. Use the below code.
       CloudPush.showTrayNotificationsWhenFocused = true;
       
    Thanks.
  4. Anis Vora 2017-01-21

    Hello, Thanks for your reply. Today we found interesting thing , that CloudPush variable was part of config.js which have all common functions and variables and that is getting included in all page off application. Now today we move that CloudPush variable and event listener to app.js and .... bingo .. it start working , but with small problem it works only in first screen of application which is getting called from app.js .. so if we move to other screen in app it is not calling that callback event. Do you have any idea on that ? Regard, Anis
  5. Sharif AbuDarda 2017-01-23

    Hello, Can you share a sample project that we can use to reproduce the issue? We will investigate it.
  6. Anis Vora 2017-01-24

    Hello, Thanks you very much for all your continues support. I sent you sample code and all required details , please let me know if anything more is require. Regards, Anis
  7. Sharif AbuDarda 2017-01-24

    Hello, Your sample app has lots of issue like ti.include, which is deprecated. I made a sample app myself which is working as expected. See my sample app. app.js
       var win = Ti.UI.createWindow({
           backgroundColor : '#ccc',
           title : 'Android Cloud Push Notification'
       });
        
       var CloudPush = require('ti.cloudpush');
         CloudPush.debug = true;
         CloudPush.enabled = true;
         CloudPush.showTrayNotificationsWhenFocused = true;
         CloudPush.showTrayNotification = true;
         CloudPush.focusAppOnPush = false;
       
       var deviceToken=null;
       var Cloud = require('ti.cloud');
       Cloud.debug = true;
        
       var submit = Ti.UI.createButton({
           title : 'Register For Push Notification',
           color : '#000',
           height : 60,
           width : 200,
           top : 100,
       });
        
       win.add(submit);
        
       submit.addEventListener('click', function(e) {
           loginDefault();
       });
       
       var submit1 = Ti.UI.createButton({
           title : 'Subscribe Device',
           color : '#000',
           height : 60,
           width : 200,
           top : 200,
       });
        
       win.add(submit1);
        
       submit1.addEventListener('click', function(e) {
           defaultSubscribe();
       });
       
       var submit2 = Ti.UI.createButton({
           title : 'Unsubscribe Device',
           color : '#000',
           height : 60,
           width : 200,
           top : 300,
       });
        
       win.add(submit2);
        
       submit2.addEventListener('click', function(e) {
           defaultUnSubscribe();
       });
       
       var submit3 = Ti.UI.createButton({
           title : 'New Window',
           color : '#000',
           height : 60,
           width : 200,
           top : 400,
       });
        
       win.add(submit3);
        
       submit3.addEventListener('click', function(e) {
           var NewWin = require("NewWindow");
           var self = NewWin();
       });
       
       
       CloudPush.retrieveDeviceToken({
           success : deviceTokenSuccess,
           error : deviceTokenError
       });
       
       function deviceTokenSuccess(e) {
           deviceToken = e.deviceToken;
       }
        
       function deviceTokenError(e) {
           alert('Failed to register for push notifications! ' + e.error);
       }
       
       CloudPush.addEventListener('callback', function(evt) {
           alert("Notification received: " + evt.payload);
       });
       
       CloudPush.addEventListener('trayClickLaunchedApp', function(evt) {
           Ti.API.info('Tray Click Launched App (app was not running)');
       });
        
       CloudPush.addEventListener('trayClickFocusedApp', function(evt) {
           Ti.API.info('Tray Click Focused App (app was already running)');
       });
        
       function loginDefault(e) {
           Cloud.Users.login({
               login : 'shajib.sigma@gmail.com',
               password : '123456'
           }, function(e) {
               if (e.success) {
                   alert("Successfully Loged In");
               } else {
                   alert('Error: ' + ((e.error && e.message) || JSON.stringify(e)));
               }
           });
       }
        
       function defaultSubscribe() {
           Cloud.PushNotifications.subscribe({
               channel : 'alert',
               device_token : deviceToken,
               type : 'android'
           }, function(e) {
               if (e.success) {
                   alert('Device Subscribed!');
               } else {
                   alert('Error:' + ((e.error && e.message) || JSON.stringify(e)));
               }
           });
       }
       
       function defaultUnSubscribe() {
           Cloud.PushNotifications.unsubscribe({
               channel: 'alert',
               device_token: deviceToken,
               type : 'android'
           }, function (e) {
               if (e.success) {
                   alert('Device Unsubscribed!');
               } else {
                   alert('Error:\n' + ((e.error && e.message) || JSON.stringify(e)));
               }
           });
       }
        
       win.open();
       
    NewWindow.js
       function NewWindow() {
           var self = Ti.UI.createWindow({
               backgroundColor : '#ffffff',
               navBarHidden : true,
               fullscreen : true,
               //width : '100%'
       
           });
           var button = Ti.UI.createButton({
               width : 100,
               height : 100,
               top: 0,
               title: 'Back'
       
           });
           
           button.addEventListener('click', function(e){
               self.close();
               
           });
           
           self.add(button);
           self.open();
       
       }
       
       module.exports = NewWindow; 
       
    First use the above code in a new project. Create a new user in the arrowDB console in platform and provide the email and pass in the above code. As
       Cloud.Users.login({
               login : 'xxxxxx@gmail.com',
               password : '123456'
           }
       
    Configure firebase push notification for the app in the console. Click register for push notification for login(see the success alert) Click subscribe device for device subscription(see the success alert) Click New window to open another window. Send push notification from the console. See the alert open as the push received and whole lot. Thanks.
  8. Anis Vora 2017-01-27

    Hello Sharif, Really thanks for all your help and time you spent to help us. Finally we found the problem , it was because of win.close .. like as part of good practice we are closing previous win once new window get loaded. So, it works fine in first screen which is loaded from app , and if we move to other screen then that win is getting closed and callback stops working. Now , one small problem .. that push notification icon in notification tray is plain white square instead of actual logo/icon. Can you help with that ? or do we need to write in other post ? Regards, Anis
  9. Sharif AbuDarda 2017-01-31

    Hello, You need to put a custom icon in project "platform/android/res/drawable" directory. This icon will be used to show notification bar and be used as icon of notification item in the notification center. provide the name of the file without the extinction in icon field when you send the push notification from platform. If the field is empty default icon will be used. See guide: http://docs.appcelerator.com/platform/latest/#!/guide/Sending_and_Scheduling_Push_Notifications Android-specific payload fields/icon.

JSON Source