Need to ask your users for push notification permission at just the right moment? We've got you covered! This guide shows you how to trigger the push notification prompt whenever it makes sense for your users.
You can trigger the push notification prompt with a simple method:
nativeFunctions.triggerPushPrompt()
Here's something important to know: iOS and Android handle push permission requests differently after a user has declined them once:
triggerPushPrompt()
will show the native system permission dialog.This behavior means you don't have to worry about handling different permission states - the function adapts automatically to give users the right path forward!
Here's a snippet that you can drop into your app to create a smart notification prompt button:
// First, check if we're running in Canvas
if (navigator.userAgent.toLowerCase().includes('canvas')) {
// Let's grab user info from wherever it's available
var userObject = null;
// Try iOS method first
if (window.mobiloudAppInfo) {
userObject = window.mobiloudAppInfo;
}
// If that doesn't work, try the Android method
else if (nativeFunctions.getDeviceInfo) {
userObject = nativeFunctions.getDeviceInfo();
}
// Only show the button if the user hasn't subscribed yet
if (userObject && userObject.pushSubscribed === false) {
// Add a push notification button to your page
document.write('<button onclick="nativeFunctions.triggerPushPrompt()">Get Notified!</button>');
}
}
When you get device info, you'll receive an object packed with useful stuff:
{
appId: string, // Your app's ID
appVersion: string, // The version of your app
brand: string, // Device brand name
hardware: string, // What's under the hood
manufacturer: string, // Who made the device
model: string, // Device model
nameos: string, // OS name
osVersion: string, // OS version number
platform: string, // iOS or Android
pushSubscribed: boolean, // The important bit! Is the user subscribed?
pushToken: string, // User's push token
pushUserId: string, // User ID for push services
timeZone: string // User's local time zone
}
Ready to boost your engagement with perfectly-timed push notifications? Let's do this! 🚀