前提・実現したいこと
<目的>
アプリがアクティブでない状態でも、Notification通知音が再生される
<状況>
GoogleFirebaseよりプッシュ通知を受け取ると、Notification通知がされるテストアプリを作成しています
作成したアプリがアクティブな状態でプッシュ通知を受信した場合は、Notification通知時に通知音が再生されます(期待動作通り)が、
アプリがアクティブでない状態(※)でプッシュ通知を受信した場合に、Notification通知はされますが、通知音が鳴りません
※「アクティブでない状態」ですが、下記を指します
・アプリ起動しているが、フォーカス?があたっていない状態(裏で生きてる状態です)
・アプリが終了されている状態
Notification通知音は下記音ファイルを指定しています
app/res/raw/applauses.mp3
該当のソースコード
コードは下記の通り
■app/src/main/java/xxx/xxxxxxxxxx/testnotifier/MainActivity.kt
Kotlin
1package xxx.xxxxxxxx.testnotifier 2 3import android.os.Bundle 4import android.support.v7.app.AppCompatActivity 5import android.util.Log 6import android.widget.Button 7import android.widget.Toast 8 9class MainActivity : AppCompatActivity() { 10 11 override fun onCreate(savedInstanceState: Bundle?) { 12 super.onCreate(savedInstanceState) 13 setContentView(R.layout.activity_main) 14 } 15}
■app/src/main/java/xxx/xxxxxxxx/testnotifier/testMessagingService.kt
Kotlin
1package xxx.xxxxxxxx.testnotifier 2 3import android.app.NotificationManager 4import android.content.ContentResolver 5import android.content.Context 6import android.net.Uri 7import android.support.v4.app.NotificationCompat 8import android.util.Log 9import com.google.firebase.messaging.FirebaseMessagingService 10import com.google.firebase.messaging.RemoteMessage 11import com.google.firebase.iid.FirebaseInstanceId 12 13class testMessagingService : FirebaseMessagingService() { 14 15 private fun convertUriFromResId(context: Context, resId: Int): Uri { 16 val sb = StringBuilder() 17 sb.append(ContentResolver.SCHEME_ANDROID_RESOURCE) 18 sb.append("://") 19 sb.append(context.resources.getResourcePackageName(resId)) 20 sb.append("/") 21 sb.append(context.resources.getResourceTypeName(resId)) 22 sb.append("/") 23 sb.append(context.resources.getResourceEntryName(resId)) 24 return Uri.parse(sb.toString()) 25 } 26 27 override fun onMessageReceived(remoteMessage: RemoteMessage?) { 28 val ct = this.getApplicationContext() 29 30 val builder = NotificationCompat.Builder(this, "testxxx").apply { 31 setSmallIcon(R.drawable.ic_launcher_foreground) 32 setContentTitle("Title") 33 setContentText("This is a notification") 34 setAutoCancel(true) 35 setSound(convertUriFromResId(ct, R.raw.applauses)) 36 } 37 val notificationManager = this.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager 38 notificationManager.notify(0, builder.build()) 39 } 40}
■app/src/main/AndroidManifest.xml
xml
1<?xml version="1.0" encoding="utf-8"?> 2<manifest xmlns:android="http://schemas.android.com/apk/res/android" 3 package="xxx.xxxxxxxxxx.testnotifier"> 4 5 <application 6 android:allowBackup="true" 7 android:icon="@mipmap/ic_launcher" 8 android:label="@string/app_name" 9 android:roundIcon="@mipmap/ic_launcher_round" 10 android:supportsRtl="true" 11 android:theme="@style/AppTheme"> 12 <activity android:name=".MainActivity"> 13 <intent-filter> 14 <action android:name="android.intent.action.MAIN" /> 15 16 <category android:name="android.intent.category.LAUNCHER" /> 17 </intent-filter> 18 </activity> 19 <service android:name=".testMessagingService"> 20 <intent-filter> 21 <action android:name="com.google.firebase.INSTANCE_ID_EVENT" /> 22 <action android:name="com.google.firebase.MESSAGING_EVENT" /> 23 </intent-filter> 24 </service> 25 </application> 26 27</manifest>
補足情報
Android SDK:28
テスト機:Xperia XZ(SO-01J)(Android7.0)
回答1件
あなたの回答
tips
プレビュー