Closed wobsoriano closed 4 years ago
Also, I don't want to update to latest version of capacitor because of old cordova plugin issues. Thank you.
After hours hours hours,
I just updated to latest version of capacitor and it still doesn't work. ( PushNotifications.requestPermission
)
If you directly use PushNotifications.register()
, it shows prompt but not gets token and not fires up registration
eventListeners ...
Update:
I resync the cap with ios 'npx cap sync ios' and now PushNotifications.requestPermission
prompts request alert but registration event listener still can't receive the token .
After hours hours hours, I just updated to latest version of capacitor and it still doesn't work. (
PushNotifications.requestPermission
) If you directly usePushNotifications.register()
, it shows prompt but not gets token and not fires upregistration
eventListeners ...Update:
I resync the cap with ios 'npx cap sync ios' and now
PushNotifications.requestPermission
prompts request alert but registration event listener still can't receive the token .
Are you testing it on a real device or simulator?
After hours hours hours, I just updated to latest version of capacitor and it still doesn't work. (
PushNotifications.requestPermission
) If you directly usePushNotifications.register()
, it shows prompt but not gets token and not fires upregistration
eventListeners ... Update: I resync the cap with ios 'npx cap sync ios' and nowPushNotifications.requestPermission
prompts request alert but registration event listener still can't receive the token .Are you testing it on a real device or simulator?
I am testing on a real device.
On the otherhand, same codebase works for adroid.
I rechecked the IOS setup again and again and followed several tutorials including the official one, and Iām pretty sure that all certificates, setup codes are in its place.
What would be the reason of not getting token after granting permissions?
@yigitbacakoglu After register your device with Push notification.
In ios, we only receive the APNS token, not FCM token. So, after registering your device, try to use this plugin to get FCM token (https://github.com/stewwan/capacitor-fcm)
fcm.getToken().then(tObj => { if (tObj && tObj.token) { const fcmToken = tObj.token; } });
After hours hours hours, I just updated to latest version of capacitor and it still doesn't work. (
PushNotifications.requestPermission
) If you directly usePushNotifications.register()
, it shows prompt but not gets token and not fires upregistration
eventListeners ... Update: I resync the cap with ios 'npx cap sync ios' and nowPushNotifications.requestPermission
prompts request alert but registration event listener still can't receive the token .Are you testing it on a real device or simulator?
I am testing on a real device.
On the otherhand, same codebase works for adroid.
I rechecked the IOS setup again and again and followed several tutorials including the official one, and Iām pretty sure that all certificates, setup codes are in its place.
What would be the reason of not getting token after granting permissions?
Me too. The setup was working on Android and not on IOS. No errors at all.
I'm using ionic and react and here's my code:
useEffect(() => {
if (!state.isAuth) {
return;
}
const registerToken = async () => {
const devicesRef = DB.collection('devices');
try {
await PushNotifications.register();
const r = await FCMPlugin.getToken();
await devicesRef.doc(r.token).set({
token: r.token,
userId: state.user.id,
});
} catch (e) {
console.log(e);
toast('Push notifications register token error', 'danger');
}
};
const subscribeToAnnouncements = async () => {
try {
await PushNotifications.register();
await FCMPlugin.subscribeTo({ topic: 'announcements' });
} catch (e) {
toast('Push notifications subscribe error', 'danger');
}
};
registerToken();
subscribeToAnnouncements();
}, [state.isAuth, state.user.id]);
useEffect(() => {
PushNotifications.addListener(
'pushNotificationReceived',
(notification) => {
if (!state.isAuth) {
return;
}
LocalNotifications.schedule({
notifications: [
{
id: Date.now(),
title: notification.title,
body: notification.body,
},
],
});
}
);
}, [state.isAuth]);
This works on Android and not on iOS, though the code was able to get the token on iOS but does not listen on pushNotificationReceived
I guess since I am not receiving any notification on our device.
ugin to get FCM toke
Do we need the FCM token on both Android and iOS or only Android? And use APN on iOS? I am using https://github.com/stewwan/capacitor-fcm and have the same setup but I got it working only on android. See https://github.com/ionic-team/capacitor/issues/2795#issuecomment-616336303
removeAllListeners is only available in Capacitor 2.x
@jcesarmobile how about requestPermissions
? Only in Capacitor 2.x too?
requestPermissions
is Android only, it was in 1.x already.
But for requesting notification permission it's requestPermission
, available on both LocalNotifications
and PushNotifications
@jcesarmobile thank you. So requestPermission
is required for iOS to start Push Notifs?
yes
yes
This is in Capacitor 2. So in Capacitor 1 maybe that's the reason that's why we are not receiving notifications using FCM in iOS but works in Android. Thank you.
In capacitor 1 calling register
also requested the permission, but doesn't do it in Capacitor 2.
But please, don't keep asking questions on github issues.
For questions you can ask on the forum, slack or stack overflow using capacitor tag.
Thanks for the issue! This issue is being locked to prevent comments that are not relevant to the original issue. If this is still an issue with the latest version of Capacitor, please create a new issue and ensure the template is fully filled out.
Bug Report
Capacitor Version
npx cap doctor
output:Affected Platform(s)
Current Behavior
PushNotifications.removeAllListeners
is not a function.PushNotifications.requestPermissions
returns an empty object.Expected Behavior
Removes all listeners.
Returns an object with
granted
property.Sample Code or Sample Application Repo
PushNotifications.removeAllListeners();
Other Technical Details
When I do
console.log(Object.keys(PushNotifications))
, I get only:But in the docs, there is a
removeAllListeners
method?npm --version
output:6.13.1
node --version
output:v13.2.0
pod --version
output (iOS issues only):1.8.4