Titanium 在用户单击通知时如何运行一些代码? (安卓)

Posted

技术标签:

【中文标题】Titanium 在用户单击通知时如何运行一些代码? (安卓)【英文标题】:Titanium with how to run some code when user click on the notification? (Android) 【发布时间】:2015-11-15 10:59:25 【问题描述】:

当用户点击通知时如何运行一些代码?如果用户单击推送通知,我想了解来自推送通知。

我该怎么办?

谢谢 -Gcm模块- //我的gcm模块 var gcm = require("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();
        ,
    );

【问题讨论】:

【参考方案1】:
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();

【讨论】:

Appcelerator面板的推送通知也是成功的,但不代表设备。我该怎么办?

以上是关于Titanium 在用户单击通知时如何运行一些代码? (安卓)的主要内容,如果未能解决你的问题,请参考以下文章

Appcelerator Titanium 中的自定义通知视图

Firebase FCM onLaunch 代码在单击通知时未运行功能

Titanium 推送通知(Android 接收通知延迟)

有没有办法在发送本地通知时运行一些代码?

在前台单击推送通知横幅后如何触发操作

应用程序关闭时用户单击通知时如何获取用户信息?