Meteor-Community-Packages / raix-push

DEPRECATED: Push notifications for cordova (ios, android) browser (Chrome, Safari, Firefox)
https://atmospherejs.com/raix/push
MIT License
513 stars 197 forks source link

Error: Push.sendGCM.... has no method 'sendGCM' - EDIT FOUND WORKAROUND #23

Open adam-hanna opened 9 years ago

adam-hanna commented 9 years ago

EDIT - FOUND WORKAROUND! See one of my comments, below.

I had push notifications working on Android in one of your old packages, but not anymore. Here's the error I'm getting. I'll post my setup below. Note the method 'sendMessage' is a server side method that I'm calling from the client to send a push notification (I'm sending notifcations out from the server).

I'm getting the following error with Push.sendGCM and Push.sendAPN. I'm also not able to get Push.send to work.

Here's my effort with Push.sendGCM. Scroll much further down below to see my setup for Push.send

error in the server console

I20150109-19:10:39.952(-5)? Exception while invoking method 'sendMessage' TypeError: Object #<EventEmitter> has no method 'sendGCM'
I20150109-19:10:40.930(-5)?     at Meteor.methods.sendMessage (app/server/server.js:160:12)
I20150109-19:10:40.931(-5)?     at maybeAuditArgumentChecks (packages/ddp/livedata_server.js:1599)
I20150109-19:10:40.931(-5)?     at packages/ddp/livedata_server.js:648
I20150109-19:10:40.932(-5)?     at _.extend.withValue (packages/meteor/dynamics_nodejs.js:56)
I20150109-19:10:40.932(-5)?     at packages/ddp/livedata_server.js:647
I20150109-19:10:40.932(-5)?     at _.extend.withValue (packages/meteor/dynamics_nodejs.js:56)
I20150109-19:10:40.933(-5)?     at _.extend.protocol_handlers.method (packages/ddp/livedata_server.js:646)
I20150109-19:10:40.936(-5)?     at packages/ddp/livedata_server.js:546

App Structure

client\client.js
server\server.js
lib
private
public
resources
config.push.json
mobile-config.js

client.js

Meteor.call("sendMessage", options, function (err, res) { ... };

Push.addListener('token', function(token) {
    if (token) {
        Meteor.call("updatePushToken", token, function(err, res) {});
    };
});

Push.id(); // Unified id - not a token

server.js

Push.debug = true;

// Internal events
// do we need this both on client and server?
Push.addListener('token', function(currentToken, newToken) {
    // Token is { apn: 'xxxx' } or { gcm: 'xxxx' } or null
    // if newToken is null then the currentToken is invalid
    // if newToken is set then this should replace the currentToken
    if (newToken) {
        Meteor.users.update( { _id: this.userId }, { $set: { pushToken: newToken } } );
    };
});

Meteor.methods({
    'updatePushToken': function(token) {
        Meteor.users.update( { _id: this.userId }, { $set: { pushToken: token } } )
    },
    'sendMessage': function(options) {
        //some code that looks up the gcm token of the recipient has been ommitted
        //code code code

        Push.sendGCM(
            { gcm: 'the token for the user I am sending to }', 
            {
                from: options.stuff,
                title: options.title,
                text: options.text
            }
        );
    }
});

config.push.json

{
  "apn": {
    "passphrase": "myPassword",  
    "key": "myKey",
    "cert": "myCert"
  },
  "gcm": {
    "apiKey": "myAPIKey",
    "projectNumber": myNumber
  },
  "production": false,
  // "badge": true,
  // "sound": true,
  "alert": true,
  "vibrate": true
}

Here's my effort for Push.send. Everything is the same except the end of the server method "sendMessage". This successfully writes to the collection "_raix_push_notifications" but I never receive a notification.

server/server.js

//inside the server method "sendMessage"
Push.send({
    from: senderUsername,
    title:  'New BCBC video!',
    text:   oRecipientVideo.title,
    // query: {}
    token:  token object from elsewhere in the code // token: appId or token eg. "{ apn: token }"
    // tokens: array of appId's or tokens
    // payload: user data
});

_raix_push_notifications

{
  from: "stuff",
  title: "stuff",
  text: "stuff",
  count: 0,
  createdAt: ISODate("2015-01-10T00:32:04.000Z"),
  createdBy: "<SERVER>",
  token: {
    gcm: "token string"
  },
  _id: "id stuff"
}
adam-hanna commented 9 years ago

Am I supposed to run Push.Config somewhere and point it to my json file?

adam-hanna commented 9 years ago

This is very weird. When I delete my json file and instead use Push.Configure({ ... }), I get a very similar error:

"TypeError: Object # has no method 'Configure'"

adam-hanna commented 9 years ago

FOUND A WORKAROUND!!!

Removing the json file and putting Push.Configure inside of a Meteor.startup on the server worked!

raix commented 9 years ago

Thats odd, try setting 'meteor --production'

adam-hanna commented 9 years ago

:( same error

adam-hanna commented 9 years ago

scrolled through the long log and found this:

Push configuration, Error: Unexpected token / SyntaxError: Unexpected token /
    at Object.parse (native)
    at Package (packages/configuration/plugin/push.configuration.js:135)
    at __bottom_mark__ (/Users/macintosh/.meteor/packages/meteor-tool/.1.0.35.wql4jh++os.osx.x86_64+web.browser+web.cordova/meteor-tool-os.osx.x86_64/tools/parse-stack.js:92:14)
adam-hanna commented 9 years ago

I'm an idiot. I had left javascript single line comments in my json (i.e. // ). It works now, except I'm getting this error:

I20150110-02:34:55.262(-5)? Create GCM Sender using "my api key"
I20150110-02:34:55.263(-5)? A:Send message to: my-phones-gcm-key
I20150110-02:34:55.732(-5)? ANDROID: Result of sender: {"multicast_id":4672542067878773000,"success":0,"failure":1,"canonical_ids":0,"results":[{"error":"NotRegistered"}]}

Maybe my gcm key has changed and it's not being updated to my server? It's weird that i had to put Push.Configure into a meteor startup function. Maybe my key listeners have to go inside a meteor.startup function as well?

raix commented 9 years ago

True the config have to be plain json - It would be nicer if it was plain javascript - but it would take more effort to implement etc.

{"error":"NotRegistered"} - it seems as if the client app isnt installed or have registered token on last install?

It would be nice to have a "minimum" push app - I'm giving a speak about this push package at the next copenhagen meteor meetup (Thursday next week) - so I might write a mini app "PushPal" to have as a general test app for credentials etc?

raix commented 9 years ago

Btw. when the error is recieved is the token removed from the appCollection - just qa - it should clean up invalidated tokens.

adam-hanna commented 9 years ago

hmmm.... yes, it was removed from "_raix_push_app_tokens".

I had set listeners on both the client and server for new tokens. Dunno why either of those didn't fire.... :(

adam-hanna commented 9 years ago

I'm a little bit confused. I have multiple tokens in "_raix_push_app_tokens" for single "userId"'s? Is the newest one the correct one?

adam-hanna commented 9 years ago

Alright, I guess feel free to close this. I've taken Push.Configure out of meteor startup and am no longer receiving the error. Strange that it's all of a sudden working!

I also found out why I wasn't able to send a push notification, the following had been logged in my _raix_push_app_tokens:

{
  _id: "Q4gJP84qArqZh6pX7",
  appName: "main",
  createdAt: ISODate("2015-01-09T21:32:15.000Z"),
  updatedAt: ISODate("2015-01-09T21:32:15.000Z"),
  userId: null
}

Notice how there is no token! I was able to retrieve the current token for this device with one of your listeners. Now I just need to figure out why google has invalidated my gcm token and won't give me a new one!

adam-hanna commented 9 years ago

ugh..... this is crashing on my heroku server but working fine on my local machine. So weird. Same issues.

raix commented 9 years ago

Hi @adam-hanna, is this still an issue on the latest version?

miri-am commented 9 years ago

The json file is also not working for me. I get TypeError: Object #<EventEmitter> has no method 'serverSend'. The Push.configureworkaround fixes this for me. This was my config.push.json:

{
  "apn": {
    "passphrase": "test123",  
    "key": "testkey.pem",
    "cert": "testcert.pem",
    "gateway": "gateway.sandbox.push.apple.com"
  },
  "production": false
}
adam-hanna commented 9 years ago

Hey, so sorry for the slow reply!

I just recently fired this project back up, updated to your latest raix:push version, migrated off heroku and onto digital ocean and everything is working great!

One of the changes listed above fixed the issues! Thanks so much for your great work!