Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 697
zch Avatar asked Mar 23 '14 22:03

zch


People also ask

How do I send push notifications to iOS?

First, you enable push notifications in the Xcode project. Select your project in the project navigator and click on the Capabilities tab. Enable push notifications by turning the switch ON. APNs will respond with a device token identifying your app instance.

How do I allow apps to send push notifications?

From the “Settings” menu, tap “Notifications”. From here, find the app you wish to receive push notifications for. From here, tap “Allow Notifications” and then choose your options for how you wish to receive push notifications: a.

Do push notifications work on iOS?

An iOS push notification is a message that pops up on an Apple device such as an iPhone. Before receiving push notifications from an app, iOS device users must explicitly give permission. Once a user opts-in, mobile app publishers can send push notifications to the users' mobile devices.

Can you send push notifications without an app?

Pushed allows you to send real-time notifications without developing your own app to iOs, Android and Desktop devices.


1 Answers

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:

  • Token Generation and Dispersal, Apple docs

  • How to get a device token, see Step 10 in Sample IOS App section of article

  • Sending a Notification, apn library doc

like image 176
alanning Avatar answered Sep 28 '22 05:09

alanning