evollu / evollu/react-native-fcm
Unregistered token - Android
- Dominant language
- Java
- Stars
- 1.7k
- Forks
- 661
- PR merge metrics
- No merged PRs in 30d
Description
I have simple problem with token on Android. Firebase tells me that token is **unregistered**. On iOS everything works fine.
here is my code:
```
componentDidMount() {
console.log('FCMEVENT', FCMEvent);
if(this.props.isAuthenticated && !this.state.notificationRegistered) {
this.registerNotification();
}
}
componentWillReceiveProps(newProps) {
if(newProps.isAuthenticated && !this.state.notificationRegistered) {
this.registerNotification();
} else {
this.removeNotification();
}
}
getToken = () => AsyncStorage.getItem(TOKEN)
.then((data) => {
return data;
})
.catch(() => {});
registerNotification = async () => {
const token = await this.getToken();
this.handleRegister();
console.log(token);
this.setState({
notificationRegistered: true,
});
};
removeNotification = () => {
FCM.on(FCMEvent.Notification, notif => {
AsyncStorage.setItem('lastNotification', JSON.stringify(notif));
if (notif.opened_from_tray) {
setTimeout(() => {
if(notif._actionIdentifier === 'reply') {
if (AppState.currentState !== 'background') {
console.log(`User replied ${JSON.stringify(notif._userText)}`);
alert(`User replied ${JSON.stringify(notif._userText)}`);
} else {
AsyncStorage.setItem('lastMessage', JSON.stringify(notif._userText));
}
}
if(notif._actionIdentifier === 'view') {
alert('User clicked View in App');
}
if(notif._actionIdentifier === 'dismiss') {
alert('User clicked Dismiss');
}
}, 1000);
}
});
this.setState({ notificationRegistered: false });
};
handleRegister = async () => {
await FCM.requestPermissions({ badge: false, sound: true, alert: true });
FCM.getFCMToken().then((token) => {
this.props.setDeviceForNotification({
deviceId: Constants.deviceId,
pushToken: token,
deviceType: Platform.OS,
});
});
AsyncStorage.getItem('lastNotification').then((data) => {
if(data) {
// if notification arrives when app is killed, it should still be logged here
console.log('last notification', JSON.parse(data));
AsyncStorage.removeItem('lastNotification');
}
});
AsyncStorage.getItem('lastMessage').then((data) => {
if(data) {
// if notification arrives when app is killed, it should still be logged here
console.log('last message', JSON.parse(data));
AsyncStorage.removeItem('lastMessage');
}
});
const showLocalNotification = () => {
FCM.presentLocalNotification({
id: new Date().valueOf().toString(), // (optional for instant notification)
title: "Test Notification with action", // as FCM payload
body: "Force touch to reply", // as FCM payload (required)
show_in_foreground: true // notification when app is in foreground (local & remote)
});
};
FCM.on(FCMEvent.Notification, notif => {
console.log('NotificationMESSAGE', notif);
if (Platform.OS === 'ios' && notif._notificationType === NotificationType.WillPresent && !notif.local_notification) {
// this notification is only to decide if you want to show the notification when user if in forground.
// usually you can ignore it. just decide to show or not.
notif.finish(WillPresentNotificationResult.All);
return;
}
if (notif.opened_from_tray) {
setTimeout(() => {
alert(`User tapped notification\n${JSON.stringify(notif)}`);
}, 500);
}
if (Platform.OS === 'ios') {
/*
optional
iOS requires developers to call completionHandler to end notification process.
If you do not call it your background remote notifications could be throttled, to read more about it see the above documentation link.
This library handles it for you automatically with default behavior
(for remote notification, finish with NoData; for WillPresent, finish depend on "show_in_foreground").
However if you want to return different result, follow the following code to override notif._notificationType is available for iOS platfrom
*/
switch (notif._notificationType) {
case NotificationType.Remote:
notif.finish(RemoteNotificationResult.NewData); // other types available: RemoteNotificationResult.NewData, RemoteNotificationResult.ResultFailed
break;
case NotificationType.NotificationResponse:
notif.finish();
break;
case NotificationType.WillPresent:
notif.finish(WillPresentNotificationResult.All); // other types available: WillPresentNotificationResult.None
// this type of notificaiton will be called only when you are in foreground.
// if it is a remote notification, don't do any app logic here. Another notification callback will be triggered with type NotificationType.Remote
break;
default:
break;
}
}
});
FCM.on(FCMEvent.RefreshToken, token => {
console.log('TOKEN (refreshUnsubscribe)', token);
});
FCM.on(FCMEvent.DirectChannelConnectionChanged, (data) => {
console.log(`direct channel connected ${data}`);
});
FCM.enableDirectChannel();
setTimeout(() => {
FCM.isDirectChannelEstablished().then((d) => {
console.log('established', d);
});
}, 1000);
};
```
Everything was made according to the repo example.
Anyone knows what I am doing wrong?
Thanks!
React Native 0.53
React Native FCM 14.1.3
google services 10.0.1
Contributor guide
No contributing guide indexed for this repository
Research direction
No repository files or tests are named. Start by reproducing the Android token path around FCM.getFCMToken and the FCMEvent.RefreshToken handler, using the supplied React Native, React Native FCM, and Google services versions. Done means identifying why Firebase reports the Android token as unregistered and documenting or correcting the reproducible cause.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- android, firebase, javascript, react-native
- Domain
- mobile
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 18/100