Titaniumã¢ããªã§Push Notification【Androidç】
ååã¯iOSã«ã¤ãã¦èª¿æ»。ä»åã¯Android端æ«ã«ããã·ã¥éç¥ããæ¹æ³ã調æ»ããã¨ãã®è¦æ¸。
éçºç°å¢: Mac OS X 10.9.4, Titanium SDK 3.3.0.GA, Android 4.0.3
ãµã¼ãã¼:CentOS 6.5
åèãµã¤ã
- Overview | Google Cloud Messaging | Android Developers
- Google Cloud Messaging for Android (GCM)ã使ã£ã¦ã¿ã - azukinohirokiã®æ¥è¨
- Androidã§ã®pushéç¥ã«ã¤ã㦠| Don't Fall - Titanium Mobileã¦ã¼ã¶ã¼ä¼ãµãã¼ãBBS
ç®æ¬¡
- Google Cloud Messaging(GCM)ãæå¹ã«
- GCMã¨ã®éä¿¡ç¨ã¢ã¸ã¥ã¼ã«ãæ¢ã
- Titaniumã¢ã¸ã¥ã¼ã«ããã«ãããç°å¢ãæ´ãã
- Titaniumã¢ã¸ã¥ã¼ã«ã®ãã«ãå®è¡
- ã¢ããªããã¢ã¸ã¥ã¼ã«ãå©ç¨
- ãµã¼ãã¼ããããã·ã¥éç¥ãã¹ã
- ãããã°ã¨æ¤è¨¼ãã¦ã¿ã
ï¼.Google Cloud Messaging(GCM)ãæå¹ã«
iOSã¯Apple Push Notification service(APNs)çµç±ã§ããã·ã¥éç¥ãéã。Androidã®å ´åã¯Google Cloud Messaging(GCM)çµç±。
GCMã®API Keyãåå¾
- Google API Consoleã«ã¢ã¯ã»ã¹
- ããã¸ã§ã¯ããä½æ。
- ããã¸ã§ã¯ãã®「Overview」ãéãã¨å·¦ä¸ã«「Project Number」ã表示ããã¦ããã®ã§ãããã¡ã¢。ãã¨ã§「sender_id」ã¨ãã¦ã¢ããªå´ã«è¨è¿°ãã。
- APIs & auth → APIsãã「Google Cloud Messaging for Android」ãæå¹ã«
- APIs & auth → Credentialsãã「Server Key」ãä½æ。
ï¼.GCMã¨ã®éä¿¡ç¨ã¢ã¸ã¥ã¼ã«ãæ¢ã
iOSã¿ããã«ãã¤ãã£ãã«ãµãã¼ããã¦ãªãã®ã§、å ¬éããã¦ããã¢ã¸ã¥ã¼ã«ãå©ç¨ãã。
「dist/com.activate.gcm-android-0.6.zip」ããã®ã¾ã¾å©ç¨ãã¦ãåé¡ãªãã£ã。ä¸å¿èªåã§ã«ã¹ã¿ãã¤ãºã§ããããã«ãã«ããã¦ã¿ã。
< 2014/12/15 Modified >
ãã¡ãã®è¨äºã§ç´¹ä»ããã¦ããæ¹ãããããä¿®æ£ããã¦ããã¿ãã。
ï¼.Titaniumã¢ã¸ã¥ã¼ã«ããã«ãããç°å¢ãæ´ãã
é·ããªã£ãã®ã§å¥è¨äºã«ãã。
ï¼.Titaniumã¢ã¸ã¥ã¼ã«ã®ãã«ãå®è¡
GitHubããcloneãã
$ git clone https://github.com/liccowee/Google-Cloud-Messaging--Titanium-
$ cd Google-Cloud-Messaging--Titanium-/
ç°å¢å¤æ°ãåºåãããã®ã§、空ã®sampleã¢ã¸ã¥ã¼ã«ãä½ã。
$ titanium create -p android -t module -d ./ -n sample --id com.sample
ç°å¢å¤æ°ãè¨è¿°ãããbuild.propertiesãã³ãã¼ãã。
$ cp sample/android/build.properties ./
ã¡ãªã¿ã«build.propertiesã¯ãããªæãã ã£ã。
titanium.platform=/Users/daiki/Library/Application Support/Titanium/mobilesdk/osx/3.3.0.GA/android
android.platform=/Applications/android-sdk-macosx/platforms/android-14
google.apis=/Applications/android-sdk-macosx/add-ons/addon-google_apis-google-14
manifestãç·¨é
$ vi ./manifest
version: 0.6
minsdk: 3.3.0.GA
< 2015/04/24 Modified >
SDK 3.5.1.GAã«ãããarchitecturesãå ããªãã¨ãã«ãã«å¤±æãã¦「There is discrepancy between the architectures specified in manifest and compiled binary.」ã¨ãããã。
$ vi ./manifest
name: gcmjs
moduleid: net.iamyellow.gcmjs
guid:
platform: android
minsdk: 3.5.1.GA
architectures: armeabi armeabi-v7a x86
timodule.xmlãç·¨é
$ vi ./timodule.xml
<uses-sdk android:minSdkVersion="10"/>
< 2015/04/24 Modified >
SDKãã¢ãããã¼ãããå ´åã¯build.propertiesã®å¤ããã§ãã¯
$ vi ./build.properties
titanium.platform=/Users/daiki/Library/Application Support/Titanium/mobilesdk/osx/3.5.1.GA/android
android.platform=/Applications/android-sdk-macosx/platforms/android-21
google.apis=/Applications/android-sdk-macosx/add-ons/addon-google_apis-google-21
distãã£ã¬ã¯ããªã¯ç©ºã«ãã¦ãã。$ rm dist/*
$ ant clean
ãã«ãå®è¡
$ ant
libãã£ã¬ã¯ããªããªãã£ã¦æããã。
$ mkdir lib
åãã«ã
$ ant
distãã£ã¬ã¯ããªã«åºåããã。
ï¼.ã¢ããªããã¢ã¸ã¥ã¼ã«ãå©ç¨
Titaniumããã¸ã§ã¯ãã®ç´ä¸ã«「com.activate.gcm-android-0.6.zip」ãè¨ç½®ããã ãã§、次åãã«ãæã«ã¤ã³ã¹ãã¼ã«ãã¦ããã。
èªåã§è§£åãã¦ãåã。
Titanium_Studio_Workspace/test_app/modules/android/com.activate.gcm
tiapp.xmlãç·¨é。sender_idã¯Google API Consoleã®「Project Number」
<modules>
<module platform="android" version="0.6">com.activate.gcm</module>
</modules>
<property name="com.activate.gcm.sender_id" type="string">123427670900</property>
<property name="com.activate.gcm.component" type="string">com.hoge.app/org.appcelerator.titanium.TiActivity</property>
ã¢ããªèµ·åå¾ã«ä¸è¨ã³ã¼ããå®è¡ãã¦åä½ããã確èª。
var gcm = require('com.activate.gcm');
gcm.registerC2dm({
success: function(e) {
console.log(e);
alert('Registration ID:' + gcm.getRegistrationId());
},
error: function(e) {
console.log(e);
},
callback: function(e) {
alert('JS message event: ' + JSON.stringify(e.data));
console.log(e);
}
});
GCMã«ããã¤ã¹ã®ç»é²ãæåããã¨「success」ããããã£ã«è¨å®ããé¢æ°ãå®è¡ããã¦、registrationIdãè¿ã£ã¦ãã。
ï¼.ãµã¼ãã¼ããããã·ã¥éç¥ãã¹ã
ãã£ãåå¾ããregistrationIdãã¿ã¼ã²ããã«ããã·ã¥éç¥ããµã¼ãã¼ããéä¿¡ãã¦ã¿ã。
ãã¡ããåèã«ããPHPãè¨ç½®。「$registration_id」ã«ã¯ããã¤ã¹ã®registrationId。「$header」ã®keyã«ã¯API Keyãè¨è¿°ãã。
ãã©ã¡ã¼ã¿ã¼ã®è©³ç´°ã¯å ¬å¼ãµã¤ããåèã«。
<?php
$url = 'https://android.googleapis.com/gcm/send';$registration_id = 'APAxxx';
$message = 'Hello, GCM!!';
$header = array(
'Content-Type: application/json',
'Authorization: key=xxx',
);
$post_list = array(
'registration_ids' => array($registration_id),
'collapse_key' => 'demo',
'data' => array( "message" => $message )
);$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($post_list));$ret = curl_exec($ch);
var_dump($ret);
?>
ãããå®è¡ããã¨ã¢ããªå´ã§「callback」ã«è¨å®ããé¢æ°ãå®è¡ãããã¯ã。
ï¼.ãããã°ã¨æ¤è¨¼ãã¦ã¿ã
ddmsãç«ã¡ä¸ãã¦ãã°åºåãç£è¦ãã¦ã¿ã。「C2dmModule」「GCM」ã¨ããTagã§çµãè¾¼ãã¨è¦ããã。
ã©ããªä»çµã¿ã§åãã¦ãããã¾ã ããåãã£ã¦ããªã。
< 2015/04/24 Modified >
ã¢ããªã®ti.xmlã«android:debuggable="true"ã追è¨ããã¨ddmsã§ãã®ã¢ããªã®åºåã ãã«çµãè¾¼ããã¨ãåºæ¥ãã®ã§ä¾¿å©。
ã¾ãtimodule.xmlã«
<property name="ti.android.debug" type="bool">true</property>
ã追è¨ããªãã¨ã¢ã¸ã¥ã¼ã«å
ã®
org.appcelerator.kroll.common.TiConfig.LOGD
ãtrueã«ãªããªãã®ã§、ãããã°ãã°ãåºåããªã。
ãã®ä»æ¤è¨¼ãã¦åãã£ããã¨ãªã©
- ã¢ããªãã¿ã¹ã¯ããã¼ã¸ã£ã¼ããKILLããæã¯ããã·ã¥éç¥ãåãåããªã。GCMBaseIntentServiceãããã·ã¥éç¥ã«åå¿ããªã。
ããã¯LINEãåã。ããã解決ããã«ã¯Messengerã®ããã«å¸¸æ稼åãããµã¼ãã¹ãä½ãå¿ è¦ããã。
ã¡ã¢ãªãå§è¿«ããã¦ã¢ã¯ãã£ããã£ãç ´æ£ãããã®ã¯Android端æ«ã®「éçºè åããªãã·ã§ã³」→「ã¢ã¯ãã£ããã£ãç ´æ£」ããã§ãã¯ããã¨åç¾ã§ãã。 - ç»é¢ãããã¯ãã¦ããç¶æ
ã ã¨com.activate.gcm.componentãæ£ããè¨å®ãã¦ãªãã¨åããªã(ãã¶ã)。ä¸è¨ã³ãã³ãã§ç¾å¨ç¨¼åä¸ã®Activityã表示ã§ãã。
$ ./adb shell dumpsys activity | grep -i run - åèµ·åç´å¾ã¯ãã¾ãåãã¦ãããªã。ddmsã§è¦ãã¨ãµã¼ãã¹ã¯åãã¦ããã·ã¥éç¥ãæ¤ç¥ãã¦ãã。è¦èª¿æ»。
< 2014/12/19 Modified >
ã¢ããªãããã¯ã°ã©ã¦ã³ãã®ã¨ãã«ãã¾ãåããªãã£ãã®ã¯gcm.jsã¨gcm_activity.jsãResourcesã®ç´ä¸ã«è¨ç½®ãã¦ãªãã£ããã。
Forkããã¦ããæ¹ãæç¶ä½¿ããããã£ã。
< Related Posts >