Did You Know?

JOOJ - Best software development company exercise agility to deliver web

Push Notifications API

Author: admin 711 views

Push Notifications API via OneSignal, Firebase, and JavaScript

Push notifications are notifications that appear on your smartphone without opening the app. These types of messages allow fantastic interaction with customers and users of the app. Your app does not have to be open. This way, your customers do not miss any important news or messages. Push Notifications are suitable for many cases, e.g., for updates to orders, live tickers, or social media community. There must be an internet connection to allow app users to receive push messages. WebViewGold supports multiple ways of reaching your users via push notifications.

Option 1: Use Local Push Notification JavaScript API

You can insert a piece of code (JavaScript) in your website, to define a push notification, which can be shown after your app was closed (while scheduling it during the app is open). This works without OneSignal configuration at all.

<script>
var seconds = 120; //seconds from now on
var message = "It is Happy Hour now! Check it out!"; //the message
var button = "Open App"; //the text of the button
window.location.href = "sendlocalpushmsg://push.send?s="+ seconds +"=msg!"+ message +"&!#"+ button +"";
</script>
      


It could be used for different situations (countdowns, happy hours (like in the example code), reminders, …).
To cancel all scheduled local push notifcations run:

<script>
window.location.href = "sendlocalpushmsg://push.send.cancel";
</script>
      


Option 2: Use OneSignal Remote Push Notifications

If you want to use OneSignal in your app, activate these options in Config.java (by switching the values from false to true):
Configuration of OneSignal

Please set your OneSignal App ID in build.gradle file afterward:


Enter your application identifier of OneSignal to enable push notifications


Set up the OneSignal service for your Android app. Yay, you are done. You can send push notifications now:

How to send a push message in OneSignal

If you want to use OneSignal URL Deeplinking (opens a specific URL when clicking on your notification), please add the desired URL with a custom field called url:

Add a custom URL for deeplinking purposes in WebView


On the OneSignal.com Backend, you can fill in this key-value-pair window after clicking on “Advanced settings”. Using the OneSignal API, you can add the “url” field to the API request (https://documentation.onesignal.com/docs/onesignal-api).

Do you want to get each individual user ID on your server for further processing and individual user push messages? Just activate the PUSH_ENHANCDE_WEBVIEW_URL option in Confing.java (by switching the value from false to true) in order to append ?onesignal_push_id=XYZ to your WebView URL. If your WebView URL is https://www.example.org, WebViewGold will call https://www.example.org?onesignal_push_id=XYZ instead. Only your FIRST URL request will get that GET variable, so save it in a session or in a cookie to access it on your linked pages.



Option 3: Use Firebase Remote Push Notifications

1. Create a Firebase project
Before you can add Firebase to your WebViewGold app, you have to create a Firebase project in your Firebase Console.

2. Register your app with Firebase
After you created your Firebase project, you can add your WebViewGold app to it.

a. In the center of the Firebase console’s project overview page, click the Android icon to launch the setup workflow.

b. If you’ve already added an app to your Firebase project, click Add app to display the platform options. Enter your app’s package name as changed/defined in this step. Make sure that you enter the package name that your app is using. You cannot add or modify this value after you register your app with your Firebase project.

4. Click the Register app button.

5. Download google-services.json

Click Download google-services.json to obtain your Firebase Android config file (google-services.json) and replace the sample file already included in the WebViewGold project buy using the Mac Finder or the Windows Explorer:

Replace google-services.json in your WebViewGold Android App

6. Activate Firebase in WebViewGold

Go to Config.java file and turn the FIREBASE_PUSH_ENABLED variable to true.

Replace google-services.json in your WebViewGold Android App

7. That’s it!

Do you want to get each individual user ID on your server for further processing and individual user push messages? Just activate the FIREBASE_PUSH_ENHANCE_WEBVIEW_URL option in Confing.java (by switching the value from false to true) in order to append ?firebase_push_id=XYZ to your WebView URL. If your WebView URL is https://www.example.org, WebViewGold will call https://www.example.org?firebase_push_id=XYZ instead. Only your FIRST URL request will get that GET variable, so save it in a session or in a cookie to access it on your linked pages.



Option 4: Use bubble.io Push Notifications

1. Create a bubble.io app
2. Install the WebViewGold OneSignal Push plugin:
bubble.io WebViewGold OneSignal Push plugin
3. Follow the OneSignal instructions (see above) and the plugin instructions (see here and here).

Loading...