ios - Appcelerator iOS 推送通知在应用程序处于前台时不起作用

标签 ios push-notification titanium appcelerator

我知道 this问题。但我认为这个问题仍然存在。所以我的要求是不要将这个问题标记为重复。

当 iOS 应用程序处于后台或未运行时,设备会收到通知。但是当应用程序处于前台时,通知不会显示。我正在使用文档中提到的相同代码 here

操作系统:iOS 11.2.5, 手机:iPhone 6 钛SDK:7.0.2.GA

下面是我的代码供引用。

exports.createNotification = function() {
if (Ti.UI.iOS.appBadge !== 0) {
    Ti.UI.iOS.appBadge = 0;
}

var deviceToken = null;
// Check if the device is running iOS 8 or later
if (Ti.Platform.name == "iOS" && parseInt(Ti.Platform.version.split(".")[0]) >= 8) {

    // Wait for user settings to be registered before registering for push notifications
    Ti.App.iOS.addEventListener('usernotificationsettings', function registerForPush() {

        // Remove event listener once registered for push notifications
        Ti.App.iOS.removeEventListener('usernotificationsettings', registerForPush);

        Ti.Network.registerUserNotificationSettings({
            success : deviceTokenSuccess,
            error : deviceTokenError,
            callback : receivePush
        });
    });

    // Register notification types to use
    Ti.App.iOS.registerUserNotificationSettings({
        types : [Ti.Network.NOTIFICATION_TYPE_BADGE, Ti.Network.NOTIFICATION_TYPE_ALERT, Ti.Network.NOTIFICATION_TYPE_SOUND]
    });
}

// For iOS 7 and earlier
else {
    Ti.Network.registerForPushNotifications({
        // Specifies which notifications to receive
        types : [Ti.Network.NOTIFICATION_TYPE_BADGE, Ti.Network.NOTIFICATION_TYPE_ALERT, Ti.Network.NOTIFICATION_TYPE_SOUND],
        success : deviceTokenSuccess,
        error : deviceTokenError,
        callback : receivePush
    });
}
// Process incoming push notifications
function receivePush(e) {
    Ti.API.info('Received push: ' + JSON.stringify(e));
    //alert('Received push: ' + JSON.stringify(e));

    if (e.data !== null) {
        //exports.insertPushToDb(e.data);
    }
}

// Save the device token for subsequent API calls
function deviceTokenSuccess(e) {

    deviceToken = e.deviceToken;
    Ti.App.Properties.setString('PushNotificationRegID', deviceToken);
    Ti.API.info('Push notification: ' + deviceToken);
    //alert('deviceToken ' + deviceToken);
}

function deviceTokenError(e) {
    Ti.API.info('Error Noti: '+JSON.stringify(e));
    //alert('Failed to register for push notifications! ' + e.error);
}

};

提前致谢。

最佳答案

您将显示推送的行注释掉了!

 //alert('Received push: ' + JSON.stringify(e));

重新启用这条线,你会再次看到它。

关于ios - Appcelerator iOS 推送通知在应用程序处于前台时不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48943311/

相关文章:

iphone - 如何在 iPhone 中获取推送通知

android - Titanium studio 无法在终端运行 "titanium"命令

javascript - 获取聊天记录pubnub中的消息时间

ios - objective C protocol issue Detail -> 主通信

ios - 如何在xcode iphone上限制移动?

ios - 如何找到 subview 的数量?

ios - Storyboard和程序化 View

ios - 当我的应用程序在后台运行时,是否可以在通知上设置 PresentationsOptions?

ios - WatchKit:在通知短外观和长外观中更改应用程序名称?

ios - Titanium:无需设备即可生成 IPA