初始化推送通知时发生奇怪的 ionic 内存泄漏导致冻结

Posted

技术标签:

【中文标题】初始化推送通知时发生奇怪的 ionic 内存泄漏导致冻结【英文标题】:Strange memory leaks with ionic while initializing push notifications cause freezing 【发布时间】:2016-03-07 10:51:00 【问题描述】:

这几天我遇到了一些非常奇怪的记忆问题。

问题是,有时应用程序会卡住并开始非常快速地增加内存使用量,直到崩溃。当内存增加时,应用程序完全冻结。

经过一些调试,我发现此代码导致了错误:

angular.module('app.shared').factory('PushNotificationService',         PushNotificationService);

PushNotificationService.$inject = [
    '$q',
    'MessagingService'
];

function PushNotificationService($q, MessagingService) 
var me = this;

initialize();

return 
    getStartupMessage: getStartupMessage,
    fetchToken: fetchToken
;

/**
 * Constructor
 * @return [type] [description]
 */
function initialize() 
    me.pusher = null;
    me.deviceToken = null;
    me.startupMessage = null;


/**
 * Fetches the push token through device interface
 *
 * @return $q Promises
 */
function fetchToken() 
    if (me.pusher != null) 
        return $q(function(resolve, reject) 
            console.log('PushService.fetchToken(): Got pusher', me.pusher);

            // when pusher was already initialized, we do not need to fetch it again
            console.log('PushService.fetchToken(): Token was already retrieved', me.deviceToken);
            resolve(me.deviceToken);
        );
    

    console.log('PushService.fetchToken(): Needs to fetch token bc not retrieved yet');

    return $q(function(resolve, reject) 
        document.addEventListener('deviceready', function() 
            console.log('PushService.fetchToken(): Device is ready', typeof resolve, typeof reject);

            resolve(true);
        , false);
    ).then(function() 
        console.log('PushService.fetchToken(): No pusher retrieved yet, do it now');

        return __initialize();
    ).then(function(push) 
        console.log('PushService.fetchToken(): Got pusher and start attaching events to it', push);

        return $q(function(resolve, reject) 
            push.on('error', function(error) 
                console.log('PushNotificationService.fetchToken(): Error while retrieving token', error.message, error);

                reject(error);
            );

            push.on('registration', function(data) 
                console.log('PushNotificationService.fetchToken(): Successfully retrieved token', data);

                me.deviceToken = data.registrationId;

                resolve(data.registrationId);
            );

            console.log('PushNotificationService.fetchToken(): Eventhandlers of pusher', push._handlers);
        );
    );


/**
 * Initializes the push notifications and tries to fetch the Push token
 * @return Cordova.Pusher [description]
 */
function __initialize() 
    me.pusher = PushNotification.init(
        android: 
            senderID: "288503736094"
        ,
        ios: 
            alert: true,
            badge: true,
            sound: true,
            clearBadge: true
        ,
        windows: 
    );

    me.pusher.on('notification', __incomingNotification);

    return me.pusher;


// additional code which is not relevant here...
// .....
// .....

它只发生在 iOS 上,完全随机,无法在崩溃中找到系统。

调试日志是这样的:

PushService.fetchToken(): Needs to fetch token bc not retrieved yet 

PushService.fetchToken(): Device is ready function function 

PushService.fetchToken(): No pusher retrieved yet, do it now 

PushService.fetchToken(): Got pusher and start attaching events to it "_handlers":"registration":[],"notification":[null],"error":[],"options":"android":"senderID":"288503736094","ios":"alert":true,"badge":true,"sound":true,"clearBadge":true,"windows": 

PushNotificationService.fetchToken(): Eventhandlers of pusher "registration":[null],"notification":[null],"error":[null]

【问题讨论】:

【参考方案1】:

这是 cordova-ios 中的一个问题,导致随机位置出现大量内存泄漏。

升级到最新版本修复它!

【讨论】:

以上是关于初始化推送通知时发生奇怪的 ionic 内存泄漏导致冻结的主要内容,如果未能解决你的问题,请参考以下文章

带有 FCM 的 Ionic 2 推送通知

使用 ionic 和谷歌云在网站上发送特定事件的推送通知

Ionic 4 & Firebase (FCM) - 如何分组推送通知

应用程序关闭时的 Ionic 3 推送通知

单击 Ionic 中的推送通知时如何打开特定页面?

收到来自 Ionic 推送的通知时如何打开到特定状态?