-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.android.js
184 lines (144 loc) · 5.95 KB
/
index.android.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/**
* Sample React Native App
* https://github.com/facebook/react-native
* @flow
*/
import React, { Component } from 'react';
import {
AppRegistry,
StyleSheet,
Text,
View,
AppState
} from 'react-native';
import { Provider } from 'react-redux'
import BackgroundTask from 'react-native-background-task';
import Router from './app/router/Router';
import store from './app/redux/store';
import services from './app/services/blog';
import FCM, {
FCMEvent,
RemoteNotificationResult,
WillPresentNotificationResult,
NotificationType
} from "react-native-fcm";
import PushController from "./app/fcm/PushController";
import LocalStorage from './app/services/localStorage';
FCM.subscribeToTopic("nisaptham-post");
FCM.getInitialNotification().then(notif => {
console.log("INITIAL NOTIFICATION APP", notif)
});
const notificationListner = FCM.on(FCMEvent.Notification, notif => {
console.log("Notification", notif);
if (notif.local_notification) {
return;
}
if (notif.opened_from_tray) {
//this.props.navigation.navigate('Chat', {friend: item} //needs to use notif. to navigate to correct chat screen
return;
}
});
BackgroundTask.define(() => {
console.log("========>>>>>>>>> Hello from a background task ");
console.log("================================== Hello from a background task at " + new Date() + "on AppState : " + AppState.currentState + " ==============================================");
// FCM.presentLocalNotification({
// id: "notif.uid" + new Date().getMinutes(),
// title: "notif.title" + new Date().getMinutes(),
// body: "notif.body",
// priority: "high",
// click_action: "fcm.ACTION.HELLO",
// show_in_foreground: true,
// local: true,
// });
fetch('https://www.blogger.com/feeds/10674130/posts/default/?alt=json&max-results=1')
.then((response) => {
if ((response.status >= 200 && response.status <= 300) || response.status === 0) {
return Promise.resolve(response)
} else {
return Promise.reject(response);
}
})
.then((response) => response.json())
.then((result) => {
result.data = result;
if (result.data) {
// console.log("BLOG GET LATEST POST RESPOSNSE :", result.data);
if (result.data.feed.entry.length > 0) {
let newPostId = result.data.feed.entry[0].id.$t;
newPostId = newPostId.toString();
console.log("BLOG GET LATEST POST ID ", newPostId);
LocalStorage.get('newPostId').then((value) => {
console.log('LocalStorage Get Success newPostId', value);
if (value != null) {
value = JSON.parse(value);
console.log("fetched POST ID ", value);
if (value != newPostId) {
LocalStorage.set('newPostId', newPostId);
let newPostObj = {};
newPostObj.title = result.data.feed.entry[0].title.$t;
newPostObj.summary = result.data.feed.entry[0].summary.$t;
FCM.presentLocalNotification({
vibrate: 500,
title: `${newPostObj.title}`,
body: `${newPostObj.summary}`,
priority: "high",
color: "#3391ed",
show_in_foreground: true,
click_action: "fcm.ACTION.HELLO",
lights: true,
icon: "ic_launcher",
large_icon: "ic_launcher",
picture: null
});
}
else {
/*
FCM.presentLocalNotification({
vibrate: 500,
title: `OLDAppState :${AppState.currentState}`,
body: `at ${new Date().getMinutes()}`,
priority: "high",
color: "#3391ed",
show_in_foreground: true,
click_action: "fcm.ACTION.HELLO",
lights: true,
icon: "ic_launcher",
large_icon: "ic_launcher",
picture: null
});
*/
}
}
else {
LocalStorage.set('newPostId', '');
}
}).catch((error) => {
console.log('LocalStorage Get Error newPostId', error);
})
}
}
else {
console.log("BLOG GET LATEST POST RESPOSNSE HAS NO Data Object==", result);
}
})
.catch((err) => {
console.log("BLOG GET LATEST POST ERROR", error);
});
BackgroundTask.finish();
})
export default class App extends Component {
componentDidMount() {
BackgroundTask.schedule();
}
render() {
return (
<View>
<PushController onChangeToken={(token) => { }} />
<Provider store={store}>
<Router />
</Provider>
</View>
)
}
};
AppRegistry.registerComponent('nisapthamblog', () => App);