Instructions for adding the OneSignal SDK to your Ionic or Capacitor app for iOS, Android
Requirements
- OneSignal Account
- Your OneSignal App Id, available in Settings > Keys & IDs.
- Ionic 1 or newer.
- Cordova 9.1.0 or newer.
- Capacitor 2.0.0 or newer.
iOS Requirements
- An iOS 9+ or iPadOS device (iPhone, iPad, iPod Touch) to test on. Xcode 14+ simulator works running iOS 16+.
- A Mac with Xcode 12+.
- An iOS Push Certificate.
- Cordova ios@5.0.0 or newer.
- CocoaPods - Install with the following from the Terminal:
Shell
sudo gem install cocoapods
pod repo update
Ionic/Capacitor
npm install onesignal-cordova-plugin
npx cap sync
Ionic + Cordova (Angular)
Add OneSignalInit() method in app.component.ts
constructor(platform: Platform) {
platform.ready().then(() => {
OneSignalInit(); //Add this line
});
}
Add the
OneSignalInit()
code block to the same file as noted above.import OneSignal from 'onesignal-cordova-plugin';
OneSignalInit(){
// NOTE: Update the setAppId value below with your OneSignal AppId.
OneSignal.setAppId("YOUR_ONESIGNAL_APP_ID");
OneSignal.setNotificationOpenedHandler(function(jsonData) {
console.log('notificationOpenedCallback: ' + JSON.stringify(jsonData));
});
// Prompts the user for notification permissions.
// * Since this shows a generic native prompt, we recommend instead using an In-App
Message to prompt for notification permission (See step 7) to better communicate to
your users what notifications they will get.
OneSignal.promptForPushNotificationsWithUserResponse(function(accepted) {
console.log("User accepted notifications: " + accepted);
});
}
Get Player Id from Onesignal plugin
submit(){
this.getPlayerId().then((playerid) => {
console.log("player id", playerid);
})
}
getPlayerId() {
return new Promise((resolve, reject) => {
OneSignal.getDeviceState((response) => {
resolve(response.userId);
})
})
}
Update Project Settings to Target Android 13 (Android Only)
Comments
Post a Comment