收到推送通知后打开窗口时出现Titanium appcelerator错误

时间:2017-01-19 13:27:11

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

我使用钛金属appcelerator作为一个小应用程序,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>
&#13;
&#13;
&#13;

其次是index.js,我接收push并希望将用户重定向到登录js,目的是从推送自定义值打开相应的页面,但这里我做的很简单,只是为了测试。 /强>

&#13;
&#13;
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');
    }  
);
&#13;
&#13;
&#13;

最后一个login.xml和login.js

&#13;
&#13;
<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');
&#13;
&#13;
&#13;

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

&#13;
&#13;
[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] :  }
&#13;
&#13;
&#13;

我不知道错误在哪里,你能帮我解决一下吗? 谢谢。

1 个答案:

答案 0 :(得分:1)

您只需要对代码进行一些更改:

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

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

对于 iOS - 您需要使用 NavigationWindow openWindow()方法,对于 Android ,这很简单打开()致电。

注意:

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

这就是为什么你得到那个空错误的原因,因为当你收到通知并点击它时,它会打开应用程序并运行 pushwoosh.onPushOpened方法,直到这次你没有任何NavigationWindow创建。所以你需要一个不同的流程来导航到不同的部分。

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

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

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

我希望您现在能清楚地知道实际导致您的应用显示错误的原因。