【问题标题】:Titanium appcelerator error when opening window after push notification received收到推送通知后打开窗口时出现 Titanium appcelerator 错误
【发布时间】:2017-06-04 05:17:53
【问题描述】:

我在一个小应用程序中使用了钛应用加速器,并使用 pushwoosh 作为通知服务器。

在我的 index.xml 中,我有以下内容:

<Alloy>
	<!-- Anddroid Window -->
	<Window id="index" platform="android">
		<Require type="view" id="firstscreen" src="firstscreen"/>
	</Window>

	<!-- iOS Window -->
	<NavigationWindow id="nav" platform="ios">
		<Window id="win1" backgroundColor="white">
			<Require type="view" id="firstscreen" src="firstscreen"/>
		</Window>
	</NavigationWindow>
</Alloy>

其次是index.js,比如我收到push,想把用户重定向到登录js,目的是从push自定义值打开对应的页面,但这里我做的很简单,只是为了测试。

if (OS_ANDROID) {
	$.index.addEventListener('open', after_win_load);
	$.index.open();
} else {
	$.nav.addEventListener('open', after_win_load);
	$.nav.open();
}

var pushwoosh = require('com.pushwoosh.module');
/*
 * PUSHWOOSH
 * */

pushwoosh.onPushOpened(function(e) {
  var message = e.message;
  var login = Alloy.createController('login').getView();
      $.nav.open(login);
});

pushwoosh.initialize({ 
    "application" : "XXXX-XXXXXX",
    "gcm_project" : "XXXXXXXXXXX"
});

pushwoosh.registerForPushNotifications(
  function(e) {
        var pushToken = e.registrationId;
        ;
		console.log('Push token ' + pushwoosh.getPushToken());
		Alloy.Globals.resgisterId =  e.registrationId;

    },
    function(e) {
        var errorMessage = e.error;
       console.log("Error during registration: " + e.error);
       // alert('push error');
    }  
);

还有最后的 login.xml 和 login.js

<Alloy>
	<Window id="login" >
		<ScrollView scrollingEnabled="true" contentWidth="Ti.UI.FILL" disableBounce="true">
			<!-- Here another view -->
		</ScrollView>
	</Window>
</Alloy>

//// login.js is simple :
var args = $.args;
console.log('hey boy');

收到推送通知时,点击它重定向到登录 js 我有以下错误:

[WARN] :   Creating [object login] in a different context than the calling function.
[WARN] :   Creating [object __alloyId48] in a different context than the calling function.
[ERROR] :  Script Error {
[ERROR] :      column = 2330;
[ERROR] :      line = 1;
[ERROR] :      message = "null is not an object (evaluating 'a.__views.login.add')";
[ERROR] :      sourceURL = "file:///var/containers/Bundle/Application/ADE5F25A-17A4-4197-98C7-0781216545A3/myApp.app/alloy/controllers/login.js";
[ERROR] :      stack = "Controller@file:///var/containers/Bundle/Application/ADE5F25A-17A4-4197-98C7-0781216545A3/myApp.app/alloy/controllers/login.js:1:2330\ncreateController@file:///var/containers/Bundle/Application/ADE5F25A-17A4-4197-98C7-0781216545A3/myApp.app/alloy.js:1:5254\nopenWin@file:///var/containers/Bundle/Application/ADE5F25A-17A4-4197-98C7-0781216545A3/myApp.app/xpng.js:1:283\nfile:///var/containers/Bundle/Application/ADE5F25A-17A4-4197-98C7-0781216545A3/myApp.app/alloy/controllers/firstscreen.js:1:3855";
[ERROR] :  }

我不知道错误在哪里,你能帮我解决这个问题吗? 谢谢。

【问题讨论】:

  • 注意:如果我将 Alloy.createController('login').getView().open() 放在 Pushwoosh 事件之外,我只能转到另一个页面

标签: push-notification titanium appcelerator appcelerator-titanium pushwoosh


【解决方案1】:

您只需要对代码稍作改动:

pushwoosh.onPushOpened(function(e) {
    var message = e.message;

    var login = Alloy.createController('login').getView();
    OS_IOS ? $.nav.openWindow(login) : login.open();
});

iOS - 你需要使用 NavigationWindowopenWindow() 方法,Android 很简单open() 调用。

注意:

由于您提到要将用户导航到应用程序的不同部分,因此在打开另一个窗口之前,您需要注意您的 NavigationWindow 是否存在。

这就是为什么您会收到 null 错误,因为当您收到通知并点击它时,它会打开应用程序并运行此 pushwoosh.onPushOpened 方法,直到此时您还没有创建的任何 NavigationWindow。因此,您需要不同的流程来导航到不同的部分。

  • 点击通知后,如果您的应用在后台模式下运行,那么我相信您不会收到此错误,因为您已经有一个 NavigationWindow已创建,

  • 但是如果您的应用处于终止状态并且您收到并点击通知,那么您将收到此错误,因为您的应用尚未创建 NavigationWindow (这就是为什么您会在控制台上看到不同的上下文)。

因此,要执行您想要的操作,您需要创建一个不同的流程来处理在收到推送消息时打开应用程序登录窗口的场景。 (简单来说,您仍然需要创建 NavigationWindow 并在其中打开 login 窗口或其他方法)。

我希望您现在清楚地知道是什么导致您的应用显示该错误。

【讨论】:

  • 你好,谢谢你,但它不起作用,我创建了一个新的登录页面,控制器内部没有任何内容,但我得到了同样的错误:脚本错误 { [错误]:列 = 1112; [错误]:行 = 1; [错误] : message = "null 不是对象(正在评估 'i.__views.login.add')";
  • 你好,谢谢,但它不起作用,我创建了一个新的登录页面,控制器内部没有任何内容,但我得到了同样的错误,但是当我把它放在 Pushwoosh 事件之外时它正在工作。
  • 我在回答中添加了一些注释,希望对您有所帮助!
  • 您好再次感谢您的回复,收到推送时我的应用已经打开(不在后台或被杀死)
  • 要点是当您点击通知时 NavigationWindow 应该存在,因为您正在使用 $.nav 。那么你确定你在收到推送消息之前已经打开了NavigationWindow吗?
猜你喜欢
  • 2016-09-24
  • 1970-01-01
  • 1970-01-01
  • 2017-07-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-06
  • 1970-01-01
相关资源
最近更新 更多