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

JS error in "Success callbackId: PushNotifiation###.." whenever a notification is sent to the device #824

Closed
csga5000 opened this issue Apr 21, 2016 · 9 comments
Labels
Milestone

Comments

@csga5000
Copy link

csga5000 commented Apr 21, 2016

Expected Behaviour

The server sends a notification while the app is open and the app's javascript callback push.on 'notification' is called

Actual Behaviour

The server sends a notification while the app is open an an error is written to the console:

Error in Success callbackId: PushNotification1141593012 : TypeError: Cannot call method 'apply' of undefined deviceready.js:5
Uncaught TypeError: Cannot call method 'apply' of undefined push.js:193

Reproduce Scenario (including but not limited to)

Sending a notification to iOS or Android device while device is open

Platform and Version (eg. Android 5.0 or iOS 9.2.1)

Android 4.4.3 and 6.0
iOS 8.4.1

(Android) What device vendor (e.g. Samsung, HTC, Sony...)

ASUS (an old nexus) and HTC (m9)

Cordova CLI version and cordova platform version

cordova --version                                    6.1.1
cordova platform version android           6.1.1
cordova platform version ios                   6.1.1

Plugin version

cordova plugin version   1.6.2

Sample Push Data Payload

Android:
{ body: msg, title: title, icon: "ic_stat_notification" }
iOS:
{ aps: { alert: title, sound: 'true', badge: 1 } }

Sample Code that illustrates the problem

try {
    var app = {
        initialize: function() {
            this.bindEvents()
            this.notification_id = null
        },
        bindEvents: function() {
            document.addEventListener('deviceready', this.onDeviceReady, false)
        },
        onDeviceReady: function() {
            console.log("Device is ready!")
            try {
                app.setupPush()
            }
            catch (err) {
                console.log('Could not setup push notifiations:')
                throw err
            }
        },
        setupPush: function() {
            var push = PushNotification.init({
                android: {
                    senderID: '...'
                },
                ios: {
                    badge: true,
                    sound: true,
                    alert: true
                },
                windows: {}
            })

            PushNotification.hasPermission(function(result){
                if (result.isEnabled) {
                    push.on('registration', function(data){
                        console.log("Registered")
                        console.log(data)
                        app.notification_id = data.registrationId
                    })

                    push.on('notification', this.onNotification)
                }
            })

            push.on('error', function(data) {
                console.log("There was an error with push notificaitons!")
                console.log(data)
            })
        },
        sendDeviceToken: function() {
            ...snip...
        },
        onNotification: function(data) {
            console.log('This data came back: ')
            console.log(data)
        }
    }

    app.initialize()
}
catch (err) {
    console.log("There was an exception setting up app:")
    console.log(err)
}

Logs taken while reproducing problem

Device is ready! deviceready.js:5
Registered deviceready.js:5
Object
 deviceready.js:5
Succeeded. deviceready.js:5
Object {data: Object, status: 200, headers: function, config: Object, statusText: "OK"}
 deviceready.js:5
Error in Success callbackId: PushNotification1340471131 : TypeError: Cannot call method 'apply' of undefined deviceready.js:5
Uncaught TypeError: Cannot call method 'apply' of undefined push.js:193

The last two lines are all that really matter.

Edit: The "Error in Success callbackId" is actually being logged on cordova.js line 268. It says deviceready.js line 5 because I wrapped console.log in my own function

Edit:
Just for the heck of it I did a little debugging. Also, I updated android/my windows machine to 6.1.1.
So the error is on cordova.js line 293.

The exception thrown:

TypeError {stack: "TypeError: Cannot call method 'apply' of undefined…vent (file:///android_asset/www/cordova.js:960:5)", message: "Cannot call method 'apply' of undefined"}

callback.success is:

function (result) {
        if (result && typeof result.registrationId !== 'undefined') {
            that.emit('registration', result);
        } else if (result && result.additionalData && typeof result.additionalData.callback !== 'undefined') {
            var executeFunctionByName = function(functionName, context /*, args */) {
                var args = Array.prototype.slice.call(arguments, 2);
                var namespaces = functionName.split('.');
                var func = namespaces.pop();
                for (var i = 0; i < namespaces.length; i++) {
                    context = context[namespaces[i]];
                }
                return context[func].apply(context, args);
            };

            executeFunctionByName(result.additionalData.callback, window, result);
        } else if (result) {
            that.emit('notification', result);
        }
    } 

And args is:

