[SOLVED] Raix:Push Notification


#1

Hi

I’m starting to wrapp up the production of my first phone app with meteor and i’m trying to get push to work but am struggling. This is basically the last part of the puzzle but as i said, i’m having a struggle.

I populate the collection by simply sending a push notification, so far so good. But i have no idea on how I can see if the client recieve a push or not. I’ve tried with the following code:

Meteor.startup(function () {
if (Meteor.isCordova) {
window.alert = navigator.notification.alert;
}

Push.addListener('alert', function(notification) {
    // Called on every message
    Dialog.alert(notification.message);
console.log(notification.message);
});

});

but as far as i can tell, this code never runs.
Could anyone point me in the right direction please? :slight_smile:

Cheers

edit: Might also add that there are no errors displayed when turning on the Push debug.
And certs seems to be in order since it gives me the option to not display push notices.


#2

Are you testing in the simulator or device?

I had a similar problem a couple of days ago and there was an unsupported error thrown on the simulator. If you add these lines, you can verify whether you’re getting an error

Push.addListener('error', function(err) {
  console.log(err);
});

#3

Hey! Cheers for your reply.

I’m testing on device.
I’ll give the code snipper you posted a go.


#4

Is is Android or IOS?
Do you receive a token? Are there any error messages?
On IOS for debug and adHoc builds, you need to use production: false. For AppStore builds, including TestFlight use production: true.

Here are some code snippets from our ios project. In client/startup.coffee

Push.debug = true
Push.addListener 'token', (token) ->
  console.log('token received: ' + JSON.stringify(token))

And this script from client/push.coffee we use for debug purposes.

pushNotification = undefined

Meteor.startup ->
  if Meteor.isCordova
# onDeviceReady = ->
    console.log 'startup event received'
    try
      pushNotification = window.plugins.pushNotification
      console.log 'registering ' + device.platform
      if device.platform == 'android' or device.platform == 'Android' or device.platform == 'amazon-fireos'
        # TODO actual notifications android code
      else
        pushNotification.register tokenHandler, errorHandler,
          'badge': 'true'
          'sound': 'true'
          'alert': 'true'
          'ecb': 'onNotificationAPN'
        # required!
    catch err
      txt = 'There was an error on this page.\n\n'
      txt += 'Error description: ' + err.message + '\n\n'
      alert txt
    return

# handle APNS notifications for iOS
window.onNotificationAPN = (e) ->
  if e.alert
    console.log 'push-notification: ' + e.alert
    # showing an alert also requires the org.apache.cordova.dialogs plugin
    # navigator.notification.alert e.alert
  if e.badge
    pushNotification.setApplicationIconBadgeNumber successHandler, e.badge
  return

# handle GCM notifications for Android
# not used now
onNotification = (e) ->
  console.log 'EVENT -> RECEIVED:' + e.event
  switch e.event
    when 'registered'
      if e.regid.length > 0
        console.log 'REGISTERED -> REGID:' + e.regid
        # Your GCM push server needs to know the regID before it can push to this device
        # here is where you might want to send it the regID for later use.
        console.log 'regID = ' + e.regid
    when 'message'
      # if this flag is set, this notification happened while we were in the foreground.
      # you might want to play a sound to get the user's attention, throw up a dialog, etc.
      if e.foreground
        console.log 'INLINE NOTIFICATION--'
      else
        # otherwise we were launched because the user touched a notification in the notification tray.
        if e.coldstart
          console.log '--COLDSTART NOTIFICATION--'
        else
          console.log '--BACKGROUND NOTIFICATION--'
      console.log 'MESSAGE -> MSG: ' + e.payload.message
      #android only
      console.log 'MESSAGE -> MSGCNT: ' + e.payload.msgcnt
      #amazon-fireos only
      console.log 'MESSAGE -> TIMESTAMP: ' + e.payload.timeStamp
    when 'error'
      console.log 'ERROR -> MSG:' + e.msg
    else
      console.log 'EVENT -> Unknown, an event was received and we do not know what it is'
      break
  return

tokenHandler = (result) ->
  console.log '[NOTIFICATIONS] token: ' + result
  # Your iOS push server needs to know the token before it can push to this device
  # here is where you might want to send it the token for later use.
  return

successHandler = (result) ->
  console.log '[NOTIFICATIONS] success:' + result
  return

errorHandler = (error) ->
  console.log '[NOTIFICATIONS] error:' + error
  return

#5

Hi!
Thank you for taking your time to respond.

I do recieve a token, what i can see in my db. The two devices(Both ios, havn’t tried android yet) that i’ve tried on are each appointed a token. I run the ios-device meteor command to build on my devices.

