2015-08-21 2 views
0

как запустить код при нажатии пользователем на уведомление? если пользователь нажимает push-уведомление, я хочу понять, что происходит из push-уведомления.Titanium с тем, как запускать некоторый код, когда пользователь нажимает на уведомление? (Android)

Как я могу это сделать?

Благодаря -Gcm // Модуль-мой гсм модуль вар гсм = требуется ("nl.vanvianen.android.gcm");

/* If the app is started or resumed act on pending data saved when the notification was received */ 
    var lastData = gcm.getLastData(); 
    if (lastData) { 
     Ti.API.info("Last notification received " + JSON.stringify(lastData)); 
     gcm.clearLastData(); 
    } 

    gcm.registerPush({ 

     senderId : 'xxxxxxxx', 
     notificationSettings : { 
      sound : 'mysound.mp3', 
      smallIcon : 'notification_icon.png', 
      largeIcon : 'appicon.png', 
      vibrate : true 
     }, 
     //Push registration success 
     success : function(event) { 
      Ti.API.info("Push registration success: " + JSON.stringify(event)); 

     }, 
//Push registration error 
     error : function(event) { 
      Ti.API.info("Push registration error = " + JSON.stringify(event)); 
      alert(event.error); 
     }, 
     //i want this function Coming from push 
     data : function(event) { 

      // console.log(" ******* Coming from push : " + JSON.stringify(event)); 


     }, 
     //my callback funtion call device token 
     callback : function(event) { 
      Ti.API.info("Push callback = " + JSON.stringify(event)); 

      var dialog = Ti.UI.createAlertDialog({ 
       title : 'Push received', 
       message : JSON.stringify(event.data) 

       // buttonNames: ['View'], 
       // cancel: 1 

      }); 
      dialog.addEventListener("click", function(event) { 
       dialog.hide(); 
       if (event.index == 0) { 
        /* Do stuff to view the notification */ 
       } 
      }); 
      dialog.show(); 
     }, 
    }); 

ответ

0
Titanium.UI.setBackgroundColor('#000'); 

var win = Ti.UI.createWindow({ 
    backgroundColor : '#ccc', 
    title : 'Android Cloud Push Notification' 
}); 

var CloudPush = require('ti.cloudpush'); 
CloudPush.debug = true; 
CloudPush.enabled = true; 
CloudPush.showTrayNotification = true; 
CloudPush.showTrayNotificationsWhenFocused = true; 
CloudPush.focusAppOnPush = false; 

var deviceToken; 
var Cloud = require('ti.cloud'); 
Cloud.debug = true; 

var submit = Ti.UI.createButton({ 
    title : 'Register For Push Notification', 
    color : '#000', 
    height : 53, 
    width : 200, 
    top : 100, 
}); 

win.add(submit); 

submit.addEventListener('click', function(e) { 
    CloudPush.retrieveDeviceToken({ 
     success : function deviceTokenSuccess(e) { 
      alert('Device Token: ' + e.deviceToken); 
      deviceToken = e.deviceToken; 
      loginDefault(); 
     }, 
     error : function deviceTokenError(e) { 
      alert('Failed to register for push! ' + e.error); 
     } 
    }); 
}); 

function loginDefault(e) { 
    // At first you need to create an user from the application dashboard 
    // Then login that email and password 
    Cloud.Users.login({ 
     login : '.....', 
     password : '....' 
    }, function(e) { 
     if (e.success) { 
      alert("login success"); 
      defaultSubscribe(); 
     } else { 
      alert('Error: ' + ((e.error && e.message) || JSON.stringify(e))); 
     } 
    }); 
} 

function defaultSubscribe() { 
    Cloud.PushNotifications.subscribe({ 
     channel : 'alert', 
     device_token : deviceToken, 
     type : 'android' 
    }, function(e) { 
     if (e.success) { 
      alert('Subscribed for Push Notification!'); 
     } else { 
      alert('Error:' + ((e.error && e.message) || JSON.stringify(e))); 
     } 
    }); 
} 

CloudPush.addEventListener('callback', function(evt) { 
    //alert(evt); 
    //alert(evt.payload); 
}); 

CloudPush.addEventListener('trayClickLaunchedApp', function(evt) { 
    Ti.API.info('Tray Click Launched App (app was not running)'); 
    //alert('Tray Click Launched App (app was not running'); 
}); 

CloudPush.addEventListener('trayClickFocusedApp', function(evt) { 
    Ti.API.info('Tray Click Focused App (app was already running)'); 
    //alert('Tray Click Focused App (app was already running)'); 
}); 

win.open(); 
+0

Он также успешно на нажимной уведомления Appcelerator панели идет в одну сторону, но не означает, что устройство. что мне делать? – KaraEski

Смежные вопросы