Skip to main content

Ionic & Capacitor SDK Setup - OneSignal Documentation

 Instructions for adding the OneSignal SDK to your Ionic or Capacitor app for iOS, Android

Requirements

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)

Validate your target SDK version is at least version 33.

Requirements:

  • Ionic Capacitor v2 or higher
android {
compileSdkVersion 33
...

defaultConfig {
...
targetSdkVersion 33
}
}

If you like this post. Please share with your friends and help others to solve this problem. You can
comment on my post if you have some problem about content of the post or you face some new issue
in Ionic Application or Android Native Application.
If you need professional help for your project do let us know. 
You can directly contact us on developers.omitech@gmail.com


Comments

Popular posts from this blog

How to align Title at center of ActionBar in Android

How to Align Title At Center of Action Bar in Android                                                                                                                                                @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); setTitle("DashBoard"); }   Activity  public void setTitle ( String title ){ getSupportActionBar (). setHomeButtonEnabled ( true ); getSupportActionBar (). setDisplayHomeAsUpEnabled ( true ); TextView textView = new TextView ( this ); textView . setText ( title ); textView . setTextSize ( 20 ); textView . setTypeface ( null , Typeface . BOLD ); textView . setLayoutParams ( new LinearLayout . LayoutParams ( LinearLayout . LayoutParams . FILL_PARENT , LinearLayout . LayoutParams . WRAP_CONTENT )); textView . setGravity ( Gravity . CENTER ); textView . setTextColor ( getResources (). ge

Difference Between Pending Intent And Intent in Android

                       Normal Intent       Normal Intent will die as soon as the app being killed.    An Android Intent is an object carrying an intent, i.e a message from one Component to another     Component either inside or outside of the application.Intent can communicate message among     any of the three core Components of an application -- Activities, Services,and BroadcastReceivers.     Two types of Intent in Android   1. Explicit Intent.   2.Implicit Intent  Explicit Intent is an Intent which is used to Call the another component Explicitly in your application  Like :We are calling  Next activity on button click of First activity using Intent Example  // Explicit Intent by specifying its class name Intent i = new Intent ( this , TargetActivity . class ); i . putExtra ( "Key1" , "ABC" ); i . putExtra ( "Key2" , "123" ); // Starts TargetActivity startActivity ( i );  Implicit Intent Intent

Expected a key while parsing a block mapping (Flutter)

Flutter makes use of the Dart packaging system, pub. Via your applications  pubspec.yam l file (or simple pubspec), you can pull down packages from the flutter ecosystem, or the broader dart community. Anyway, i need to add some images to my flutter application, and so had to add an assets section to the pubspec .the default Android Studio generated apps pubspec has a lot of commented out code with explainations about what is going on, e.g # To add assets to your application, add an assets section, like this: # assets: # - images/a_dot_burr.jpeg # - images/a_dot_ham.jpeg # An image asset can refer to one or more resolution-specific "variants", see # https://flutter.io/assets-and-images/#resolution-aware. So I uncommented these lines # assest : - - - #  -  images/a_dot_ham.jpeg with the idea from these comment.  that i would just edit it to suit my particular needs. Once you have edited your  pubspec, you need to click on the "Get dependencies"