Quantcast
Viewing all articles
Browse latest Browse all 8068

android push notification problems

Hey, I am trying to integrate push notification for an android app. I am using Titanium 3.1.3.GA and using my nexus 4(4.4.2) to run/debug the application I have so far the following code:

Titanium.UI.setBackgroundColor('#000');
var deviceToken;
var win = Ti.UI.createWindow({
    backgroundColor : '#ccc',
    title : 'Android Cloud Push Notification'
});
 
var CloudPush = require('ti.cloudpush');
 
//fetch device token
CloudPush.retrieveDeviceToken({
    success : function deviceTokenSuccess(e) {
        deviceToken = e.deviceToken;
        alert('Device Token: ' + deviceToken);
        Ti.API.info('Device Token: ' + e.deviceToken);
    },
    error : function deviceTokenError(e) {
        alert('Failed to register for push! ' + e.error);
    }
});
 
CloudPush.debug = true;
CloudPush.enabled = true;
CloudPush.showTrayNotificationsWhenFocused = true;
CloudPush.focusAppOnPush = false;
 
var Cloud = require('ti.cloud');
Cloud.debug = true;
 
var submit = Ti.UI.createButton({
    title : 'Register For Push Notification',
    color : '#000',
    height : '53dp',
    width : '200dp',
    top : '100dp'
});
 
win.add(submit);
 
submit.addEventListener('click', function(e) {
    loginDefault();
});
 
function loginDefault(e) {
    //Create a Default User in Cloud Console, and login with same credential
    Cloud.Users.login({
        login : 'username',
        password : 'password'
    }, function(e) {
        if (e.success) {
            alert("Login success");
            defaultSubscribe();
        } else {
            alert('Login error: ' + ((e.error && e.message) || JSON.stringify(e)));
        }
    });
}
 
function defaultSubscribe() {
    Cloud.PushNotifications.subscribe({
        channel : 'alert2',//'alert' is channel name
        device_token : deviceToken,
        type : 'gcm'
    }, function(e) {
        if (e.success) {
            alert('Subscribed for Push Notification!');
        } else {
            alert('Subscrib error:' + ((e.error && e.message) || JSON.stringify(e)));
        }
    });
}
 
 
CloudPush.addEventListener('callback', function(evt) {
    alert(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)');
});
win.open();
the CloudPush.retrieveDeviceToken success callback is being called and I am able to retrieve the device token, also both the login callback and register callback are being logged however the problems seems to happen when i access the Titanium ACS and send a push notification from the web interface, my app force closes and I get the following debug error
[ERROR][AndroidRuntime(22606)] FATAL EXCEPTION: main
[ERROR][AndroidRuntime(22606)] Process: com.fouadkada.titaniumpush, PID: 22606
[ERROR][AndroidRuntime(22606)] java.lang.NoSuchMethodError: org.appcelerator.titanium.TiApplication.isCurrentActivityInForeground
[ERROR][AndroidRuntime(22606)]  at ti.cloudpush.CloudpushModuleGeneric.receivePayload(CloudpushModuleGeneric.java:81)
[ERROR][AndroidRuntime(22606)]  at ti.cloudpush.GCMReceiver.onReceive(GCMReceiver.java:26)
[ERROR][AndroidRuntime(22606)]  at android.app.ActivityThread.handleReceiver(ActivityThread.java:2419)
[ERROR][AndroidRuntime(22606)]  at android.app.ActivityThread.access$1700(ActivityThread.java:135)
[ERROR][AndroidRuntime(22606)]  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1272)
[ERROR][AndroidRuntime(22606)]  at android.os.Handler.dispatchMessage(Handler.java:102)
[ERROR][AndroidRuntime(22606)]  at android.os.Looper.loop(Looper.java:136)
[ERROR][AndroidRuntime(22606)]  at android.app.ActivityThread.main(ActivityThread.java:5017)
[ERROR][AndroidRuntime(22606)]  at java.lang.reflect.Method.invokeNative(Native Method)
[ERROR][AndroidRuntime(22606)]  at java.lang.reflect.Method.invoke(Method.java:515)
[ERROR][AndroidRuntime(22606)]  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
[ERROR][AndroidRuntime(22606)]  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
[ERROR][AndroidRuntime(22606)]  at dalvik.system.NativeStart.main(Native Method)
[WARN][ActivityManager(  597)]   Force finishing activity com.fouadkada.titaniumpush/.TitaniumpushActivity
Thanks in advance

Viewing all articles
Browse latest Browse all 8068

Trending Articles