# [TITANIUM] 自定模組

- URL: https://justfly.idv.tw/titanium-%e8%87%aa%e5%ae%9a%e6%a8%a1%e7%b5%84/
- 日期: 2013-02-28
- 分類: WEB&amp;RIA
- 標籤: android, error, Google App, Titanium

https://justfly.idv.tw/2013/02/28/Just_1652.html

承繼之前在「[[TITANIUM] ALLOY與GCM的應用](https://justfly.idv.tw/2013/01/16/Just_1538.html)」中使用[Google-Cloud-Messaging-Titanium](https://github.com/liccowee/Google-Cloud-Messaging--Titanium-)來實現android與接收GCM的訊息後。隨之而生的問題是當我們在「啟動一次app，保持在背景動作的情況下重新開機後」就收不到push了。

雖然也有網友使用0.6版可以解決問題，只是我從0.2版用到0.6版，收不到就是收不到＠＠

看了[Google-Cloud-Messaging-Titanium的issues](https://github.com/liccowee/Google-Cloud-Messaging--Titanium-/issues)後，有很多人都主張重組模組。但是一開始當看到[官方說明](https://wiki.appcelerator.org/display/guides/Android+Module+Development+Guide)，一張落落長要安裝的工具就頭皮發麻….

不過經過測試，發現問題在於當app啟動時，C2dmModule.java，CommonUtilities.java，GCMIntentService.java會被註冊到service中。當service停止時GCM模組的Instance就被丟棄，所以GCMIntentService.java中

```
if (C2dmModule.getInstance() != null){
			C2dmModule.getInstance().sendMessage(data);
		}
```

就無法被執行。

解決的辦法是把他改寫成：

```
if (C2dmModule.getInstance() != null){
			C2dmModule.getInstance().sendMessage(data);
		}else {
			NotificationManager mManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
			Notification n = new Notification();
			Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("Justfly://tw.idv.justfly"));
			PendingIntent pi = PendingIntent.getActivity(this, 0, i, 0);

			n.icon = 2130837504;
			n.tickerText = messageArr[0];
			n.number = 1;
			n.flags = 1073741824;
			n.defaults |= Notification.DEFAULT_VIBRATE;
			n.setLatestEventInfo(getApplicationContext(), "Justfly", messageArr[0], pi);
			mManager.notify(1, n);

			Vibrator vib = (Vibrator)getSystemService(VIBRATOR_SERVICE);
			vib.vibrate(500);
		}
```

然後….還是要重祖一次模組＠＠

重組方式可以參考[Google-Cloud-Messaging–Titanium- モジュールを作る](http://qiita.com/items/af3634022e47497240b2)跟[官方說明](https://wiki.appcelerator.org/display/tis/Creating+a+New+Titanium+Module)。我只列一下我在重組時碰到的問題跟解決方式：

- ERR:Compile failed; see the compiler error output for details.You may need to install the Command Line Tools package through XCode, in case you haven’t done so yet的錯誤訊息。

ANS:可以參考[這篇](http://developer.appcelerator.com/question/137994/where-is-the-error-output-for-titanium-modules-compiler)的回答去安裝[Command Line Tools for Xcode](https://developer.apple.com/downloads/index.action?name=for%20Xcode%20-)跟重新命名模組專案。但是我後來是用命令列下ANT指令才解決。

- ERR:Compile failed; see the compiler error output for details.You may need to install the  Neither the ANDROID_NDK environment variable, or the android.ndk property is not set to an existing Android NDK installation (check your module’s build.properties)的錯誤訊息。

ANS:除了把[Android NDK](http://developer.android.com/tools/sdk/ndk/index.html)下載回來外，記得到build.properties加入：

android.ndk=/Users/justfly/Documents/android-ndk-r8d

指定ndk的位置

原本我一直覺得很怪的是，我明明是要做android的模組，為何他一直要我安裝xcode的Command Line Tools!?但是後來在titanium studio中的命令列中用ant指令來執行成功一次之後，那個錯誤訊息就不見了。目前依然..原因不明…..

(Photo via [1uplego](http://www.flickr.com/photos/1uplego/6898047487/), CC License)
