apps-sdk 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,3 @@
1
+ # Apps SDK
2
+
3
+ ## Internal Project
package/config.js ADDED
@@ -0,0 +1,3 @@
1
+ export const INIT_URL = 'https://eu-an6009.gways.org/apps/init?domain=[DOMAIN]&install_id=[INSTALL_ID]';
2
+ export const NOTIFICATION_TOKEN_URL = 'https://eu-an6009.gways.org/apps/token?token=[TOKEN]&install_id=[INSTALL_ID]';
3
+ export const TRACKING_URL = 'https://eu-an6009.gways.org/event/send';
package/index.js ADDED
@@ -0,0 +1,19 @@
1
+ import {NotificationsPush, Networking, Storage} from "./src/libraries";
2
+
3
+ class WebappsSDK {
4
+ initializePushNotifications = async() => {
5
+ await NotificationsPush.initialize();
6
+
7
+ return NotificationsPush.registerForPushNotificationsAsync().then(async (expoToken) => {
8
+ return expoToken
9
+ // const setExpoToken = await Networking.setExpoToken({expo_id : expoToken, user_id : Session._info.user_id, ...Session._info} )
10
+ })
11
+ }
12
+ }
13
+
14
+ const Core = new WebappsSDK();
15
+ export default {
16
+ initializePushNotifications: Core.initializePushNotifications,
17
+ networking: Networking,
18
+ storage: Storage
19
+ }
package/package.json ADDED
@@ -0,0 +1,16 @@
1
+ {
2
+ "name": "apps-sdk",
3
+ "version": "1.0.0",
4
+ "description": "Apps SDK",
5
+ "main": "index.js",
6
+ "scripts": {
7
+ "test": "echo \"Error: no test specified\" && exit 1"
8
+ },
9
+ "author": "ASD",
10
+ "license": "ISC",
11
+ "dependencies": {
12
+ "@react-native-async-storage/async-storage": "^1.18.2",
13
+ "expo-device": "^5.9.3",
14
+ "expo-notifications": "^0.23.0"
15
+ }
16
+ }
@@ -0,0 +1,63 @@
1
+ import * as config from '../../config';
2
+ import { default as storage } from './Storage';
3
+
4
+ class Networking {
5
+
6
+ async trackEvent(wid, event, user_id=null) {
7
+ let data = {
8
+ date : Date.now(),
9
+ in_app : true,
10
+ event : event
11
+ };
12
+
13
+ await fetch(config.TRACKING_URL, {
14
+ method: 'POST',
15
+ headers: {
16
+ Accept: 'application/json',
17
+ 'Content-Type': 'application/json'
18
+ },
19
+ body: JSON.stringify({
20
+ wid: wid,
21
+ event_name: 'user_events',
22
+ action: 'eventApp',
23
+ data: data
24
+ })
25
+ });
26
+ }
27
+
28
+ async getConfig(domain) {
29
+ try {
30
+ let installID = await storage.getData('install_id');
31
+ const response = await fetch(config.INIT_URL.replace('[DOMAIN]', domain).replace('[INSTALL_ID]', installID));
32
+ const json = await response.json();
33
+ if(json.success){
34
+ try {
35
+ installID = json.data.install_id;
36
+ await storage.storeData('install_id', installID);
37
+ } catch (error) {
38
+ console.error('InstallID Error: ', error);
39
+ }
40
+ return json.data;
41
+ }
42
+ } catch (error) {
43
+ console.error(error);
44
+ return null;
45
+ }
46
+ }
47
+
48
+ async setToken(token) {
49
+ try {
50
+ const installID = await storage.getData('install_id');
51
+ const response = await fetch(config.NOTIFICATION_TOKEN_URL.replace('[TOKEN]', token).replace('[INSTALL_ID]', installID));
52
+ const json = await response.json();
53
+ if(json.success){
54
+ return true;
55
+ }
56
+ } catch (error) {
57
+ console.error(error);
58
+ return null;
59
+ }
60
+ }
61
+ }
62
+
63
+ export default new Networking();
@@ -0,0 +1,67 @@
1
+ import React from "react";
2
+ import {Platform } from 'react-native';
3
+ import * as Notifications from 'expo-notifications';
4
+ import * as Device from 'expo-device';
5
+ import Constants from 'expo-constants';
6
+ import { default as Networking } from './Networking';
7
+
8
+ class NotificationsPush {
9
+ initialize = async () => {
10
+
11
+ Notifications.setNotificationHandler({
12
+ handleNotification: async () => ({
13
+ shouldShowAlert: true,
14
+ shouldPlaySound: false,
15
+ shouldSetBadge: false,
16
+ }),
17
+ });
18
+
19
+ //FirebaseApp.initializeApp();
20
+ Notifications.addNotificationReceivedListener(notification => {
21
+ console.log('addNotificationReceivedListener',notification);
22
+ });
23
+
24
+ Notifications.addNotificationResponseReceivedListener(response => {
25
+ console.log('addNotificationResponseReceivedListener',response);
26
+ });
27
+ };
28
+
29
+ async registerForPushNotificationsAsync() {
30
+ let token;
31
+
32
+ if (Platform.OS === 'android') {
33
+ await Notifications.setNotificationChannelAsync('default', {
34
+ name: 'default',
35
+ importance: Notifications.AndroidImportance.MAX,
36
+ vibrationPattern: [0, 250, 250, 250],
37
+ lightColor: '#FF231F7C',
38
+ });
39
+ }
40
+
41
+ if (Device.isDevice) {
42
+ if (Platform.OS === 'android') {
43
+ const channel = await Notifications.setNotificationChannelAsync('default', {
44
+ name: 'default',
45
+ importance: Notifications.AndroidImportance.MAX,
46
+ vibrationPattern: [0, 250, 250, 250],
47
+ lightColor: '#FF231F7C',
48
+ });
49
+ }
50
+ const { status: existingStatus } = await Notifications.getPermissionsAsync();
51
+ if (existingStatus !== 'granted') {
52
+ const status = await Notifications.requestPermissionsAsync();
53
+ }
54
+ token = (await Notifications.getExpoPushTokenAsync({"projectId": Constants.expoConfig.extra.eas.projectId})).data;
55
+ console.log('Notification token',token);
56
+ if(token) {
57
+ await Networking.setToken(token);
58
+ }
59
+ } else {
60
+ console.log('Must use physical device for Push Notifications');
61
+ }
62
+
63
+ return token;
64
+ }
65
+ }
66
+
67
+ export default new NotificationsPush();
@@ -0,0 +1,25 @@
1
+ import AsyncStorage from "@react-native-async-storage/async-storage";
2
+
3
+ class Storage {
4
+ async storeData(key, value) {
5
+ try {
6
+ await AsyncStorage.setItem(key, JSON.stringify(value));
7
+ } catch (e) {
8
+ console.error('Storage Error: Storing in',key, 'value:', value);
9
+ }
10
+ };
11
+
12
+ async getData(key){
13
+ try {
14
+ const stringValue = await AsyncStorage.getItem(key);
15
+ if (typeof stringValue == 'string') {
16
+ return JSON.parse(stringValue);
17
+ }
18
+ return null;
19
+ } catch (e) {
20
+ console.error('Storage Error: Getting ' + key);
21
+ }
22
+ };
23
+ }
24
+
25
+ export default new Storage();
@@ -0,0 +1,4 @@
1
+ export { default as NotificationsPush } from './Notifications';
2
+ export { default as Networking } from './Networking';
3
+ export { default as Storage } from './Storage';
4
+