raix-push icon indicating copy to clipboard operation
raix-push copied to clipboard

GCM: Need some help, hints, details or basics.md

Open pefi1011 opened this issue 9 years ago • 20 comments

pefi1011 avatar Dec 27 '14 15:12 pefi1011

Added a basic walkthrough https://github.com/raix/push#getting-started

I've fixed the match error allowing user to be null

raix avatar Dec 27 '14 16:12 raix

Bumped to version 2.1.3

raix avatar Dec 27 '14 16:12 raix

wooow Morten!! Thanks for your immediate reply!! Looks like the Senta Clause is coming from Denmark :P It works :) I have tough two questions, what is the difference between Push.init() at client and server side? And what would you recommend, where to put Push.allow() code? Thanks ;)

pefi1011 avatar Dec 27 '14 17:12 pefi1011

Thanks :) I see you are somewhere in the milky way?

From a general view Push.init is really all about setting credentials / keys / certificates - Thinking about it it should perhaps be renamed to Push.credentials at some point.

Depending on platforms apn and gcm got different strategies for this + extra strategies if running on safari / chrome os - For security reasons keys and certificates should not be sent to the client.

Push.allow/deny can be set on both client and server - but to have any true effect server-side is required (just like regular allow/deny rules in Meteor - in fact its the same code)

raix avatar Dec 27 '14 19:12 raix

Alright, thanks for your answer, but what I don't understand is why do we set the projectNumber at client side and not at server side..can I remove code from client/client.js and put it in server/server.js like this?

var optionsProduction = {
  gcm: {
    apiKey: 'myApiKey',
    projectNumber: 'myProjectNumber'
  },
  // apn: {
  //   'passphrase': 'xxxxxxx',
  //   // Place the certificate files in /private
  //   'certData': Assets.getText('apnProdCert.pem'),
  //   'keyData': Assets.getText('apnProdKey.pem'),
  // },
  production: true
};

pefi1011 avatar Dec 27 '14 20:12 pefi1011

Its ok to set the projectNumber on server too - but the apiKey may not exist on the client for security reasons. GCM requires this pattern for "pairing" app and server I guess.

So the ideal scenario for this would be that you would not need to call Push.init at all - that we had a general configuration or settings for packages https://github.com/MeteorCommunity/discussions/issues/11

raix avatar Dec 27 '14 21:12 raix

I see, the config like for Nemo64/meteor-bootstrap looks straightforward to me but I'm a beginner..

So I deleted

  Push.init({
    gcm: {
      projectNumber: 'myNumber' 
    }
  });

from client/init.js Meteor.startup() and after trying to send push notification my console said:

I20141227-22:30:42.907(1)? Push: Sent message "Hello World" to 0 ios apps 0 android apps

so I guess GCM couldn't pair the app and server.. After it I did

  • reset meteor

  • meteor run android-device

    but my app didn't want to fetch any data and I didn't get any errors..so I just put the code back to Meteor.startup and executed reset meteor and meteor run android-device so it's working now...

Thank you for your time Morten ;)

pefi1011 avatar Dec 27 '14 21:12 pefi1011

@pefi1011 check out 2.2.0 I've added support for a config.push.json file

raix avatar Dec 28 '14 11:12 raix

btw. I havent doen any QA on it - so it might work/break

raix avatar Dec 28 '14 11:12 raix

how do you manage to do it so fast? Share your secret :P

Unfortunately my app is not working..this is what I did:

  • I added the config.push.json
{
  "gcm": {
    "apiKey": "myApiKey",
    "projectNumber": "myNumber"
  },
  "production": true
}

file and removed other code. the only code remaining is in server/pushServer.js

Push.allow({
  send: function(userId, notification) {
    // Allow all users to send to everybody - For test only!
    return true;
  }
});

Push.debug = true;

but my app is not working anymore..it's showing my loading template and I don't get any errors and when I run Push.send({..}) via meteor shell the push notification doesn't arrive:

I20141228-15:08:45.808(1)? Push: Sent message "Neuer Kommentar" to 0 ios apps 0 android apps

In second case I removed from my router configuration the waitOn function, redeployed the app and still the same..

In third case I put router configuration back and added within Meteor.startup()

  //Fire up push asap
  Push.init({
    gcm: {
      projectNumber: 'myNumber' //
    }
  });

and everything is still the same..

In fourth case I removed waitOn from router config again and nothing changed..

..and at the very beginning I got the following error:

I20141228-14:34:48.026(1) (android:http://meteor.local/packages/raix_push.js:286) Uncaught TypeError: Cannot read property 'pushNotification' of undefined

but I can't reproduce it..

pefi1011 avatar Dec 28 '14 14:12 pefi1011

2.2.1 fixed minor bugs and cleaned up code - let me know if it fixed it

raix avatar Dec 29 '14 10:12 raix

It works now..the only code I have is allow/deny in server/pushServer.js, push.config.json and push.Send() in client/client.js..so no Push.init anymore..thanks ;)

btw do u know why is the app called main?

I20141229-17:24:18.072(1)?   appName: 'main',
I20141229-17:24:18.072(1)?   userId: null,
I20141229-17:24:18.073(1)?   metadata: {} }

and what I noticed, after I logged in into the app, logging says that the push notification has been sent to 2 android apps..

pefi1011 avatar Dec 29 '14 16:12 pefi1011

The app name is for simple namespacing - in theory you could have multiple mobile apps connecting to same notification server (not sure if that would be a common pattern - we still need a control file in Meteor - I think its WIP)

Is it two android apps with different tokens or same? if the same token we should create a new issue,

raix avatar Dec 29 '14 17:12 raix

alright, thanks..

I think its only one android app with one token because I have the app deployed only on one cellphone..

pefi1011 avatar Dec 29 '14 17:12 pefi1011

when you set Push.debug = true - it will print out the gcm tokens it sent to

raix avatar Dec 29 '14 17:12 raix

I have the same tokens..each time I opened the app without being logged in I got "registered", so when I log in I "register" again..so I opened the app 3 times and logged in only the last time and the push notification gets sent 3 times but arrives only once

I20141229-19:33:21.296(1)? Push: Sent message "Neuer Kommentar" to 0 ios apps 3 android apps

pefi1011 avatar Dec 29 '14 18:12 pefi1011

And I get sometimes following error when clicking around the app

I20141229-20:12:16.314(1) (android:http://meteor.local/push.config.web.cordova_plugins.js:1) Uncaught SyntaxError: Unexpected token <

the app crashes after it and I have to close it and open again

pefi1011 avatar Dec 29 '14 19:12 pefi1011

Ill have to do some qa on Android to see the behaviour, it should only create app in Push.appCollection when app gets a token, the userid is updated on existing apps only. Try running meteor shell and look at the appCollection to verify registered apps

raix avatar Dec 29 '14 22:12 raix

The random error lookes like a build error try running with --production (this should bundle files correctly, lets keep an eye on this, we might have to open an issue on meteor if we Can create a reproduction)

raix avatar Dec 29 '14 22:12 raix

tnx

pefi1011 avatar Dec 29 '14 22:12 pefi1011