Upgrade to Pro — share decks privately, control downloads, hide ads and more …



2016.04.26 関西モバイルアプリ研究会 #13 用資料です。


April 25, 2016

More Decks by masanori_msl

Other Decks in Programming


  1. Doze Mode  Android6.0で追加された省電力化のための機能  以下の条件を満たすと開始  Screen Off 

    端末が移動していない  電源に接続されていない  上記3条件が揃った状態で一定時間が経過
  2. Sample WakeupApplication Blog Google Cloud Messaging(GCM) を受信する 1 (受信側) https://github.com/masanori840816/WakeupApplication

    http://mslgt.hatenablog.com/entry/2016/04/22/224801 Google Cloud Messaging(GCM) を受信する 2 (送信側) http://mslgt.hatenablog.com/entry/2016/04/23/001000
  3. 3. GCMを使う(受信側) AndroidManifest.xml <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="jp.wakeupapplication"> <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" /> <uses-permission

    android:name="android.permission.WAKE_LOCK" /> <application ~ <!-- Messageを受信したらGcmListenerServiceに伝える. コードの記述は不要 --> <receiver android:name="com.google.android.gms.gcm.GcmReceiver" android:exported="true" android:permission="com.google.android.c2dm.permission.SEND" > <intent-filter> <action android:name="com.google.android.c2dm.intent.RECEIVE" /> <category android:name="jp.wakeupapplication" /> </intent-filter> </receiver>
  4. 3. GCMを使う(受信側) <!-- サーバー側でMessageの送信に必要なToken を取得する --> <service android:name="jp.wakeupapplication.RegistrationIntentService" android:exported="false"> </service>

    <!-- GcmListenerServiceを継承し、Message を受信する --> <service android:name="jp.wakeupapplication.MessageListenerService" android:exported="false" > <intent-filter> <action android:name="com.google.android.c2dm.intent.RECEIVE" /> </intent-filter> </service> <!-- Token が更新された通知を受け取る --> <service android:name="jp.wakeupapplication.RefreshTokenListenerService" android:exported="false"> <intent-filter> <action android:name="com.google.android.gms.iid.InstanceID"/> </intent-filter> </service>
  5. 3. GCMを使う(受信側) RegistrationIntentService.kt class RegistrationIntentService: IntentService("Registration Service") { override fun

    onHandleIntent(intent: Intent){ try { var instanceID = InstanceID.getInstance(this) // Tokenを取得する var token = instanceID.getToken({API Consoleで取得したSenderID}, GoogleCloudMessaging.INSTANCE_ID_SCOPE, null) // 取得したTokenをServer側に渡し、それを元にMessageを送信する. Log.i("WUA", "Token: " + token) } catch (e:Exception) { Log.d("WUA", "Failed to get token", e) } } } Tokenを取得する
  6. 3. GCMを使う(受信側) RegistrationIntentService.kt class MessageListenerService : GcmListenerService(){ // サーバーからMessageを受信したら実行される. override

    fun onMessageReceived(from: String, data: Bundle?) { var message = data?.getString("message") // TODO: Notificationの表示など. } Messageを受信する
  7. 3. GCMを使う(送信側) RubyでGCM(Gem)を使う gcmSender.rb # encoding: utf-8 # Windows環境下でのOpenSSLのエラー対応. ENV['SSL_CERT_FILE']

    = File.expand_path('ca-bundle.crt') require 'gcm' gcm = GCM.new(“{API Consoleで取得したサーバー用API Key}”) # Messageの優先度を「高」にするとDozeモード中でもリアルタイム受信が可能. option = {priority: 'high', data: {message: 'Wake up!'} } response = gcm.send_notification(“{Android Appで取得したToken}”, option) puts response
  8. 参考 Optimizing for Doze and App Standby - Android Developers

    http://developer.android.com/intl/ja/training/monitoring-device- state/doze-standby.html 動作の変更点 - Android Developers http://developer.android.com/intl/ja/preview/behavior- changes.html Android Nで導入される「浅いDoze」について - ブライテクノBlog http://brightechno.com/blog/archives/405 Google Samples – Google Services - GCM https://github.com/googlesamples/google-services/tree/master/ android/gcm
  9. 参考 Google Play Servicesを使ってアプリにGCM を導入する - mokelab tech sheets https://tech.mokelab.com/android/gms/gcm/client.html

    Android Marshmallow の Doze テストの正しい手順 - Qiita http://qiita.com/usagimaru/items/7c549336a9aee447dd97 gcm – specialdb - GitHub https://github.com/spacialdb/gcm
  10. Credit Kotlin  Main Page:  License: Material icons 

    Main Page:  License:  Main Page:  License: Google Noto Fonts https://github.com/JetBrains/kotlin Apache License 2.0 https://design.google.com/icons/ https://www.google.com/get/noto/ CC-BY License SIL Open Font License (OFL)
  11. Credit Google Samples – Google Services - GCM  Main

    Page:  License: https://github.com/googlesamples/google- services/tree/master/android/gcm Copyright 2015 Google, Inc. Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.