Add push notification logic
This commit is contained in:
parent
f12f1ea614
commit
f9ecd331a3
|
@ -0,0 +1,58 @@
|
||||||
|
import Meteor from 'react-native-meteor';
|
||||||
|
import PushNotification from 'react-native-push-notification';
|
||||||
|
import { AsyncStorage } from 'react-native';
|
||||||
|
import Random from 'react-native-meteor/lib/Random';
|
||||||
|
import RocketChat from '../lib/rocketchat';
|
||||||
|
|
||||||
|
try {
|
||||||
|
AsyncStorage.getItem('pushId')
|
||||||
|
.then((pushId) => {
|
||||||
|
if (!pushId) {
|
||||||
|
AsyncStorage.setItem('pushId', Random.id());
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
// Error saving data
|
||||||
|
}
|
||||||
|
|
||||||
|
PushNotification.configure({
|
||||||
|
|
||||||
|
// (optional) Called when Token is generated (iOS and Android)
|
||||||
|
async onRegister({ token }) {
|
||||||
|
|
||||||
|
|
||||||
|
console.log( 'TOKEN:', token );
|
||||||
|
Meteor.Accounts.onLogin(async() => {
|
||||||
|
console.log('onLogin');
|
||||||
|
RocketChat.registerPushToken(await AsyncStorage.getItem('pushId'), { gcm: token })
|
||||||
|
.then(sucesso => console.log('FOI -> ',sucesso))
|
||||||
|
.catch(error => console.log('ERRO ->', error));
|
||||||
|
});
|
||||||
|
},
|
||||||
|
|
||||||
|
// (required) Called when a remote or local notification is opened or received
|
||||||
|
onNotification(notification) {
|
||||||
|
console.log( 'NOTIFICATION:', notification );
|
||||||
|
},
|
||||||
|
|
||||||
|
// ANDROID ONLY: GCM Sender ID (optional - not required for local notifications, but is need to receive remote push notifications)
|
||||||
|
senderID: '673693445664',
|
||||||
|
|
||||||
|
// IOS ONLY (optional): default: all - Permissions to register.
|
||||||
|
permissions: {
|
||||||
|
alert: true,
|
||||||
|
badge: true,
|
||||||
|
sound: true
|
||||||
|
},
|
||||||
|
|
||||||
|
// Should the initial notification be popped automatically
|
||||||
|
// default: true
|
||||||
|
popInitialNotification: false,
|
||||||
|
|
||||||
|
/**
|
||||||
|
* (optional) default: true
|
||||||
|
* - Specified if permissions (ios) and token (android and ios) will requested or not,
|
||||||
|
* - if not, you must call PushNotificationsHandler.requestPermissions() later
|
||||||
|
*/
|
||||||
|
requestPermissions: true
|
||||||
|
});
|
|
@ -366,6 +366,23 @@ const RocketChat = {
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
registerPushToken(pushId, token) {
|
||||||
|
console.log('registerPushToken ->', pushId, token);
|
||||||
|
|
||||||
|
const data = {
|
||||||
|
id: pushId,
|
||||||
|
token,
|
||||||
|
appName: 'main',
|
||||||
|
userId: null,
|
||||||
|
metadata: {}
|
||||||
|
};
|
||||||
|
return call('raix:push-update', data);
|
||||||
|
},
|
||||||
|
|
||||||
|
updatePushToken(pushId) {
|
||||||
|
return call('raix:push-setuser', pushId);
|
||||||
|
},
|
||||||
|
|
||||||
logout() {
|
logout() {
|
||||||
return AsyncStorage.clear();
|
return AsyncStorage.clear();
|
||||||
}
|
}
|
||||||
|
@ -378,6 +395,13 @@ if (RocketChat.currentServer) {
|
||||||
}
|
}
|
||||||
|
|
||||||
Meteor.Accounts.onLogin(() => {
|
Meteor.Accounts.onLogin(() => {
|
||||||
|
AsyncStorage.getItem('pushId')
|
||||||
|
.then((pushId) => {
|
||||||
|
console.log('update -> ', pushId);
|
||||||
|
RocketChat.updatePushToken(pushId)
|
||||||
|
.catch(error => console.log('lero ->', error));
|
||||||
|
});
|
||||||
|
|
||||||
Promise.all([call('subscriptions/get'), call('rooms/get')]).then(([subscriptions, rooms]) => {
|
Promise.all([call('subscriptions/get'), call('rooms/get')]).then(([subscriptions, rooms]) => {
|
||||||
subscriptions = subscriptions.sort((s1, s2) => (s1.rid > s2.rid ? 1 : -1));
|
subscriptions = subscriptions.sort((s1, s2) => (s1.rid > s2.rid ? 1 : -1));
|
||||||
rooms = rooms.sort((s1, s2) => (s1._id > s2._id ? 1 : -1));
|
rooms = rooms.sort((s1, s2) => (s1._id > s2._id ? 1 : -1));
|
||||||
|
|
|
@ -10,6 +10,8 @@ import PhotoView from './views/Photo';
|
||||||
import CreateChannel from './views/CreateChannel';
|
import CreateChannel from './views/CreateChannel';
|
||||||
import store from './lib/createStore';
|
import store from './lib/createStore';
|
||||||
|
|
||||||
|
import './lib/pushNotification';
|
||||||
|
|
||||||
Navigation.registerComponent('Rooms', () => RoomsListView, store, Provider);
|
Navigation.registerComponent('Rooms', () => RoomsListView, store, Provider);
|
||||||
Navigation.registerComponent('Room', () => RoomView, store, Provider);
|
Navigation.registerComponent('Room', () => RoomView, store, Provider);
|
||||||
Navigation.registerComponent('Photo', () => PhotoView, store, Provider);
|
Navigation.registerComponent('Photo', () => PhotoView, store, Provider);
|
||||||
|
|
Loading…
Reference in New Issue