Skip to content
This repository has been archived by the owner on Sep 4, 2020. It is now read-only.

Crash with Android at startup #144

Closed
FabienHenon opened this issue Sep 16, 2015 · 14 comments
Closed

Crash with Android at startup #144

FabienHenon opened this issue Sep 16, 2015 · 14 comments
Labels

Comments

@FabienHenon
Copy link

Hi everyone and thanks for this plugin!

I'm developing a cordova/angularJs application.

Everything is working fine on ios, but on Android the application is crashing at startup.

I tried to debug it and I found that the PushPlugin.sendEvent method is called before the PushPlugin.execute method. Thus, the pushContext variable is null and makes the application crashing.

I'm calling the PushNotification.ini(...) JS code in the run block of my angular module. I also tried to call it in the deciveReady cordova event but the issue is still present.

Am I doing something wrong? Or is there a real issue here?

Thank you for your help!

@macdonst
Copy link
Member

@FabienHenon if the app is crashing at startup can you get me a adb logcat? That should point to any errors in the Android implementation. Also,a small sample of how you are calling it would be great.

@FabienHenon
Copy link
Author

@macdonst Here is the logcat

09-16 10:20:33.473  30510-30682/com.app.production:Engagement V/engagement-test﹕ com.app.production: Connection: Connecting in 0 ms
09-16 10:20:33.473  30510-30682/com.app.production:Engagement D/engagement-test﹕ com.app.production: Connection: Connecting...
09-16 10:20:33.817  30460-30460/com.app.production V/GCMBroadcastReceiver﹕ onReceive: com.google.android.c2dm.intent.REGISTRATION
09-16 10:20:33.818  30460-30460/com.app.production V/GCMRegistrar﹕ Setting the name of retry receiver class to com.adobe.phonegap.push.CordovaGCMBroadcastReceiver
09-16 10:20:33.818  30460-30460/com.app.production V/GCMBroadcastReceiver﹕ GCM IntentService class: com.adobe.phonegap.push.GCMIntentService
09-16 10:20:33.821  30460-30460/com.app.production V/GCMBaseIntentService﹕ Acquiring wakelock
09-16 10:20:33.826  30460-30460/com.app.production V/GCMBaseIntentService﹕ Intent service name: GCMIntentService-GCMIntentService-1
09-16 10:20:33.831  30460-30697/com.app.production V/GCMRegistrar﹕ Registering receiver
09-16 10:20:33.833  30460-30697/com.app.production D/GCMBaseIntentService﹕ handleRegistration: registrationId = APA91bGjFME_3T5OUM8D07x9ipElZ9XvRVB1k4-OxW7Mn47palbW2HQHflSBnfadtHKdonUosmhyR_7QKFtQma-Qrr1MCc-wPaqCQdGle0_UTcgrmtV5ceM2Gh7O9i2L5PuPkfUXhZm7, error = null, unregistered = null
09-16 10:20:33.833  30460-30697/com.app.production D/GCMRegistrar﹕ resetting backoff for com.app.production
09-16 10:20:33.840  30460-30697/com.app.production V/GCMRegistrar﹕ Saving regId on app version 300682
09-16 10:20:33.840  30460-30697/com.app.production V/PushPlugin_GCMIntentService﹕ onRegistered: APA91bGjFME_3T5OUM8D07x9ipElZ9XvRVB1k4-OxW7Mn47palbW2HQHflSBnfadtHKdonUosmhyR_7QKFtQma-Qrr1MCc-wPaqCQdGle0_UTcgrmtV5ceM2Gh7O9i2L5PuPkfUXhZm7
09-16 10:20:33.841  30460-30697/com.app.production V/PushPlugin_GCMIntentService﹕ onRegistered: {"registrationId":"APA91bGjFME_3T5OUM8D07x9ipElZ9XvRVB1k4-OxW7Mn47palbW2HQHflSBnfadtHKdonUosmhyR_7QKFtQma-Qrr1MCc-wPaqCQdGle0_UTcgrmtV5ceM2Gh7O9i2L5PuPkfUXhZm7"}
09-16 10:20:33.841  30460-30697/com.app.production V/GCMBaseIntentService﹕ Releasing wakelock
09-16 10:20:33.847  30510-30510/com.app.production:Engagement I/engagement-test﹕ com.app.production: Crash triggered:
    java.lang.NullPointerException: Attempt to invoke virtual method 'void org.apache.cordova.CallbackContext.sendPluginResult(org.apache.cordova.PluginResult)' on a null object reference
            at com.adobe.phonegap.push.PushPlugin.sendEvent(PushPlugin.java:121)
            at com.adobe.phonegap.push.GCMIntentService.onRegistered(GCMIntentService.java:68)
            at com.google.android.gcm.GCMBaseIntentService.handleRegistration(GCMBaseIntentService.java:296)
            at com.google.android.gcm.GCMBaseIntentService.onHandleIntent(GCMBaseIntentService.java:197)
            at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.os.HandlerThread.run(HandlerThread.java:61)
