1

I have seen multiple similar items but no one was in the same context (with angular, not using ionic). I am using cordova 5.0.0 (cordova --version in cmd shows 5.0.0, in the device plugin module I see 3.6.4)

I have an angular based application that runs inside a Cordova application. I am trying to add push notification using the PushPlugin.

I am able (inside the main module) to call the register method of the push plugin and in Android (currently only device I test on) the "success" handler is called. The ecb though is never called regardless of where I put the callback function.

In my app.js:

angular.module('myApp', [...]).config().run(['$rootScope',..., 
function($rootScope,...) {
    // etc etc...
    document.addEventListener("deviceready", function(){
        var pushNotification = window.plugins.pushNotification;
        pushnotification.register(
            successHandler,
            errorHandler,
            {
                "senderID":"<sender_id>",
                "ecb":"window.onNotification"
            });
    });

    // this is invoked
    function successHandler (result) {
        alert('result = ' + result);
    };

    function errorHandler (error) {
        alert('error = ' + error);
    };

    // option 1:
    function onNotification(e) {...};

    // option 2:
    var onNotification = function(e) {...};

    // option 3 
    // (tried below and above the call to register
    // though I believe it doesn't matter):
    window.onNotification = function(e) {...};
}]);

// option 4:
var onNotification = function(e) {...};

So far, none of them work. I assume that I am doing something wrong with the scope but I am not sure what.

Is it something with the scope? Could it be something else? What? How to diagnose?

EDIT: I checked the logcat and some thing does not make sense:

I/chromium       ( 3981): [INFO:CONSOLE(217)] "registering with GCM", source: file:///android_asset/www/js/app.js (217)
V/PushPlugin     ( 3981): execute: action=register
V/PushPlugin     ( 3981): execute: data=[{"senderID":"SENDER_ID","ecb":"window.onNotification"}]
V/PushPlugin     ( 3981): execute: jo={"senderID":"SENDER_ID","ecb":"window.onNotification"}
V/PushPlugin     ( 3981): execute: ECB=window.onNotification senderID=SENDER_ID
D/GCMRegistrar   ( 3981): resetting backoff for com.my.app
V/GCMRegistrar   ( 3981): Registering app com.my.app of senders SENDER_ID
W/ActivityManager( 1254): Unable to start service Intent {act=com.google.android.c2dm.intent.REGISTER pkg=com.google.android.gsf (has extras) } U=0: not found

The plugin version I see (cordova plugin list) is 2.4.0. How come the intent is c2dm? Shouldn't it use the newer GCM?

Tomer Cagan
  • 1,078
  • 17
  • 31
  • 1
    I seems to be a scope issue. Refer to this [example](http://stackoverflow.com/a/30634495/1761793). The ecb should be available in the global scope when the plugin wakes your application. Attach your function to global `window`, and refer to it as `window.onNotification`. – Ajoy Jul 09 '15 at 04:22
  • 1
    Also see this (better) [answer](http://stackoverflow.com/a/25427145/1761793). – Ajoy Jul 09 '15 at 04:31
  • I did as suggested - adding the `onNotification` as a property on `window` and using the fully qualified name `"window.onNotification"` as the value for ecb. I am still not getting the callback fired... could it be that I am not getting a response at all? how can I check this? – Tomer Cagan Jul 09 '15 at 09:59
  • 1. Add `alert` or `console.log` statements to your ecb. 2. Open your app in your emulator, device or browser. Check if the alerts pop up. 3. If you have any error-prone code in the ecb use [try...catch](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/try...catch) to find and print the errors, if any. – Ajoy Jul 09 '15 at 10:07
  • Hi, I have the stuff you suggested and looked at it through chrome remote debugging. The notification is never called. I may have found some issue using logcat - but not sure what the cause/implications... could it be that the ecb is never actually called? – Tomer Cagan Jul 09 '15 at 10:20
  • That is generally not the case. I have used PushPlugin with both EmberJS and AngularJS (ionic). The only issues were because of an error in my setup or undocumented features. See this [answer] for instance. PushPlugin must be properly initialized. – Ajoy Jul 09 '15 at 10:26
  • This was this [answer](http://stackoverflow.com/a/27577635/1761793) I was referring to in my previous comment. – Ajoy Jul 09 '15 at 10:37

1 Answers1

2

As it turns out, it was an issue with the emulator.

All the comments above are correct as to the scoping of the notification event callback (ecb), still, what led me to finally having the code work in the emulattor was the logcat printout. The warning about not being able to start the registration service intent since it is missing (see last line of the log excerpt in the question) was the actual issue.

Googling for that warning, I have found this discussion: not notification on Android with GCM - the answer from 2015-07-01 by hmedney suggests to use a different device target in the emulator. After changing the emulator to use "Google APIs (x86 System Image)(Google Inc.) - API Level 19" the onNotification function is called and I see in my log the registration ID.

Tomer Cagan
  • 1,078
  • 17
  • 31