This quickstart tutorial will show you how to send a Push Notification with an Ionic 4/5 app.
To learn more about the component, and any other settings or options, please see the notification documentation page.
Testing Push Notifications
To test sending and receiving Push Notification messages the app has to be installed on the device.
If this is your first time working with Push Notifications, we recommend you create a new app so that it will not impact anything else you are working on.
Please follow the steps on the Configuration page to learn how to enable and configure Push Notifications.
In Push Notifications > Settings, keep the Show alert checkbox checked. Otherwise, the issue with push notifications permissions on iOS devices can be observed.
- From the Apps page, click the Create new app button.
- Select Ionic 4/5 > Ionic 4/5 Blank for the application type, enter PushNotificationsApp for the app name, and click Create.
- Once the App Builder loads, from the left menu, go to the Project > App Settings > Cordova plugins. Under the Core Cordova Plugins tab, select PushPlugin click the SAVE button at the top:
Whether you use an existing or a new app, you need to create a very simple UI to handle Push Notifications.
- Unfold the Pages folder, navigate to Pages > Screen1 and select the DESIGN tab.
- Now, select the ToolbarTitle1 component and change it's Text property to Push Notifications App. You can also add the needed components to the page. For example, drag & drop a Text component into the app. Here is an example:
- Switch to the app page and expand its EVENTS panel.
- Add the Push registration success event for the Page component . Then, add the Present alert action. For the alert, set the Device has been registered message and save:
- Now, for the same Page component, add the Push registration fail event with the Present alert action. For the alert, set this message: Device registration failed.
- Finally, add one more event for this page, Push notification and, for its action, select Run TypeScript, enter the code below and save:
Here is how the EVENTS panel with all the events added should look like:
If different event handlers are added to different pages, the most recent event will be prioritized in handling (processing) a notification. This can be used to activate/deactivate alerts when opening a particular page.
After starting the application, you should receive a notification that the device has been registered.
Both iOS and Android handle notifications in the following way:
- When the app is in the background (not active), a standard notification will be shown to the user.
- When the app is in the foreground (active), a standard notification will not be shown as the app is currently active. The notification will still arrive, but you simply need to add an event handler (described in Step 4) to handle (process) this notification.
This is just a sample behavior and you might want to invoke another action to process the notification in your project.
Want to know more?
Also, you can check this tutorial to learn how you can keep application logic working when in the background mode.
Note that you need to test it on a device as the plugin won’t work inside the browser.
So, to be able to receive Push Notification messages, build a binary for the app and install it on the device. This App Export section covers how to build a binary.
Make sure you follow all the steps described in the Configuration document. Otherwise, no push notifications will be sent.
Now, when you have successfully registered your device, you can arrange for sending your first Push Notification.
- Go to Apps > PushNotificationsApp > Push Notifications > Send Push Notification.
- For Send Push Notification, keep the All Devices option selected.
- For Select device types. select the device type to which you want to send the message. You can select both if you have installed the app on iOS and Android.
- For Badge, enter: 1.
- For Title, enter: First Push Message.
- For Message, enter: First message with the Appery.io platform!:
- Now, scroll to the bottom of the page, check that the needed devices have been registered, and click the Send button to send the message.
To learn more about sending pushes, check this document.
Within a few seconds, your device should receive a Push Notification message you have just sent:
Updated about 1 month ago