Quantcast
Channel: Appcelerator Developer Center Q&A Unanswered Questions 20
Viewing all articles
Browse latest Browse all 8068

Android Push Notification NullPointerException

$
0
0

Hey everyone,

I have already built a small application for a client that has push notification feature, everything was working as expected until recently, push notification messages seems to arrive to the device some of the time and they never arrive if the application is not started and sometimes while debugging the code I see the following in the log and the application force closes

[ERROR] :  APSCloudPush: Payload is null!
[WARN] :   dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x4161cba8)
[ERROR] :  TiApplication: (main) [269394,269394] Sending event: exception on thread: main msg:java.lang.RuntimeException: Unable to start receiver com.appcelerator.cloudpush.GCMReceiver: java.lang.NullPointerException; Titanium 3.4.0,2014/09/25 16:42,b54c467
[ERROR] :  TiApplication: java.lang.RuntimeException: Unable to start receiver com.appcelerator.cloudpush.GCMReceiver: java.lang.NullPointerException
[ERROR] :  TiApplication:   at android.app.ActivityThread.handleReceiver(ActivityThread.java:2414)
[ERROR] :  TiApplication:   at android.app.ActivityThread.access$1700(ActivityThread.java:135)
[ERROR] :  TiApplication:   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1272)
[ERROR] :  TiApplication:   at android.os.Handler.dispatchMessage(Handler.java:102)
[ERROR] :  TiApplication:   at android.os.Looper.loop(Looper.java:136)
[ERROR] :  TiApplication:   at android.app.ActivityThread.main(ActivityThread.java:5001)
[ERROR] :  TiApplication:   at java.lang.reflect.Method.invokeNative(Native Method)
[ERROR] :  TiApplication:   at java.lang.reflect.Method.invoke(Method.java:515)
[ERROR] :  TiApplication:   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
[ERROR] :  TiApplication:   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
[ERROR] :  TiApplication:   at dalvik.system.NativeStart.main(Native Method)
[ERROR] :  TiApplication: Caused by: java.lang.NullPointerException
[ERROR] :  TiApplication:   at org.json.JSONTokener.nextCleanInternal(JSONTokener.java:116)
[ERROR] :  TiApplication:   at org.json.JSONTokener.nextValue(JSONTokener.java:94)
[ERROR] :  TiApplication:   at org.json.JSONObject.<init>(JSONObject.java:155)
[ERROR] :  TiApplication:   at org.json.JSONObject.<init>(JSONObject.java:172)
[ERROR] :  TiApplication:   at com.appcelerator.cloudpush.APSCloudPush.showTrayNotification(APSCloudPush.java:334)
[ERROR] :  TiApplication:   at com.appcelerator.cloudpush.APSCloudPush.receivePayload(APSCloudPush.java:240)
[ERROR] :  TiApplication:   at com.appcelerator.cloudpush.GCMReceiver.onReceive(GCMReceiver.java:31)
[ERROR] :  TiApplication:   at android.app.ActivityThread.handleReceiver(ActivityThread.java:2407)
[ERROR] :  TiApplication:   ... 10 more
The following is part of my code
function retrieveDeviceToken(cb) {
        // Enable push notifications for this device
        // Save the device token for subsequent API calls
        function deviceTokenSuccess(e) {
            Ti.API.info('successfully got device token');
            Ti.App.Properties.setString(deviceTokenKey, e.deviceToken);
            cb(null);
        }
 
        function deviceTokenError(e) {
            Ti.API.error('Failed to register for push notifications! ' + e.error);
            Ti.App.Properties.setString(deviceTokenKey, '');
            cb(e.error);
        }
 
        if (osname === 'android') {
            var CloudPush = require('ti.cloudpush');
 
            // Initialize the module
            CloudPush.retrieveDeviceToken({
                success : deviceTokenSuccess,
                error : deviceTokenError
            });
 
            // Process incoming push notifications
            CloudPush.addEventListener('callback', function(evt) {
                Ti.API.info("Notification received: " + evt.payload);
                var payload = JSON.parse(evt.payload);
                handlePushNotificationMessages(payload);
            });
        } 
    }
 
    function subscribeToChannel() {
        // Subscribes the device to the 'news_alerts' channel
        // Specify the push type as either 'android' for Android or 'ios' for iOS
        Cloud.PushNotifications.subscribeToken({
            device_token : Ti.App.Properties.getString(deviceTokenKey, ''),
            channel : pnChannel,
            type : Ti.Platform.name == 'android' ? 'android' : 'ios'
        }, function(e) {
            if (e.success) {
                Ti.API.info('Subscribed to channel');
                Ti.App.Properties.setBool(pnEnabledKey, true);
                switchButton.value = true;
            } else {
                Ti.API.error('Error: ' + ((e.error && e.message) || JSON.stringify(e)));
                Ti.App.Properties.setBool(pnEnabledKey, false);
                switchButton.value = false;
            }
        });
    }
 
    function unsubscribeToChannel() {
        // Unsubscribes the device from the 'test' channel
        Cloud.PushNotifications.unsubscribeToken({
            device_token : Ti.App.Properties.getString(deviceTokenKey, ''),
            channel : pnChannel,
        }, function(e) {
            if (e.success) {
                Ti.API.info('Unsubscribed from channel');
                Ti.App.Properties.setBool(pnEnabledKey, false);
                switchButton.value = false;
            } else {
                Ti.API.error('Error: ' + ((e.error && e.message) || JSON.stringify(e)));
                Ti.App.Properties.setBool(pnEnabledKey, true);
                switchButton.value = true;
            }
        });
    }
 
    function handlePushNotificationMessages(payload) {
        var newsObj = {
            title : payload.postTitle,
            link : payload.postUrl,
            date : payload.postDate,
            time : payload.postTime,
            pubDate : '',
            description : '',
            image : ''
        };
        newsObj.pushNotificationNews = true;
 
        var DetailView = require('ui/common/DetailView');
        var detailView = new DetailView(newsObj, true);
        if (parentWindow) {
//something for ios
            var currentBadge = Ti.UI.iPhone.getAppBadge();
            if (currentBadge > 0) {
                currentBadge = currentBadge - 1;
                Ti.UI.iPhone.setAppBadge(currentBadge);
            }
            if (parentWindow.isLeftWindowOpen()) {
                parentWindow.toggleLeftWindow();
            }
            parentWindow.getCenterWindow().openWindow(detailView, {
                //this is mainly for ios devices
                animated : true
            });
        } else {
//android part
            detailView.open();
        }
    };
 
    function handleSwitchButtonChanges(event) {
        if (event.value) {
            /**
             * i need to enable push notification
             */
            retrieveDeviceToken(function(err) {
                if (!err) {
                    subscribeToChannel();
                }
            });
        } else {
            /**
             * i need to disable push notification
             */
            retrieveDeviceToken(function(err) {
                if (!err) {
                    unsubscribeToChannel();
                }
            });
        }
    }
 
    function createSwitchButton(text) {
            var pnIsEnabled = Ti.App.Properties.getBool(pnEnabledKey, false);
            var tokenIsAvailable = Ti.App.Properties.getString(pnEnabledKey, '');
            if (tokenIsAvailable && pnIsEnabled) {
                switchButton.value = true;
            } else {
                switchButton.value = false;
            }
 
        switchButton.addEventListener('change', handleSwitchButtonChanges);
    }
any help would be highly appreciated.

I need to mention that I am using the appcelerator console to test this and i can confirm that the push notifications arrives most of the times when the app is running but when the app is not running the notifications never make it

Thanks in advance


Viewing all articles
Browse latest Browse all 8068

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>