[{"message":"New Notification","additionalData":{"icon":"ic_stat_notification","foreground":true,"collapse_key":"do_not_collapse"},"title":"ReVision - Notification"}]
@csga5000
Copy link
Author

So I followed the tree and realized that I was incorrectly assigning the callback function.

I changed:

push.on('notification', this.onNotification)

To:

push.on('notification', app.onNotification)

I kind of think the code should present a more useful error if the handler is not valid It's misleading seeing an error in cordova's code right after the android native code callback occurs

@macdonst macdonst added this to the Release 1.7.0 milestone Apr 22, 2016
macdonst added a commit that referenced this issue Apr 22, 2016
@macdonst
Copy link
Member

@csga5000 great catch, I've fixed it for 1.7.0.

@csga5000
Copy link
Author

Awesome, thanks for your effort.

macdonst added a commit that referenced this issue Apr 25, 2016
macdonst added a commit that referenced this issue Apr 29, 2016
macdonst added a commit that referenced this issue May 5, 2016
macdonst added a commit that referenced this issue May 11, 2016
@alrazyreturn
Copy link

alrazyreturn commented May 13, 2016

hi sir ,
I have same issue but when I using local notification kindly help
this is my index.js code

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
var app = {
    // Application Constructor
    initialize: function() {
        this.bindEvents();
    },
    // Bind Event Listeners
    //
    // Bind any events that are required on startup. Common events are:
    // 'load', 'deviceready', 'offline', and 'online'.
    bindEvents: function() {
        document.addEventListener('deviceready', this.onDeviceReady, false);
    },
    // deviceready Event Handler
    //
    // The scope of 'this' is the event. In order to call the 'receivedEvent'
    // function, we must explicitly call 'app.receivedEvent(...);'
    onDeviceReady: function() {
      //  app.receivedEvent('deviceready');
        app.alertFunction();
        app.notifyFunc();
    },

    alertFunction :function () {
                function alertDismissed() {
            welcome
        }

        navigator.notification.alert(
            'You are the winner!',  // message
            alertDismissed,         // callback
            'Game Over',            // title
            'Done'                  // buttonName
        );
    },
    // Update DOM on a Received Event

    notifyFunc :function () {


         var now = new Date().getTime(),
                    _5_sec_from_now = new Date(now + 5 * 1000);

          var sound = device.platform == 'Android' ? 'file://sound.mp3' : 'file://beep.caf';
         cordova.plugins.notification.local.schedule({
                    id: '1',
                    title: 'Scheduled with delay',
                    text: 'Test Message 1',
                    at: _5_sec_from_now,
                   sound : sound,
                    badge: '12'
});

cordova.plugins.notification.local.on("click", function (notification) {
   // joinMeeting(notification.data.meetingId);
});


    },

    receivedEvent: function(id) {
        var parentElement = document.getElementById(id);
        var listeningElement = parentElement.querySelector('.listening');
        var receivedElement = parentElement.querySelector('.received');

        listeningElement.setAttribute('style', 'display:none;');
        receivedElement.setAttribute('style', 'display:block;');

        console.log('Received Event: ' + id);
    }
};
``

it give  me this error:
 [phonegap] [console.log] Error in Success callbackId: Notification1021604172 : ReferenceError: welcome is not defined

kindly help

@macdonst
Copy link
Member

@alrazyreturn well first off you are not using the push plugin you are using the local notification plugin. Regardless, your problem is already diagnosed by the error you see in console.log, welcome is not defined. Look at your function:

function alertDismissed() {
    welcome
}

that function or variable welcome is not defined anywhere.

@alrazyreturn
Copy link

Many thanks sir,
but I solve this issue but it still not working I can't receive any notification ,please note I run this application directly on my mobile using phonegap simulator prog (in google store)
so could you help me please

@macdonst
Copy link
Member

macdonst commented May 15, 2016 via email

@alrazyreturn
Copy link

Many thanks for your replay,
phonegap version 1.6.3

macdonst added a commit that referenced this issue May 25, 2016
macdonst added a commit that referenced this issue May 26, 2016
macdonst added a commit that referenced this issue May 26, 2016
macdonst added a commit that referenced this issue Jun 3, 2016
macdonst added a commit that referenced this issue Jun 6, 2016
@macdonst macdonst removed the staging label Jun 6, 2016
@lock
Copy link

lock bot commented Jun 4, 2018

This thread has been automatically locked.

@lock lock bot locked as resolved and limited conversation to collaborators Jun 4, 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