Push

Push helps you reach out to your users in a timely fashion with important messages. You can use it to inform users about updates to some piece of data, about new services available nearby or that they are winners in a game. Using Custom server code you can set up triggers which send push notifications to a targeted set of people based on changes in the data you specify.

Kinvey brings Android Push Notifications to you through an integration with Google Cloud Messaging (GCM), and iOS Push Notifications through an integration with AWS SNS. While push notifications can only be configured to be used by either iOS or Android devices, our APIs provide support for registering/unregistering devices and associating them with Kinvey users.

Setup

Console Set Up

Android

  1. Navigate to the Firebase Console and follow the guide to create a new project or select an existing project.
  2. Click the gear icon in the top left of the side menu and select Project Settings.
  3. Click on the Cloud Messaging tab and write down the Server Key and Sender ID.
  4. On Kinvey's console select your App.
  5. Navigate to Engagement and select Push.
  6. Click Configure Push.
  7. In the Android section, enter the Server Key and Sender ID (formely known as Project Number) obtained in step 3.
  8. Click Save

iOS

When using Apple Push Notification service (APNS) you will get two push certificates; one for development and one for production/ad-hoc. These certificates require push notifications to be sent to different servers depending on if your app is in development or production.

The production certificate is only usable if your app is in the App Store.

  1. Generate an Apple Push Certificate .p12 file for your app (instructions).
  2. After you export the .p12 file, on Kinvey's console navigate to Engagement and select Push.
  3. Click Configure Push.
  4. In the iOS section drag your .p12 file generated in step 1 where it says Drag here or click to upload a .p12 file.
  5. Click Save
  6. When you are ready to deploy your app, use your production/ad-hoc certificate. Export the .p12 file, and upload that to our service. Then select production as the certificate type and click Save. Deploying your application is a one-time action and cannot be undone.

Install Push Plugin

Install the nativescript-push-plugin for your app.

tns plugin add nativescript-push-notifications

Follow this guide to setup the nativescript-push-plugin if you are developing an Android app. Skip Step 2. Obtain the FCM Server Key

Register the device

The device needs to be registered to receive push notifications through Kinvey. There must be a logged-in user when registering. This user will be linked to the device, allowing you to send targeted push notifications.

To register the device, initialize the Push module:

import { Push } from 'kinvey-nativescript-sdk/push';

const promise = Push.register({
  android: {
    senderID: '<Sender ID>'
  },
  ios: {
    alert: true,
    badge: true,
    sound: true
  }
})
  .then((deviceToken: string) => {
    // ...
  })
  .catch((error: Error) => {
    // ...
  });
var Push = require('kinvey-nativescript-sdk/push').Push;
var promise = Push.register({
  android: {
    senderID: '<Sender ID>'
  },
  ios: {
    alert: true,
    badge: true,
    sound: true
  }
})
  .then(function(deviceToken) {
    // ...
  })
  .catch(function(error) {
    // ...
  });

Handling Push Notifications

You need to register a listener with the Push module to receive push notifications.

Push.onNotification((data: any) => {
  // ...
});
Push.onNotification(function(data) {
  // ...
});

Disabling Push Notifications

The following code will disable push notifications for the active user:

const promise = Push.unregister()
  .then(() => {
    // ...
  })
  .catch((error: Error) => {
    // ...
  });
var promise = Push.unregister()
  .then(function() {
    // ...
  })
  .catch(function(error) {
    // ...
  });
Got a question?