Skip to content

Instantly share code, notes, and snippets.

@yonahforst
Last active July 26, 2016 14:12
Show Gist options
  • Save yonahforst/5d01c90c9b7769ae5bdf3ded3444139e to your computer and use it in GitHub Desktop.
Save yonahforst/5d01c90c9b7769ae5bdf3ded3444139e to your computer and use it in GitHub Desktop.
OneSignal helpers for RN
///OneSignal.js
'use strict';
const appName = 'Foobar'
class OneSignal {
constructor() {
this._useSandbox = typeof __DEV__ == 'undefined' ? false : __DEV__
this._applicationId = ******
this._apiKey = *****
this._apiBaseURL = 'https://onesignal.com/api/v1'
}
register(userId, token, os) {
this._token = token;
this._userId = userId;
return this._fetch({
method: 'POST',
url: '/players',
body: {
app_id: this._applicationId,
tags: {userId: this._userId},
identifier: this._token,
device_type: os == 'android' ? '1' : '0',
test_type: this._useSandbox ? '1' : '0',
}
}).then((response) => {
if (response.status === 200 || response.status === 201) {
return response;
} else {
throw(response);
}
})
.catch((error) => {
console.log(error);
throw(error);
});
}
send(userIds, message, data) {
let tags = []
if (!Array.isArray(userIds)) {
userIds = [userIds]
}
userIds.forEach(id => {
tags.push({"key": "userId", "relation": "=", "value": id})
tags.push({"operator": "OR"})
})
tags.pop() //remove the last OR operator
let body = {
app_id: this._applicationId,
isIos: true,
isAndroid: true,
tags: tags,
content_available: true,
android_background_data: true,
}
if (message) {
body.headings = {en: appName}
body.contents = {en: message}
}
if (data) {
body.data = data
}
return this._fetch({
method: 'POST',
url: '/notifications',
body: body
})
.then((response) => {
if (response.status === 200 || response.status === 201) {
return response;
} else {
throw(response);
}
})
.catch((error) => {
console.log(error);
throw(error);
});
}
_fetch(opts) {
opts = {
method: 'GET',
url: null,
body: null,
callback: null,
...opts
}
var reqOpts = {
method: opts.method,
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
'Authorization': `Basic ${this._apiKey}`,
}
};
if (opts.body) {
reqOpts.body = JSON.stringify(opts.body);
}
return fetch(this._apiBaseURL + opts.url, reqOpts);
}
}
module.exports = new OneSignal();
///PushNotifications.js
'use strict';
import RNPushNotification from 'react-native-push-notification';
import OneSignal from './OneSignal';
class PushNotifications {
initialize(userId, callback) {
if (!console.ignoredYellowBox)
console.ignoredYellowBox = [];
console.ignoredYellowBox.push('PushNotificationIOS.popInitialNotification() is deprecated. Use getInitialNotification() instead.');
return RNPushNotification.configure({
onRegister: response => {
OneSignal.register(userId, response.token, response.os);
this._isRegistered = true;
},
onNotification: function(notification) {
callback(notification);
},
// IOS ONLY: (optional) default: true
requestPermissions: true,
// ANDROID ONLY: (optional) GCM Sender ID.
senderID: *******,
});
}
checkPermissions(callback) {
return RNPushNotification.checkPermissions(callback);
}
requestPermissions() {
return RNPushNotification.requestPermissions()
}
sendSilentNotification(userId, data) {
return OneSignal.send(userId, data).catch(e => console.log(e))
}
send(userId, message, data) {
return OneSignal.send(userId, message, data).catch(e => console.log(e))
}
}
module.exports = new PushNotifications();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment