Advertisement
sombriks

onesignal register example

Jun 20th, 2016
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // serviços de alerta
  2. angular.module("roundabout").factory("alertaservice",function($http,$mdToast){
  3.   return {
  4.     onesignalinit:function(usuariologado){
  5.       //alert("Aqui")
  6.       if(!window.plugins){
  7.         console.debug("rodando web, nada de registrar token mobile");
  8.         return;
  9.       }
  10.       console.debug("registrando onesignal");
  11.       if(window.plugins.OneSignal == "undefined"){
  12.         alert("problema no plugin de notificações");
  13.         return;
  14.       }
  15.       // Enable to debug issues.
  16.       // window.plugins.OneSignal.setLogLevel({logLevel: 4, visualLevel: 4});
  17.      
  18.       // registrando esse dispositivo
  19.       //var onesignal_key = "<app 1 session key>";// solys
  20.       var onesignal_key = "<app 2 session key>";// ambbi
  21.  
  22.       // calback pra quando conseguir registrar
  23.       window.plugins.OneSignal.getIds(function(ids) {
  24.        
  25.         console.log('getIds: ' + JSON.stringify(ids));
  26.         $http({
  27.           url:"/alerta/registraostoken",
  28.           data:JSON.stringify({
  29.             oskey:onesignal_key,
  30.             idcondomino:usuariologado.idcondomino,
  31.             pushToken:ids.pushToken,
  32.             userId:ids.userId
  33.           }),
  34.           method:"POST"
  35.         });
  36.       });
  37.  
  38.       window.plugins.OneSignal.init(onesignal_key,{
  39.         googleProjectNumber: "<google project number>",
  40.         autoRegister:true
  41.       },function(data) {// callback de notificação
  42.         console.debug(data);
  43.         $mdToast.simple()
  44.           .title(data.aditionalData.title)
  45.           .textContent(data.message)
  46.           .action('OK')
  47.           .highlightAction(true)
  48.           .position({top:true})
  49.           .capsule(true);  
  50.       });
  51.      
  52.       // Show an alert box if a notification comes in when the user is in your app.
  53.       window.plugins.OneSignal.enableInAppAlertNotification(true);
  54.       window.plugins.OneSignal.registerForPushNotifications();
  55.     },
  56.     ultimos:function(idcondomino){
  57.       return $http({
  58.         params:{idcondomino:idcondomino},
  59.         url:"/alerta/ultimos",
  60.         method:"GET"
  61.       });
  62.     },
  63.     gerais:function(idcomunidade){
  64.       return $http({
  65.         params:{idcomunidade:idcomunidade},
  66.         url:"/alerta/gerais",
  67.         method:"GET"
  68.       });
  69.     },
  70.     novo:function(data){
  71.       return $http({
  72.         data:JSON.stringify(data),
  73.         url:"/alerta/novo",
  74.         method:"POST",
  75.       });
  76.     }
  77.   };
  78. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement