How to use Parse Push with Cordova, Appcelerator,

2019-02-16 05:42发布

问题:

Parse's JavaScript SDK doesn't have support for registering for push notifications, but Parse knows how to send pushes to iOS and Android devices. Is there any way to get set up for push with iOS, Android, etc?

回答1:

It's true that Parse's JS SDK doesn't have direct support for registering a device for push notifications, but the system is documented well enough that it shouldn't be difficult to reverse engineer (I've seen it done successfully). You tell Parse about a device to which you can send a push by creating an Installation object for that device. This class has well-known fields which are documented. In JavaScript, this class is pre-defined as Parse.Installation. To register your device for push on iOS, set the following fields:

  • deviceType: "ios"
  • deviceToken: a hex64 string of the second parameter returned in the ApplicationDelegate method application:didRegisterForRemoteNotificationsWithDeviceToken:
  • timeZone (optional): a TZ database format time zone (e.g. America/Los_Angeles). This is only necessary if you want to use local push scheduling
  • badge (optional): If you send pushes that increment badge counts, you need to use this to tell Parse what the current badge count on the device is. This lets us know what to increment from.

To register an android device, request a GCM token with Parse's GCM sender ID (1076345567071). Then create a Parse.Installation with the following fields:

  • deviceType: "android"
  • pushType: "gcm"
  • deviceToken: the "registration_id" extra returned by the com.google.android.c2dm.intent.REGISTRATION intent.
  • timeZone (optional): a TZ database format time zone (e.g. America/Los_Angeles). This is only necessary if you want to use local push scheduling

The main caveat is that you'll want to save your Parse.Installation locally and only send new requests when the Installation is first created or you have detected changes; otherwise you'll waste API requests every time your app is launched.