@@ -8,6 +8,7 @@ import config from '../config';
88import { registerPush , unregisterPush } from '../api' ;
99import { logErrorRemotely } from '../utils/logging' ;
1010import { doNarrow } from '../message/messagesActions' ;
11+ import { saveTokenPush , deleteTokenPush } from './notificationActions' ;
1112
1213const getGroupNarrowFromNotificationData = ( data : NotificationGroup , usersById : UserIdMap = { } ) => {
1314 const userIds = data . pm_users . split ( ',' ) ;
@@ -98,58 +99,50 @@ export class NotificationListener {
9899 }
99100}
100101
101- const getTokenIOS = ( auth : Auth , saveTokenPush : ( pushToken : string ) => void ) => {
102+ const getTokenIOS = ( auth : Auth , dispatch : Dispatch ) => {
102103 NotificationsIOS . addEventListener ( 'remoteNotificationsRegistered' , async deviceToken => {
103104 await registerPush ( auth , deviceToken ) ;
104- saveTokenPush ( deviceToken ) ;
105+ dispatch ( saveTokenPush ( deviceToken ) ) ;
105106 } ) ;
106107 NotificationsIOS . addEventListener ( 'remoteNotificationsRegistrationFailed' , ( error : string ) => {
107108 logErrorRemotely ( new Error ( error ) , 'Failed to register iOS push token' ) ;
108109 } ) ;
109110 NotificationsIOS . requestPermissions ( ) ;
110111} ;
111112
112- const getTokenAndroid = (
113- auth : Auth ,
114- oldToken : string | null ,
115- saveTokenPush : ( pushToken : string ) => void ,
116- ) => {
113+ const getTokenAndroid = ( auth : Auth , oldToken : string | null , dispatch : Dispatch ) => {
117114 if ( auth . apiKey !== '' && oldToken === null ) {
118115 NotificationsAndroid . refreshToken ( ) ;
119116 }
120117 NotificationsAndroid . setRegistrationTokenUpdateListener ( async deviceToken => {
121118 try {
122119 await registerPush ( auth , deviceToken ) ;
123- saveTokenPush ( deviceToken ) ;
120+ dispatch ( saveTokenPush ( deviceToken ) ) ;
124121 } catch ( e ) {
125122 logErrorRemotely ( e , 'Failed to register GCM' ) ;
126123 }
127124 } ) ;
128125} ;
129126
130- export const getNotificationToken = (
131- auth : Auth ,
132- oldToken : string | null ,
133- saveTokenPush : ( pushToken : string ) => void ,
134- ) => {
127+ export const getNotificationToken = ( auth : Auth , oldToken : string | null , dispatch : Dispatch ) => {
135128 if ( Platform . OS === 'ios' ) {
136- getTokenIOS ( auth , saveTokenPush ) ;
129+ getTokenIOS ( auth , dispatch ) ;
137130 } else {
138- getTokenAndroid ( auth , oldToken , saveTokenPush ) ;
131+ getTokenAndroid ( auth , oldToken , dispatch ) ;
139132 }
140133} ;
141134
142135export const tryStopNotifications = async (
143136 auth : Auth ,
144137 token : string | null ,
145- callback : ( ) = > void ,
138+ dispatch : Dispatch ,
146139) => {
147140 if ( token !== null ) {
148141 try {
149142 await unregisterPush ( auth , token ) ;
150143 } catch ( e ) {
151144 logErrorRemotely ( e , 'failed to unregister Push token' ) ;
152145 }
153- callback ( ) ;
146+ dispatch ( deleteTokenPush ( ) ) ;
154147 }
155148} ;
0 commit comments