i have a uber-like project under android studio.
When a driver receives a ride, a notification sound plays, then the app opens, then it starts ringing for 60 sec.
I'm facing a problem with sound notification at driver app at incoming ride while app is running background, especially under Android 10
the key file that manage that is NotificationUtils.java
i had this part:
/**
* Playing notification sound
*/
public void playNotificationSound() {
try {
Uri alarmSound = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE
+ "://" + mContext.getPackageName() + "/raw/notification");
Ringtone r = RingtoneManager.getRingtone(mContext, alarmSound);
r.play();
} catch (Exception e) {
e.printStackTrace();
}
}
On android 10, when app is closed, notification sound sometimes works, the phone rings one time (without opening the app), but sometimes it doesn't work (seems like the app process is killed in background or in sleep mode)
A developer removed that part and replaced it with:
Intent intent = new Intent(mContext, BackgroundSoundService.class);
mContext.stopService(intent);
mContext.startService(intent);
}
public static class BackgroundSoundService extends Service {
private static final String TAG = null;
//MediaPlayer player;
Ringtone r;
public IBinder onBind(Intent arg0) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
try {
Uri alarmSound = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE
+ "://" + getApplicationContext().getPackageName() + "/raw/notification");
r= RingtoneManager.getRingtone(getApplicationContext(), alarmSound);
} catch (Exception e) {
e.printStackTrace();
}
}
public int onStartCommand(Intent intent, int flags, int startId) {
r.play();
return START_STICKY;
}
public void onStart(Intent intent, int startId) {
// TO DO
}
public IBinder onUnBind(Intent arg0) {
// TO DO Auto-generated method
return null;
}
public void onStop() {
}
public void onPause() {
}
@Override
public void onDestroy() {
r.stop();
}
@Override
public void onLowMemory() {
}
}
On android 10, when app is closed or opened, sound notification never plays! , it's only working well under android 9 and less. My developer left the project and i don't know how to fix that ?
any suggestion please ?
my manifest permissions
<uses-feature android:glEsVersion="0x00020000" android:required="true"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="com.pickup.driver.permission.MAPS_RECEIVE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.CALL_PHONE"/>
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE"/>
<uses-permission android:name="android.permission.GET_ACCOUNTS"/>
<uses-permission android:name="android.permission.USE_CREDENTIALS"/>
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
<service android:name="com.myproject.driver.FCM.NotificationUtils$BackgroundSoundService" android:enabled="true"></service>
question from:
https://stackoverflow.com/questions/65940896/sound-notification-not-playing-under-android-10-android-studio-project 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…