質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.51%
Android

Androidは、Google社が開発したスマートフォンやタブレットなど携帯端末向けのプラットフォームです。 カーネル・ミドルウェア・ユーザーインターフェイス・ウェブブラウザ・電話帳などのアプリケーションやソフトウェアをひとつにまとめて構成。 カーネル・ライブラリ・ランタイムはほとんどがC言語/C++、アプリケーションなどはJavaSEのサブセットとAndroid環境で書かれています。

Q&A

解決済

2回答

4005閲覧

Android Firebaseでの通知受信について

WestField

総合スコア18

Android

Androidは、Google社が開発したスマートフォンやタブレットなど携帯端末向けのプラットフォームです。 カーネル・ミドルウェア・ユーザーインターフェイス・ウェブブラウザ・電話帳などのアプリケーションやソフトウェアをひとつにまとめて構成。 カーネル・ライブラリ・ランタイムはほとんどがC言語/C++、アプリケーションなどはJavaSEのサブセットとAndroid環境で書かれています。

0グッド

0クリップ

投稿2016/12/28 12:53

FirebaseでPush通知を処理するプログラムを作っています。
端末がスリープ状態にある時、通知を受けた時点でスリープから復帰したいのですが、
どうしても出来ません。

import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.media.AudioManager; import android.media.RingtoneManager; import android.net.Uri; import com.google.firebase.messaging.FirebaseMessagingService; import com.google.firebase.messaging.RemoteMessage; import java.util.Map; import biz.flashsystem.cmasystem.HomeActivity; import biz.flashsystem.cmasystem.R; import static android.app.Notification.DEFAULT_ALL; import static android.app.Notification.PRIORITY_MAX; /** * Created by WestField_w7 on 2016/09/13. */ public class FcmMessagingService extends FirebaseMessagingService { private String TAG = "FcmMessagingService"; @Override public void onMessageReceived(RemoteMessage remoteMessage) { if (remoteMessage.getData().size() > 0) { Map<String, String> data = remoteMessage.getData(); String title = data.get("custom_title"); String body = data.get("custom_body"); sendNotification(title, body); } if (remoteMessage.getNotification() != null) { } } private void sendNotification(String messageTitle, String messageBody) { Intent intent = new Intent(this, HomeActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent, PendingIntent.FLAG_ONE_SHOT); Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); Notification notification = new Notification.Builder(this) .setSmallIcon(R.mipmap.ic_launcher) .setContentTitle(messageTitle) .setContentText(messageBody) .setAutoCancel(true) .setDefaults(DEFAULT_ALL) .setPriority(PRIORITY_MAX) .setContentIntent(pendingIntent) .build(); NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.notify(1, notification); } }

現在は上記のように作っているのですが、上記だとスリープから復帰するどころか音も鳴らずバイブもしません。

どうすれば良いのでしょうか?

宜しくお願い致します。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答2

0

自己解決

import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.media.AudioManager; import android.media.RingtoneManager; import android.net.Uri; import android.os.PowerManager; import android.support.v7.app.NotificationCompat; import android.util.Log; import com.google.firebase.messaging.FirebaseMessagingService; import com.google.firebase.messaging.RemoteMessage; import com.google.firebase.messaging.RemoteMessage.Notification; import java.util.Map; import static android.app.Notification.DEFAULT_ALL; import static android.app.Notification.PRIORITY_MAX; /** * Created by WestField_w7 on 2016/09/13. */ public class FcmMessagingService extends FirebaseMessagingService { private String TAG = "FcmMessagingService"; private AudioManager audioManager; private PowerManager.WakeLock wakelock; private int volumeLevel; @Override public void onMessageReceived(RemoteMessage remoteMessage) { Map<String, String> fcm_data = remoteMessage.getData(); RemoteMessage.Notification fcm_notification = remoteMessage.getNotification(); wakeUp(); if (fcm_data.size() > 0) { int level = Integer.parseInt(fcm_data.get("level")) * 2; setAlertSound(level); sendNotification(fcm_data.get("title"), fcm_data.get("body")); setAlertSound(volumeLevel); } if (fcm_notification != null) { sendNotification(fcm_notification.getTitle(), fcm_notification.getBody()); } } private void sendNotification(String messageTitle, String messageBody) { try{ Log.d(TAG + "R57",messageTitle); Intent intent = new Intent(this, HomeActivity.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK); PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE); NotificationCompat.Builder builder = (android.support.v7.app.NotificationCompat.Builder) new NotificationCompat.Builder(getApplicationContext()) .setSmallIcon(R.mipmap.ic_launcher) .setVibrate(new long[]{0, 1000}) .setSound(alarmSound) .setAutoCancel(true) .setContentText(messageBody) .setContentTitle(messageTitle) .setColor(getResources().getColor(R.color.tableColor)) .setContentIntent(contentIntent); NotificationManager mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE); mNotificationManager.notify(1,builder.build()); }catch(Exception e){ e.printStackTrace(); } } private void setAlertSound(int level){ //Log.d(TAG + "R81",String.valueOf(level)); audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE); audioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL); volumeLevel = audioManager.getStreamVolume(AudioManager.STREAM_NOTIFICATION); audioManager.setStreamVolume(AudioManager.STREAM_NOTIFICATION,level,1); } private void wakeUp(){ wakelock = ((PowerManager) getSystemService(android.content.Context.POWER_SERVICE)) .newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.ON_AFTER_RELEASE, "disableLock"); wakelock.acquire(20000); } }

サーバ側JSON

{
"to": "device token",
"data": {
"title": "タイトル",
"body": "本文",
"level": "通知音量レベル"
}
}

以上で通知を受信した時に復帰させる事が出来ました。

投稿2016/12/29 10:29

WestField

総合スコア18

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

PowerManagerを使ってスリープ状態から復帰しないとできないと思いますよ。
まずはWakeLockのキーワードで調べましょう。

投稿2016/12/28 17:02

yona

総合スコア18155

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

WestField

2016/12/29 00:24

ご回答有難う御座います。 仰る通りでPowerManagerを使わないと復帰出来ないのは確かです。 しかしながら、よくよく調べてみるとFirebaseの通知仕様に原因がある事が判りました。 現状の状態でonMessageRevcievedにWakeLockを組み込んでも上手くいかなかったのは Notificationの仕様上アプリが起動していない状態ではonMessageRecievedを通過せずに自動的に表示されるようになっているようです。 アプリが起動している状態ではonMessageRecievedを通るようです。 FirebaseのPush通知仕様にNotificationとdataの2種類の構造があり、data構造を持つ通知だと起動していない状態でもonMessageRecievedを通るような事が書かれていましたので、試してみたいと思います。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.51%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問