【问题标题】:Titanium: Check iOS notifications upon opening the app without clicking the notificationTitanium:在打开应用程序时检查 iOS 通知而不点击通知
【发布时间】:2017-05-04 11:48:25
【问题描述】:

我正在尝试在 iOS 上处理推送通知。

我的简单代码如下所示:

var Cloud = require("ti.cloud");
var deviceToken = null;

var deviceToken = Ti.App.Properties.getString('deviceToken');

Ti.App.iOS.registerUserNotificationSettings({
    types: [
        Ti.App.iOS.USER_NOTIFICATION_TYPE_ALERT,
        Ti.App.iOS.USER_NOTIFICATION_TYPE_SOUND,
        Ti.App.iOS.USER_NOTIFICATION_TYPE_BADGE
    ]
});

Ti.App.iOS.addEventListener('usernotificationsettings', function registerForPush() {
    Ti.App.iOS.removeEventListener('usernotificationsettings', registerForPush); 

    Ti.Network.registerForPushNotifications({
        success: function(e) {
            if (e.deviceToken !== Ti.App.Properties.getString('deviceToken', null)) {
                deviceToken = e.deviceToken;
                Ti.App.Properties.setString('deviceToken', deviceToken)
                subscribeToChannel();
            } else {
                Ti.API.info('Already registered for push notifications!');
            }
        },
        error: function(e) {
            Ti.API.error('Failed to register for push notifications: ' + e.error);
        },
        callback: receivePush
    });

});

function subscribeToChannel () {
    Cloud.PushNotifications.subscribeToken({
        device_token: deviceToken,
        channel: 'general',
        type: Ti.Platform.name == 'android' ? 'android' : 'ios'
    }, function (e) {
          alert(e.success === true ? 'Subscribed' : 'Error!');
    });
}

// When receieve interactive remote notification
Ti.App.iOS.addEventListener('remotenotificationaction', function(e) {
    alert('remotenotificationaction: ' + JSON.stringify(e));
});

// When receieve interactive notification in the background
Ti.App.iOS.addEventListener('localnotificationaction', function(e) {
    alert('localnotificationaction');
});

// When receieve interactive notification in the foreground 
Ti.App.iOS.addEventListener('notification', function(e) { 
    alert('notification');
});

function receivePush(e) {
    alert('receivePush');
}

在大多数情况下,一切正常。当我发送远程推送通知时会发生以下情况:

  • 当应用程序在后台时,会出现一条通知。单击通知后,我按预期收到“receivePush”消息
  • 当应用程序在前台时,没有出现通知,但我仍然按预期收到“receivePush”消息。
  • 但是,当我在应用处于后台时收到通知,然后直接点击应用(即不点击通知),以上事件均未触发!

如何确保为最后一种情况触发事件。

【问题讨论】:

    标签: ios titanium appcelerator appcelerator-titanium titanium-alloy


    【解决方案1】:

    我认为这是不可能的,因为您的回调函数被分配了通知行为,而不是应用程序启动。如果您知道我的意思,这不是 Titanium 问题,而是工作流程误解。 我认为对你来说最好总是在应用启动时检查一些与通知无关的内容。

    【讨论】:

    • 我想我明白你的意思了,但是当应用在后台运行并且通知还没有被点击的时候,我刚刚收到通知,至少必须有一个回调。
    猜你喜欢
    • 1970-01-01
    • 2014-12-22
    • 1970-01-01
    • 2012-11-22
    • 1970-01-01
    • 2020-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多