明星的jpush module android 版本用于用Titanium開發(fā)Android消息推送功能,使用步驟如下:
1.從github上下載titanium_module_jpush_android;
2.從項(xiàng)目中找到titanium_module_jpush_android/android/dist/com.mamashai.jpush-android-3.0.8.zip,解壓并將得到modules文件;
3.在需要消息推送的項(xiàng)目下新建modules目錄用于存放所需的modules,因module為Android版本,所以modules下新建android文件夾塘匣;
4.將解壓縮得到的modules目錄下android下的com.mamashai.jpush拷貝到新建的android文件夾下巷帝;
5.在xxx.js你所需要添加消息推送的js文件下)下鍵入如下代碼:
//訂閱推送:
var push = require('com.mamashai.jpush');
push.setAlias(Ti.App.Properties.getString("userid", ""), function(e){
Ti.API.log("register to jpush code: " + e.code + ", token: " + e.device_token);
});
//接收推送:
var act = Titanium.Android.currentActivity;
var _intent = act.intent;
var str = _intent.getStringExtra("cn.jpush.android.EXTRA");
if (str && str.length > 0){
//push_call(_intent.getStringExtra("cn.jpush.android.ALERT"), JSON.parse(str));
//自己寫的顯示消息框方法
show_alert("提示", e.intent.getStringExtra("cn.jpush.android.EXTRA"));
}
//用戶點(diǎn)擊了推送
var bc = Ti.Android.createBroadcastReceiver({
onReceived : function(e) {
Ti.API.info("cn.jpush.android.PUSH_ID: " + e.intent.getStringExtra("cn.jpush.android.PUSH_ID"));
Ti.API.info("app: " + e.intent.getStringExtra("app"));
Ti.API.info("cn.jpush.android.ALERT: " + e.intent.getStringExtra("cn.jpush.android.ALERT"));
Ti.API.info("cn.jpush.android.EXTRA: " + e.intent.getStringExtra("cn.jpush.android.EXTRA"));
Ti.API.info("cn.jpush.android.NOTIFICATION_ID: "+ e.intent.getStringExtra("cn.jpush.android.NOTIFICATION_ID"));
Ti.API.info("cn.jpush.android.NOTIFICATION_CONTENT_TITLE: " + e.intent.getStringExtra("cn.jpush.android.NOTIFICATION_CONTENT_TITLE"));
Ti.API.info("cn.jpush.android.MSG_ID: " + e.intent.getStringExtra("cn.jpush.android.MSG_ID"));
Ti.API.info("cn.jpush.android.TITLE: " + e.intent.getStringExtra("cn.jpush.android.TITLE"));
Ti.API.info("cn.jpush.android.MESSAGE: " + e.intent.getStringExtra("cn.jpush.android.MESSAGE"));
Ti.API.info("cn.jpush.android.CONTENT_TYPE: " + e.intent.getStringExtra("cn.jpush.android.CONTENT_TYPE"));
var str = e.intent.getStringExtra("cn.jpush.android.EXTRA");
if (str && str.length > 0){
//自己寫的顯示消息框方法
show_alert("提示", e.intent.getStringExtra("cn.jpush.android.EXTRA"));
}
}
});
Ti.Android.registerBroadcastReceiver(bc, ['mamashai_jpush']);
//一定要釋放掉楞泼,否則容易出問題
win.addEventListener("close", function(){
Ti.Android.unregisterBroadcastReceiver(bc);
});
//收到推送,還未打開
var bc2 = Ti.Android.createBroadcastReceiver({
onReceived : function(e) {
var json = JSON.parse(e.intent.getStringExtra("cn.jpush.android.EXTRA"));
//to do
}
});
Ti.Android.registerBroadcastReceiver(bc2, ['mamashai_jpush_received']);
//一定要釋放掉堕阔,否則容易出問題
win.addEventListener("close", function(){
Ti.Android.unregisterBroadcastReceiver(bc2);
});
6.在tiapp.xml文件下鍵入如下代碼:
<modules>
<module platform="android" version="3.0.8">com.mamashai.jpush</module>
</modules>
7.注冊并登錄Jpush后,在控制臺創(chuàng)建應(yīng)用,獲得AppKey超陆;
8.在tiapp.xml文件下的
<android>
</android>
內(nèi)鍵入如下代碼:
<manifest package="(你的包名,例如com.uubpay.xxx)" >
<permission
android:name="(你的包名时呀,例如com.uubpay.xxx).permission.JPUSH_MESSAGE"
android:protectionLevel="signature" />
<!-- Required 一些系統(tǒng)要求的權(quán)限张漂,如訪問網(wǎng)絡(luò)等-->
<uses-permission android:name="(你的包名,例如com.uubpay.xxx).permission.JPUSH_MESSAGE" />
<uses-permission android:name="android.permission.RECEIVE_USER_PRESENT" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
<!-- Optional for location -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<application>
<service android:enabled="true" android:exported="false" android:name="cn.jpush.android.service.PushService">
<intent-filter>
<action android:name="cn.jpush.android.intent.REGISTER"/>
<action android:name="cn.jpush.android.intent.REPORT"/>
<action android:name="cn.jpush.android.intent.PushService"/>
<action android:name="cn.jpush.android.intent.PUSH_TIME"/>
</intent-filter>
</service>
<receiver android:enabled="true" android:name="cn.jpush.android.service.PushReceiver">
<intent-filter android:priority="1000">
<action android:name="cn.jpush.android.intent.NOTIFICATION_RECEIVED_PROXY"/>
<category android:name="(你的包名谨娜,例如com.uubpay.xxx)"/>
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.USER_PRESENT"/>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.PACKAGE_ADDED"/>
<action android:name="android.intent.action.PACKAGE_REMOVED"/>
<data android:scheme="package"/>
</intent-filter>
</receiver>
<receiver android:name="com.mamashai.jpush.MamashaiReceiver">
<intent-filter>
<action android:name="cn.jpush.android.intent.REGISTRATION"/>
<action android:name="cn.jpush.android.intent.UNREGISTRATION"/>
<action android:name="cn.jpush.android.intent.MESSAGE_RECEIVED"/>
<!--Required 用戶接收SDK消息的intent-->
<action android:name="cn.jpush.android.intent.NOTIFICATION_RECEIVED"/>
<!--Required 用戶接收SDK通知欄信息的intent-->
<action android:name="cn.jpush.android.intent.NOTIFICATION_OPENED"/>
<!--Required 用戶打開自定義通知欄的intent-->
<action android:name="cn.jpush.android.intent.ACTION_RICHPUSH_CALLBACK"/>
<!--Optional 用戶接受Rich Push Javascript 回調(diào)函數(shù)的intent-->
<action android:name="cn.jpush.android.intent.CONNECTION"/>
<!-- 接收網(wǎng)絡(luò)變化 連接/斷開 since 1.6.3 -->
<category android:name="(你的包名航攒,例如com.uubpay.xxx)"/>
</intent-filter>
</receiver>
<activity
android:configChanges="orientation|keyboardHidden"
android:name="cn.jpush.android.ui.PushActivity" android:theme="@android:style/Theme.Translucent.NoTitleBar">
<intent-filter>
<action android:name="cn.jpush.android.ui.PushActivity"/>
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="(你的包名,例如com.uubpay.xxx)"/>
</intent-filter>
</activity>
<service android:enabled="true" android:exported="false" android:name="cn.jpush.android.service.DownloadService"/>
<receiver android:name="cn.jpush.android.service.AlarmReceiver"/>
<!-- Required . Enable it you can get statistics data with channel -->
<meta-data android:name="JPUSH_CHANNEL" android:value="c_1908"/>
<meta-data android:name="JPUSH_APPKEY" android:value="(你的AppKey)" />
</application>
</manifest>
將如上代碼中的包名和AppKey替換成你自己的(AppKey從Jpush的應(yīng)用管理中得到)
9.運(yùn)行項(xiàng)目趴梢;
10.在Jpush中推送消息漠畜,測試能否實(shí)現(xiàn)消息推送(確保測試時客戶端聯(lián)網(wǎng)),另外,如果通知欄下拉菜單圖標(biāo)不顯示請換個手機(jī)品牌試一下,coolpad發(fā)現(xiàn)就不顯示圖標(biāo)垢油。