简体   繁体   English

Titanium Appcelerator推送通知回调多次调用

[英]Titanium Appcelerator push notification callback called more than once

I have been able to register a mobile device and subscribe to a channel in Titanium . 我已经能够注册移动设备并订阅Titanium中的频道。 When the mobile device receives 2 push notifications and the user clicks on one of them . 当移动设备接收到2个推送通知并且用户单击其中之一时。

The callback is called twice . 该回调被调用两次 How do i know for which notification it was clicked or how do i Know the total number of push notification is pending? 我如何知道单击了哪个通知,或如何知道推送通知的总数?

    var CloudPush = require('ti.cloudpush');

    //CloudPush.singleCallback = true; 

    // Initialize the module
    CloudPush.retrieveDeviceToken({
        success : deviceTokenSuccess,
        error : deviceTokenError
    });

    // Enable push notifications for this device
    // Save the device token for subsequent API calls
    function deviceTokenSuccess(e) {
        //CloudPush.enabled = true;

        deviceToken = e.deviceToken;
        subscribeToChannel();
        //sendTestNotification();

    }

    function deviceTokenError(e) {
        //alert('Failed to register for push notifications! ' + e.error);
    }

    // Triggered when the push notifications is in the tray when the app is not running
    CloudPush.addEventListener('trayClickLaunchedApp', function(evt) {
        CloudPush.addEventListener('callback', function(evt) {
            var title = JSON.parse(evt.payload);

        });

    });
    // Triggered when the push notifications is in the tray when the app is running
    CloudPush.addEventListener('trayClickFocusedApp', function(evt) {

        CloudPush.addEventListener('callback', function(evt) {
            var title = JSON.parse(evt.payload);
        });

    });



var Cloud = require("ti.cloud");
function subscribeToChannel() {
    // Subscribes the device to the 'test' channel
    // Specify the push type as either 'android' for Android or 'ios' for iOS
    //alert(deviceToken+"ss");
    Titanium.App.Properties.setString("token", deviceToken);
    Cloud.PushNotifications.subscribeToken({
        device_token : deviceToken,
        channel : 'test',
        type : Ti.Platform.name == 'android' ? 'android' : 'ios'
    }, function(e) {
        if (e.success) {
            //alert('Subscribed');
        } else {
            alert('Error:\n' + ((e.error && e.message) || JSON.stringify(e)));
        }
    });
}

callback is called twice because you are implementing callback function inside : callback被调用了两次,因为您正在内部实现回调函数:

CloudPush.addEventListener('trayClickLaunchedApp', function(evt) {
    CloudPush.addEventListener('callback', function(evt) {
        var title = JSON.parse(evt.payload);

    }); 

and

CloudPush.addEventListener('trayClickFocusedApp', function(evt) {

    CloudPush.addEventListener('callback', function(evt) {
        var title = JSON.parse(evt.payload);
    });

});

Just implement the function like this : 只需实现如下功能:

CloudPush.addEventListener('callback', function(evt) {
var title = JSON.parse(evt.payload);
});

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

 });

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

});

For this How do I know for which notification it was clicked ? 为此,我如何知道单击了哪个通知?

Answer : You can check the badge number that you get in response from Push Notification inside callback function and based on that you can handle whether it is clicked or in pending state. :您可以检查在回调函数中从Push Notification响应中获得的徽章编号,并以此为基础来处理它是单击还是处于挂起状态。

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android的Appcelerator Titanium推送通知? - Appcelerator Titanium Push Notification for Android? Appcelerator钛Android推送通知GCM失败? - Appcelerator titanium Android push notification GCM failure? Appcelerator / Titanium:无法将推送通知发送到Android - Appcelerator/ Titanium: Cannot send push notification to Android Android模拟器的Appcelerator Titanium推送通知? - Appcelerator Titanium Push Notification for Android Emulator? Appcelerator Titan Push使用C2DM进行通知? - Appcelerator titanium push Notification using C2DM? 钛Android推送通知回调侦听器 - titanium android push notification callback listener 当startforeground多次调用时,通知remoteview重复 - Notification remoteview duplicates when startforeground called more than once Appcelerator Android CloudPush-调用了回调,但未清除通知 - Appcelerator Android CloudPush - callback is called but notification isn't cleared Android上使用appcelerator钛的多个预定安卓通知 - multiple scheduled android notification on android with appcelerator titanium Titanium Studio appcelerator(全部运行一次写入) - Titanium Studio appcelerator (write once run for all)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM