Closed florianchevallier closed 4 years ago
Alright ! seems that adding
uaSkipApplyGoogleServicesPlugin=true
into the gradle.properties
file does the trick. You still need to do the refactor to AndroidX.
I get the notification in background while the app is open, but not while it's closed. But it may be something else not related.
To anyone coming here, here is my ReactJS code to make it work. You'll see that you still need register()
to make it work, and to use the Push Notification from @capacitor/core
.
I've also followed all the integration of push notification tutorial for iOS apps in capacitor with firebase to make it work for the iPhones.
import { IonContent, IonHeader, IonPage, IonTitle, IonToolbar, useIonViewDidEnter } from '@ionic/react';
import React from 'react';
import ExploreContainer from '../components/ExploreContainer';
import { UrbanAirShip } from '@ionic-native/urbanairship';
import './Home.css';
import {
Plugins,
PushNotification,
PushNotificationToken,
PushNotificationActionPerformed } from '@capacitor/core';
const { PushNotifications } = Plugins;
PushNotifications.register();
async function registerNotification() {
try {
await UrbanAirShip.takeOff({
development: {
appKey: "xxx",
appSecret: "xxxx"
},
production: {
appKey: "", // needed, even if empty
appSecret: "" // needed, even if empty
}
})
await UrbanAirShip.setUserNotificationsEnabled(true)
const isEnabled = await UrbanAirShip.isUserNotificationsEnabled();
const isAppEnabled = await UrbanAirShip.isAppNotificationsEnabled();
const channelId = await UrbanAirShip.getChannelID();
console.log('isEnabled', isEnabled, isAppEnabled);
console.log('channelId', channelId);
PushNotifications.addListener('registration',
(token: PushNotificationToken) => {
console.log('Push registration success, token: ' + token.value);
}
);
// Some issue with our setup and push will not work
PushNotifications.addListener('registrationError',
(error: any) => {
console.log('Error on registration: ' + JSON.stringify(error));
}
);
// Show us the notification payload if the app is open on our device
PushNotifications.addListener('pushNotificationReceived',
(notification: PushNotification) => {
console.log('Push received: ' + JSON.stringify(notification));
}
);
// Method called when tapping on a notification
PushNotifications.addListener('pushNotificationActionPerformed',
(notification: PushNotificationActionPerformed) => {
console.log('Push action performed: ' + JSON.stringify(notification));
}
);
} catch (err) {
console.log(err);
}
}
const Home: React.FC = () => {
useIonViewDidEnter(() => {
registerNotification();
})
return (
<IonPage>
<IonHeader>
<IonToolbar>
<IonTitle>Blank</IonTitle>
</IonToolbar>
</IonHeader>
<IonContent>
<IonHeader collapse="condense">
<IonToolbar>
<IonTitle size="large">Blank</IonTitle>
</IonToolbar>
</IonHeader>
<ExploreContainer />
</IonContent>
</IonPage>
);
};
export default Home;
I use hook "capacitor:copy:after":
package.json
:
{
"scripts": {
"capacitor:copy:after": "node ./hooks/capacitor-copy-after.js $CAPACITOR_PLATFORM_NAME"
}
}
hooks/capacitor-copy-after.js
:
const fs = require('fs');
if (process.argv[2] === 'android') {
const androidManifest = './android/capacitor-cordova-android-plugins/src/main/AndroidManifest.xml';
const googleServicesBase = './google-services.json';
const googleServicesCap = './android/capacitor-cordova-android-plugins/google-services.json';
fs.readFile(androidManifest, 'utf8', (err, data) => {
if (err) {
console.error(err);
return;
}
data = data.replace('capacitor.android.plugins', 'my.project.android'); // CHANGE APPLICATION ID
fs.writeFile(androidManifest, data, (err) => {
if (err) {
console.error(err);
}
});
});
fs.copyFile(googleServicesBase, googleServicesCap, (err) => {
if (err) {
console.error(err);
}
});
}
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.
TL:DR : the step to reproduce the issue :
And the error :
What I've tried so far
place a
google-services.json
incapacitor-cordova-android-plugins
If I place a
google-services.json
here :I get a different error :
Which lead me here #1286 ending in
Rename the package name of
capacitor-cordova-android-plugins/src/main/AndroidManifest.xml
To match the id of my current firebase config. Which lead me here :
Which seems to be a bug with AndroidX. Tried this : https://github.com/ionic-team/capacitor/pull/2045#issuecomment-546707620 (with this repo on master) + Refactor > Migrate to AndroidX
And now... it works on Android. The thing is, I shouldn't have to edit the package name of
capacitor-cordova-android-plugins/src/main/AndroidManifest.xml
since it's always generated.Do you have any idea how it could be easier to implement ?
Cheers