Search code examples
phppush-notificationmoengage

Latest Notification alerts


I am using https://www.moengage.com/ to generate the smart trigger campaign notification alert for my users for desktop and mobile. using their <link rel="manifest" href="manifest.json">

<script type="text/javascript">
(function(i,s,o,g,r,a,m,n){
i['moengage_object']=r;t={}; q = function(f){return function(){(i['moengage_q']=i['moengage_q']||[]).push({f:f,a:arguments});};};
f = ['track_event','add_user_attribute','add_first_name','add_last_name','add_email','add_mobile',
'add_user_name','add_gender','add_birthday','destroy_session','add_unique_user_id','moe_events','call_web_push','track','location_type_attribute'];
for(k in f){t[f[k]]=q(f[k]);}
a=s.createElement(o);m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m);
i['moe']=i['moe'] || function(){n=arguments[0];return t;}; a.onload=function(){if(n){i[r] = moe(n);}};
})(window,document,'script','https://cdn.moengage.com/webpush/moe_webSdk.min.latest.js','Moengage'); 

Moengage = moe({
  app_id:"MyAppID",
  debug_logs: 0
}); 
</script>

campaign is created and I can view the notification alert pop up but when I click "allow" it is not throwing the notifications.

how do I solve this ?


Solution

  • Hi I work at MoEngage currently. Smart Triggers are not instantaneous. There is a processing time of about 5 minutes before the notification is sent out. What you can do instead is wait for our callback event (docs -> https://docs.moengage.com/docs/tracking-opt-ins-on-your-own#section-this-is-how-you-can-add-listeners-for-the-events-mentioned-above-) and show a welcome notification by javascript.

    ...
    else if (e.detail == "opt_in_allowed") {
      spawnNotification();
    }
    ...
    
    function spawnNotification() {
      var title = "Your title";
      var body = "Your body";
      var icon = "https://path.to.your/icon/file";
      var options = {
        body: body,
        icon: icon
      };
      navigator.serviceWorker.ready.then(function(sw_reg) {
        var n = sw_reg.showNotification(title, options);
      }).catch(function(e) {
        console.log(e);
      });
    }