09-16 10:20:33.848  30460-30697/com.app.production E/AndroidRuntime﹕ FATAL EXCEPTION: IntentService[GCMIntentService-GCMIntentService-1]
    Process: com.app.production, PID: 30460
    java.lang.NullPointerException: Attempt to invoke virtual method 'void org.apache.cordova.CallbackContext.sendPluginResult(org.apache.cordova.PluginResult)' on a null object reference
            at com.adobe.phonegap.push.PushPlugin.sendEvent(PushPlugin.java:121)
            at com.adobe.phonegap.push.GCMIntentService.onRegistered(GCMIntentService.java:68)
            at com.google.android.gcm.GCMBaseIntentService.handleRegistration(GCMBaseIntentService.java:296)
            at com.google.android.gcm.GCMBaseIntentService.onHandleIntent(GCMBaseIntentService.java:197)
            at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.os.HandlerThread.run(HandlerThread.java:61)

Here is the code sample :

Module

angular
        .module('app.notifications', [])
        .run(runBlock);

runBlock.$inject = ['PushFactory'', '$log', '$window'];

    function runBlock(PushFactory, $log, $window){
        document.addEventListener('deviceready', function () {
            PushFactory.init();
        });
    }

PushFactory

angular
        .module('app.notifications')
        .factory("PushFactory", PushFactory);


    PushFactory.$inject = ['notifConfig', '$window', 'EventBroadcasterMixin', 'notifEvents', '$log', 'CommonMixin', '$http'];
    function PushFactory(notifConfig, $window, EventBroadcasterMixin, notifEvents, $log, CommonMixin, $http){

        var service = {
            //Initialize Push notifications
            init: _init,
            getRegistrationID : _getRegistrationID,
            getCurrentNotification : _getCurrentNotification,
            onNotification: _onNotification
        };

        var inited = false;

        angular.extend(service, EventBroadcasterMixin.prototype);
        angular.extend(service, CommonMixin.prototype);

        return service;

        var registrationID = "";
        var _currentNotification = null;
        var pushService = null;

        //Global Namespace for Notifications -----------------------------
        function _onNotification(e) {
            // data.message,
            // data.title,
            // data.count,
            // data.sound,
            // data.image,
            // data.additionalData

            _currentNotification = e;
            service.dispatch(notifEvents.MESSAGE_RECEIVED, {action:"increment"});
        }
        //----------------------------------------------------------------

        function _getCurrentNotification(){
            return _currentNotification;
        }

        function _getRegistrationID(){
            $log.log("PushFactory::_getRegistrationID");
            return registrationID;
        }

        function _errorHandler(error) {
            // Return error on registration for all os

            $log.warn("PushFactory._errorHandler");
            $log.warn(error);
        }



        function _registerDeviceToken(pData) {
            $log.log("PushFactory._registerDeviceToken");
            $log.log("PushFactory : uuid => "+pData.user);
            $log.log("PushFactory : type => "+pData.type);
            $log.log("PushFactory : token => "+pData.token);

            registrationID = pData.token;
        }



        function _init() {

            $log.log("PushFactory.init");

            try {

                if (!inited) {
                    inited = true;

                    var pushNotification = window.PushNotification;

                    if (pushNotification) {
                        pushService = PushNotification.init({
                            android: {
                                senderID: notifConfig.ANDROID.PROJECT_ID
                            },
                            ios: {}
                        });

                        pushService.on('registration', function (data) {
                            // data.registrationId
                            switch (device.platform) {
                                case 'android':
                                case 'Android':
                                    _registerDeviceToken({
                                        'user': device.uuid,
                                        'type': 'android',
                                        'token': data.registrationId
                                    });
                                    break;
                                default:
                                    _registerDeviceToken({
                                        'user': device.uuid,
                                        'type': 'ios',
                                        'token': data.registrationId
                                    });
                                    break;
                            }
                        });

                        pushService.on('notification', function (data) {
                            $log.log('PushFactory MESSAGE RECEIVED !', data);
                            // data.message,
                            // data.title,
                            // data.count,
                            // data.sound,
                            // data.image,
                            // data.additionalData
                            _onNotification(data);
                        });

                        pushService.on('error', function (e) {
                            $log.log('PushFactory ERROR : ', e);
                            _errorHandler(e);
                        });
                    }
            }
            } catch(error) {
                $log.warn("PushFactory.init | ERROR");
                $log.warn(error.message);
            }
        }

    }

@macdonst
Copy link
Member

@FabienHenon thanks for the logcat. I just pushed what I hope is a work around for you. Please test with the latest as it should stop crashing now.

Also, your registration code can probably be simpiler:

pushService.on('registration', function (data) {
         _registerDeviceToken({
              'user': device.uuid,
              'type': device.platform.toLowerCase(),
              'token': data.registrationId
         });
});

@fredgalvao
Copy link
Collaborator

I'm curious as to why this hasn't happened all the time... I'd guess this is related to something else entirely or based on some performance metric or race condition.

@macdonst
Copy link
Member

@fredgalvao you and me both. I still think the guard code I put in is valid regardless but I'm not at a place where I can dig in, in depth.

@macdonst macdonst added the bug label Sep 18, 2015
@macdonst
Copy link
Member

@FabienHenon did you re-test with master? Please let us know if this resolves the problem.

@FabienHenon
Copy link
Author

@macdonst I'm sorry, I couldn't test yet. I'll let you know as soon as I try again (probably next monday).

@macdonst
Copy link
Member

Kk, I want to get a release out this weekend. If this doesn't work around
your problem I'll have to do a bug release.
On Sep 18, 2015 5:29 PM, "Fabien Henon" notifications@github.com wrote:

@macdonst https://github.com/macdonst I'm sorry, I couldn't test yet.
I'll let you know as soon as I try again (probably next monday).


Reply to this email directly or view it on GitHub
#144 (comment)
.

@FabienHenon
Copy link
Author

@macdonst I tried on ios but I don't seem to receive any registration ID (my code hasn't change since last time).

On Android, is the com.android.support:support-v13:23+ required ? I only have it to version com.android.support:support-v13:22

I changed nothing on my code but I'm now using the latest version from github of the plugin

@macdonst
Copy link
Member

@FabienHenon That's odd about iOS but we should track that in a different issue.

Yes on Android we now require support library v13 release 23 or higher. That is easy to update just by going into the Android SDK Manager and updated the Android Support Library.

@macdonst
Copy link
Member

@FabienHenon did you ever get a chance to test this?

@FabienHenon
Copy link
Author

Sorry but no.
I could just figure out that the issue with iOS was because we were using another plugin that was also using the notification system.

Le 12 oct. 2015 à 23:36, Simon MacDonald notifications@github.com a écrit :

@FabienHenon did you ever get a chance to test this?


Reply to this email directly or view it on GitHub.

@macdonst
Copy link
Member

I'm closing this as I believe it to be fixed. Re-open if it can be reproduced.

@fredgalvao fredgalvao mentioned this issue Nov 22, 2016
21 tasks
@lock
Copy link

lock bot commented Jun 5, 2018

This thread has been automatically locked.

@lock lock bot locked as resolved and limited conversation to collaborators Jun 5, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Development

No branches or pull requests

3 participants