AndroidでDevice Tokenの取得

sample

https://developers.google.com/cloud-messaging/android/start

https://github.com/googlesamples/google-services.git

方法

app/build.gradleのdependenciesに追記

compile 'com.google.android.gms:play-services-gcm:8.4.0'

res/value/string.xmlにSenderIDを追加

SenderIDはCloud Messaging  |  Google Developersから取得

<string name="gcm_defaultSenderId">0000000000000000</string>

RegistrationIntentServiceクラスを作成

class RegistrationIntentService : IntentService("RegIntentService") {

    private val TAG = "RegIntentService"

    override fun onHandleIntent(intent: Intent) {

        try {
            val instanceID = InstanceID.getInstance(this)
            val token = instanceID.getToken(getString(R.string.gcm_defaultSenderId),
                    GoogleCloudMessaging.INSTANCE_ID_SCOPE, null)

            // TODO: Implement this method to send any registration to your app's servers.
            sendRegistrationToServer(token)

            //SharedPreferencesにデバイストークンを保存する処理など

        } catch (e: Exception) {
            Log.d(TAG, "Failed to complete token refresh", e)
            //SharedPreferencesに保存されているデバイストークンを削除する処理など
        }
    }

    private fun sendRegistrationToServer(token: String) {
        //なんらかの処理
    }

}

デバイストークンを取得する側に追記

class HogeFragment: Fragment(){
    private val PLAY_SERVICES_RESOLUTION_REQUEST = 9000
    private val TAG = "HogeFragment"
    
    ~デバイストークンを取得したいところで~
        if (checkPlayServices()) {
            // Start IntentService to register this application with GCM.
            val intent = Intent(activity, RegistrationIntentService::class.java)
            activity.startService(intent)
        }
    ~~~~~~~~~~~~~~~~~~~
    
    private fun checkPlayServices(): Boolean {
        val apiAvailability = GoogleApiAvailability.getInstance()
        val resultCode = apiAvailability.isGooglePlayServicesAvailable(activity)
        if (resultCode != ConnectionResult.SUCCESS) {
            if (apiAvailability.isUserResolvableError(resultCode)) {
                apiAvailability.getErrorDialog(activity, resultCode, PLAY_SERVICES_RESOLUTION_REQUEST).show()
            } else {
                Log.i(TAG, "This device is not supported.")
            }
            return false
        }
        return true
    }
}

マニフェストファイルに追記

<service android:name=".utility.RegistrationIntentService"></service>

参考

http://techbooster.jpn.org/andriod/application/1570/

はじめてのReact

test.html

<!DOCTYPE html>
<html>

<head>
<meta charset="utf-8" />
<title>React Test</title>
<script src="https://fb.me/react-0.14.2.js"></script>
<script src="https://fb.me/react-dom-0.14.2.js"></script>
<script src="lib/jquery-2.2.0.min.js"></script>
<script src="reacttest.js"></script>
</head>

<body>
<div id="content"></div>
</body>

</html>

 

reacttest.js

$(function(){
var CommentBox = React.createClass({
render: function() {
return (
React.createElement(
'div',
{className: "commentBox"},
"Hello, world! I am a CommentBox." ) ); } });

ReactDOM.render( React.createElement(CommentBox,null), document.getElementById('content') );
});

 

とりあえずrenderを書けばよい

AngularJSでフォームの値を操作する

sample.html

<!doctype html>
<html ng-app="MyApp">
<head>
<script src="angular.min.js"></script>
<script src="sample.js"></script>
</head>
<body ng-controller="MyController">
何か入力してください<input type="text" ng-model="Input">「{{Input}}
</body>
</html>

sample.js

var myApp = angular.module('MyApp', []);

myApp.controller('MyController', ['$scope', function($scope) {
$scope.Input = "あいうえお";
}]);