Modified action and webhook
2 files changed
tree: f37940c4c303bc5a08ae6f4d2997b27e3ee36634
  1. README.md
  2. sendMessage.js
  3. webhook.js
README.md

Using the Push package

The /whisk.system/pushnotifications package enables you to work with a push service. It includes the following action and feed.

EntityTypeParametersDescription
/whisk.system/pushnotificationspackageappId, appSecretWork with the Push Service
/whisk.system/pushnotifications/sendMessageactiontext, url, deviceIds, platforms, tagNames, apnsBadge, apnsCategory, apnsActionKeyTitle, apnsSound, apnsPayload, apnsType, gcmCollapseKey, gcmDelayWhileIdle, gcmPayload, gcmPriority, gcmSound, gcmTimeToLiveSend push notification to the specified device(s)
/whisk.system/pushnotifications/webhookfeedeventsFire trigger events on device activities (device (un)registration / (un)subscription) on the Push Service
Even though its not mandatory , it‘s suggested that you create a package binding with the appId and appSecret values. This way you don’t need to specify these credentials every time you invoke the actions in the package.

Setting up IBM Push Notifications package

While creating a IBM Push Notifications package you have to give the following parameters,

  • appId: The Bluemix app GUID.
  • appSecret: The Bluemix push notification service appSecret.

The following is an example of creating a package binding.

  1. Create a Bluemix application in Bluemix Dashboard.

  2. Initialize the Push Notification Service and bind the service to the Bluemix application

  3. Configure the IBM Push Notification application.

    Be sure to remember the App GUID and the App Secret of the Bluemix app you created.

  4. Make sure your OpenWhisk CLI is in the namespace corresponding to the Bluemix organization and space that you used in the previous step.

$ wsk property set --namespace myBluemixOrg_myBluemixSpace

Alternatively, you can use

wsk property set --namespace

to set a namespace from a list of those accessible to you.

  1. Create a package binding with the /whisk.system/pushnotifications.
 wsk package bind /whisk.system/pushnotifications myPush -p appId "myAppID" -p appSecret "myAppSecret"
  1. Verify that the package binding exists.
wsk package list
packages
/myNamespace/myPush private binding

Sending Push Notifications

The /whisk.system/pushnotifications/sendMessage action sends push notifications to registered devices. The parameters are as follows:

  • text - The notification message to be shown to the user. Eg: -p text “Hi ,OpenWhisk send a notification”.
  • url: An optional URL that can be sent along with the alert. Eg : -p url “https:\www.w3.ibm.com”.
  • gcmPayload - Custom JSON payload that will be sent as part of the notification message. Eg: -p gcmPayload “{“hi”:“hello”}”
  • gcmSound - The sound file (on device) that will be attempted to play when the notification arrives on the device .
  • gcmCollapseKey - This parameter identifies a group of messages
  • gcmDelayWhileIdle - When this parameter is set to true, it indicates that the message should not be sent until the device becomes active.
  • gcmPriority - Sets the priority of the message.
  • gcmTimeToLive - This parameter specifies how long (in seconds) the message should be kept in GCM storage if the device is offline.
  • apnsBadge - The number to display as the badge of the application icon.
  • apnsCategory - The category identifier to be used for the interactive push notifications .
  • apnsIosActionKey - The title for the Action key .
  • apnsPayload - Custom JSON payload that will be sent as part of the notification message.
  • apnsType - [‘DEFAULT’, ‘MIXED’, ‘SILENT’].
  • apnsSound - The name of the sound file in the application bundle. The sound of this file is played as an alert.

Here is an example of sending push notification from the pushnotification package.

  1. Send push notification by using the sendMessage action in the package binding that you created previously. Be sure to replace /myNamespace/myPush with your package name.
wsk action invoke /myNamespace/myPush/sendMessage --blocking --result  -p url https://example.com -p text "this is my message3"  -p sound soundFileName -p deviceIds '["T1","T2"]'
{
"result": {
"pushResponse": "{"messageId":"11111H","message":{"message":{"alert":"this is my message3","url":"http.google.com"},"settings":{"apns":{"sound":"default"},"gcm":{"sound":"default"},"target":{"deviceIds":["T1","T2"]}}}"
},
"status": "success",
"success": true
}

Firing a trigger event on IBM Push Notifications Service activity

The /whisk.system/pushnotifications/webhook configures the IBM Push Notifications service to fire a trigger when there is a device activity such as device registration / unregistration or subscription / unsubscription in a specified application

The parameters are as follows:

  • appId: The Bluemix push notification service appSecret.
  • appSecret: The Bluemix app GUID.
  • events: Supported events are onDeviceRegister, onDeviceUnregister, onSubscribe, onUnsubscribe.To get notified for all events use the wildcard character *.

The following is an example of creating a trigger that will be fired each time there is a new device registered with the IBM Push Notifications Service application.

  1. Create a package binding configured for your IBM Push Notifications service with your appId and appSecret.
$ wsk package bind /whisk.system/pushnotifications myNewDeviceFeed --param appID myapp --param appSecret myAppSecret --param events onDeviceRegister
  1. Create a trigger for the IBM Push Notifications Service onDeviceRegister event type using your myPush/webhook feed.
 $ wsk trigger create myPushTrigger --feed myPush/webhook --param events onDeviceRegister