You are viewing Skygear v1 Documentation.Switch to Skygear v0 Documentation

Skygear Push Basics

You need to register the devices before sending them push notifications.

Registering device

It is suggested that you register a device with remote server on every launch. Since the container remembers the device ID when the device is registered on the remote server, it will reuse an existing device ID whenever you try to register the current device.

You should also request a remote notification token at some point in the program. If it is appropriate to ask the user for permission for remote notification, you can also do so when the application launches.

The method to register the device varies depends on the framework you use with the JavaScript SDK.

When the device is registered on the remote server, a device ID will be available to the client application. If you register the current device using the above convenient methods, the device ID is returned from deviceID property on the container.

React-Native on iOS

You should add PushNotificationIOS to your project which helps you by requesting permission to obtain push notification token.

When the device obtained the push notification token, the register event is fired. Add an event listener for this event and use the registerDevice function to register the token with Skygear.

var { PushNotificationIOS } = require('react-native');
var skygear = require('skygear');

PushNotificationIOS.addEventListener('register', function(token) {
  skygear.push.registerDevice(token, 'ios', 'CHANGE-ME-bundleID');
});

React-Native on Android

You should follow Set up a GCM Client App on Android and use GCM for React Native Android to obtain the push notification token.

When the device obtained the push notification token, the register event is fired. Add an event listener for this event and use the registerDevice function to register the token with Skygear.

var GcmAndroid = require('react-native-gcm-android');
var skygear = require('skygear');

GcmAndroid.addEventListener('register', function(token) {
  skygear.push.registerDevice(token, 'android', 'CHANGE-ME-packagename');
});

Sending push notification from cloud code

To send push notifications through cloud code, please refer to the Cloud Code Guide: Push Notifications.

Sending push notification to users

var skygear = require('skygear');

skygear.push.sendToUser(
  ['2aa4af2a-699a-4e43-8d67-7598757fc7ed'], // User IDs
  {
    'apns': {
        'aps': {
            'alert': {
                'title': title,
                'body': message,
            }
        },
        'from': 'skygear',
        'operation': 'notification',
    },
    'fcm': {
         'notification': {
              'title': title,
              'body': message,
          }
    },
  }
);

Sending push notification to devices

var skygear = require('skygear');

skygear.push.sendToDevice(
  ['2aa4af2a-699a-4e43-8d67-7598757fc7ed'], // Device IDs
  {
    'apns': {
        'aps': {
            'alert': {
                'title': title,
                'body': message,
            }
        },
        'from': 'skygear',
        'operation': 'notification',
    },
    'fcm': {
         'notification': {
              'title': title,
              'body': message,
          }
    },
  }
);

Unregistering device

When a device is registered, it is associated with the authenticated user. Therefore it is necessary to unregister the device when the user is no longer associated with the device, for example when the user logs out.

var skygear = require('skygear');

skygear.unregisterDevice();