How can one send iOS push notifications from a Meteor app?
Asked Answered
M

3

10

I have failed to find a comprehensive tutorial for sending push notifications from a Meteor app. I understand that Meteor runs using node.js, so I have been following this highly referenced and recommended tutorial https://blog.engineyard.com/2013/developing-ios-push-notifications-nodejs but to no avail; upon deploying my app, it uploads but the website is then unresponsive (and given there are no errors with deployment, I cannot see where the problem lies, though I presume it has to do with how I'm organizing my files).

I have downloaded and loaded all of the certificates properly per the tutorial's instruction. I have used their example app to properly get my test device's ID. I just cannot figure out where to properly place additional files and dependencies given Meteor's folder configuration. How should it differ from the structure in the tutorial (in other words should this structure be placed inside of a folder within the .meteor folder of my app)?

I think the overarching issue is that Meteor simply structures their apps differently than plain Node.js apps, and as such I need to be placing these certificates and dependencies in a specific folder, not just the main application folder with application.html, application.js, and application.css.

Mebane answered 23/3, 2014 at 22:33 Comment(4)
You probably need to wrap apnagent into a new Meteor package. See github.com/oortcloud/…Epicene
@GeoffreyBooth I completely agree, and after reading that document and following a relevant tutorial, I get an Uncaught TypeError: undefined is not a function that I believe is referencing var apn = Meteor.require('apnagent'); even though it's been downloaded as a dependency properly. Could this be the case?Mebane
Without seeing your code I have no idea, and I've never tried to set up APN myself; I was just pointing out how to work with NPM packages in Meteor in general. I would suggest you follow @alanning's answer as it appears he's actually gotten this to work :)Epicene
github.com/raix/push Make sure you doublecheck your bundle identifier.Doreendorelia
S
14

We use the apn npm package with our Meteor app. We looked at apnagent (because of that same tutorial) but went with apn due to its greater popularity. Although apnagent should also work fine within your Meteor app, you may want to try apn just to troubleshoot.

We set it up server-side like this...

var apn = Meteor.require("apn"),
    path = Npm.require('path'),
    apnOptions = Meteor.settings.apnOptions || {},
    alertSound = apnOptions.sound || "alert.aiff",
    apnConnection

// default apn connection options
apnOptions = _.extend({
  cert: path.join(appRootPath, "private", "cert.pem"),
  key: path.join(appRootPath, "private", "key.pem"),
}, apnOptions)
apnConnection = new apn.Connection(apnOptions)

...and use it like this:

  sendAppleNotifications: function (alert, url, pushIds) {
    var note = new apn.Notification()

    // expires 1 hour from now
    note.expiry = Math.floor(Date.now() / 1000) + 3600
    note.sound = alertSound
    note.alert = alert
    note.payload = {'url': url}

    _.each(pushIds, function (token) {
      var device = new apn.Device(token)
      apnConnection.pushNotification(note, device)
    })

    return {success:'ok'}
  },  // end sendAppleNotifications

Note that Meteor.require is enabled by the npm meteor package which you can read about here. Alternately you could just put your code that uses the apn package in your own Meteor package and use Npm.require as @GeoffreyBooth suggested.

==

June 20, 2015 - Update

I recently answered a question about device tokens; hope these resources are helpful:

Strike answered 25/3, 2014 at 13:38 Comment(2)
Excellent explenation. Thank you for providing such an in depth walk throughMebane
In this example, how do you obtain the (alert, url, pushIds) parameters?Doreendorelia
R
3

Using the Cordova PushPlugin, you will have to:

  • roll your own integration with the Meteor user model, so that you can select which users to send your push notifications to.
  • generate your own certificates,
  • securely store the associated .pem and other authorization files.

In fact, it's quite a bit of work.

raix:push "solves" the first issue, allowing a push notification to be delivered to a user or group of users. However:

  • it's no longer maintained.
  • it does not (in my experience) work anymore. iOs notifications show up but they don't vibrate the phone or make any sound. Some people have proposed hacks to get around it but none of them work for me.
  • it asks you to version your production passwords and .pem files, which is certainly not considered safe practices for a proper 12-factor app.

Pushwoosh is a service that makes it easy to send push notifications to your app, and includes automatic setup and handling of your .pem files and certificates and a super rich feature set.

lpender:meteor-pushwoosh is a package I wrote, which

  • Allows your app to create and receive push notifications.
  • Allows querying by user when creating a notification.
  • Works for iOs and Android devices.

Keep in mind, it's not totally clear from their pricing page, but after the free trial, it's $49 a month to continue use their API to programmatically generate messages from your app.

Remittent answered 27/4, 2016 at 0:12 Comment(1)
Whats up Lee! It's niko. Small world :)Doreendorelia
A
2

If you only care about iOS notifications, the apn package mentioned by alanning might do the job.

Meteor however has a package, raix:push that's far easier to use, and supports notifications on APN iOS, GCM Android, and partially on a few other systems (APN Safari web, GCM Chrome OS, Firefox OS).

Adopt answered 24/2, 2016 at 22:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.