Custom Code for OneSign Push Notifications

Below OneSign custom code will do the follow features:

  • Show small subscription bell only when users are not subscribed yet
  • Hide the OneSign logo
  • Delay subscription request prompt
  • Customize notification text
  • Send custom notification welcome message

Implementation

Reference: https://documentation.onesignal.com/docs/web-push-custom-code-setup

Generate and upload SDK files then add below code in HTML header

<link rel="manifest" href="/manifest.json">
<script src="https://cdn.onesignal.com/sdks/OneSignalSDK.js" async></script>
<script src="/onesignal-moddiy.js" async></script> 

In onesignal-moddiy.js file

var OneSignal = window.OneSignal || [];
OneSignal.push(function() {
	OneSignal.init({
		appId: "f0964f83-6f53-4152-b9bd-aee6ee68a440",
		autoResubscribe: true,
		autoRegister: false,
		notifyButton: {
			enable: true,
			size: 'small',
			theme: 'default',
			position: 'bottom-left',
			showCredit: false,
			prenotify: true,
			displayPredicate: function() {
				return OneSignal.isPushNotificationsEnabled()
				.then(function(isPushEnabled) { return !isPushEnabled;});
			},
			text: {
				'tip.state.unsubscribed': 'Get instant discount coupon',
				'tip.state.blocked': "You've blocked instant discount coupon",
				'message.prenotify': 'Click to get instant discount coupon',
				'dialog.blocked.title': 'Unblock Instant Coupon',
				'dialog.blocked.message': "Follow these instructions to allow instant discount coupons and important notifications:"
			}
		},
		welcomeNotification: {
			"title": "Thank you for subscribing!",
			"message": "Get 10% off all products today, please use coupon code PU10SH, enjoy :)",
			"url": "https://www.moddiy.com/pages/Thank-you-for-subscribing%21.html"
		}
	});
	var notificationPromptDelay = 15000;
	var navigationStart = window.performance.timing.navigationStart;
	var timeNow = Date.now();
    setTimeout(promptAndSubscribeUser, Math.max(notificationPromptDelay - (timeNow - navigationStart), 0));
});

function promptAndSubscribeUser() {
	window.OneSignal.isPushNotificationsEnabled(function(isEnabled) {
		if (!isEnabled) {
			OneSignal.push(function() {OneSignal.registerForPushNotifications();});
		}
	});
}

Leave a Reply

Your email address will not be published. Required fields are marked *