Поделиться через


Send push notifications to authenticated PhoneGap users

Let me start out by letting folks know that feedback from my blog post Push Notifications to PhoneGap Apps using Notification Hubs Integration was so positive that the Mobile Services team has added push support to the official HTML/JavaScript client. This means that you can now register for push notifications using the Push object on the client (example code and instructions are still pending….). Note that this built-in support is only for iOS and Android clients (Windows Phone doesn’t make the cut, at this point, so if you want to support WP folks, stay with my blog post).

We have other Mobile Services tutorials that demonstrate how to use tags to send push notifications to specific users, and I wanted to let folks know how do this also for PhoneGap clients. The basic process in a nutshell is this:

  1. Authenticate the client before registration. Because you need to lock-down the registration endpoint to only allow authenticated users, you will get a 401 if you try to register for push notifications before you authenticate the user. Follow Add authentication to your Mobile Services app see how this is done. Note that for a PhoneGap app, you must also add the following plugins to the project:
    • https://git-wip-us.apache.org/repos/asf/cordova-plugin-device.git
    • https://git-wip-us.apache.org/repos/asf/cordova-plugin-inappbrowser.git
  2. Restrict your registration endpoint to only authenticated users. Follow steps 1 and 2 in the section Update the service to require authentication to register.
  3. Hook-into the registration process to add a userId tag to the incoming registration. The code that does this for a JavaScript backend mobile service is shown in step 3 in Update the service to require authentication to register.
  4. Now, you can send push notifications to specific signed-in users by sending a tag-based push. See step 5 in Update the service to require authentication to register for an example of sending a tag-based notification from a JavaScript backend mobile service.
  5. To make this all truly useful, you will probably need to add a table that maps between userIds and something interesting that you are tracking, or you might want to add additional tags based on who is logged in, but this is the basic gist.
    Just an FYI, you can get more info (which might make good tags) from the identity provider (e.g. Facebook) on the server-side. See Carlos’ blog post on the Enhanced Users feature for more about this.

Hope that helps PhoneGap folks move things forward. We will be providing more Azure content for PhoneGap in the near future.

Cheers!

Glenn Gailey