I do not receive any server, nor client side errors as far as i can tell.
But the listeners doesn’t seem to be executed on startup.

To give an more indepth look at my file structure - what’s included:

config.push.json - Root folder
//passwords has been changed since i managed to include one of them lol

"apn": {
    "passphrase": "xxxxxx",
    "key": "apn-production/key.pem",
    "cert": "apn-production/cert.pem"
  },
  "apn-dev": {
    "passphrase": "xxxxx",
    "key": "apn-development/key.pem",
    "cert": "apn-development/cert.pem"
  },
  "production": false,
  "badge": true,
  "sound": true,
  "alert": true,
  "vibrate": true
}

App.js - client folder

if(Meteor.isClient){
  Meteor.startup(function () {
    console.log('is up');
      if (Meteor.isCordova) {
      console.log('is cordova');
          window.alert = navigator.notification.alert;
      }
    //Is not working
    Push.addListener('message', function(notification) {
      alert(notification.message);
      console.log(notification);
    });
  });
}

To send push i use meteor shell which returns an id and stores it in the db.

Push.send({
  from: 'Test',
  title: 'Hello',
  text: 'World',
  badge: 12,
  query: {}
});

So what the problem seems to come down to is that the event listeners aren’t triggered.


#8

Is there a reason you marked this as solved? I am having problems testing push notifications too. I will test the stuff you have here and I will add if I have any break throughs


#9

@tejpen Please how did you end up solving this problem where the event listeners are not getting triggered? I am still having the same problem


#10

Hey!
Sorry for not getting back to you. I’ve had a few busy days with the release coming up.

Could you provice me with your config.push.json file(remove all sensitive data) as well as the client/server side code and i’ll take a look at it. :slight_smile:


#11

@tejpen here is the code. I am currently only working on android for now. I have added the raix:push package and all the dependencies. I have a bunch of listeners added (which you will see below) but the only one that seems to work is the token listener which ouputs a token. When I trigger a Push.send I get back an ID. There are no errors. I just never recieve anything on the device.

This is the code I run to put the app on the device (HTC Desire) the sub domain points to the meteor app. Sorry for all the info dump. I have been stumped for a week and have been trying to see what I am doing wrong but cannot figure it out. Thanks for all you anticipated help

meteor run android-device --mobile-server http://app.mywebsite.com --settings settings.json

config.push.json

{ 
 "gcm": {
          "apiKey": "xxxxxxxxxxxxxxxxx",
          " projectNumber": xxxxxxxxxxxx
},
"production": true
}

client/common/startup.js

Meteor.startup(function(){

  if (Meteor.isCordova){
      Push.debug = true
      console.log('startup event cordova');
      window.alert = navigator.notification.alert;

     //ONLY This works
     Push.addListener('token', function(token) {
     console.log('token received: ' + JSON.stringify(token));
    });

    Push.addListener('error', function(err) {
     console.log(err);
    });
    Push.addListener('message', function(notification) {
     // Called on every message
     console.log(JSON.stringify(notification))
     alert(notification.message);
   })

   Push.addListener('alert', function(notification) {
     // Called on every message
     Dialog.alert(notification.message);
     console.log(notification.message);
   });

)}

The above are the two files I use and then after I use mup to deploy my code to my server (digital ocean) I go into the the web inspector/console and paste this

Push.send({
  from: 'test app',
  title: 'hello',
  text: 'hello world',
  query: {}
});

#12

@tejpen Ah never mind

I switched the project number and apikey to a new GCM project and the code above just started working. I have no idea what the difference is but it all works fine now. Thanks for all your help


#13

+1 not storing device token @adim86 any help


#14

I did not have any problems with the device tokens. I tested on an HTC Desire try adding a listener like I added above.


#15

@adim86 Yes I have added listener i got device token in following listener but appCollection is still empty

Push.addListener(‘token’, function(token) {
console.log('token received: ’ + JSON.stringify(token));
});


#16

Can it runs during the app is turned off?


#18

I think it can’t. Because push.addListener would not run anymore when app is destroyed (swipe from recent apps)


#19

raix:push will run if the app is destroyed. it will start up the app when the gcm notification is received from the server.


#20

thank you for the information :slight_smile:


#21

We used to have a _raix_push_app_tokens collection containing the tokens for each user, but after reseting our test database, this collection stays empty when we recreate our users. We dropped this collection, and now it is no longer being created. We do, however, see _raix_push_notifications.

We think this might be the reason that no pushes are being sent.

Any idea where the tokens are stored, or why _raix_push_app_tokens has disappeared?